immutable-js
Version:
Immutable types in JavaScript
47 lines (41 loc) • 1.37 kB
JavaScript
/**
* Copyright (c) 2015, Jan Biasi.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
export var TYPEDEF_FIELD = '__type__';
export function typedef(blueprint, prefered) {
if(typeof blueprint === 'object') {
if(!blueprint.prototype) {
return;
}
for(var scheme in blueprint) {
if(scheme && blueprint.hasOwnProperty(scheme)) {
typedef(blueprint[scheme], scheme);
}
}
} else if(blueprint && typeof prefered === 'string') {
blueprint.prototype[TYPEDEF_FIELD] = prefered;
}
}
export function invariant(condition, error) {
if (!condition) throw new Error(error);
}
/**
* Contributes additional methods to a constructor
*/
export function mixin(ctor, funcs) {
var copier = key => { ctor.prototype[key] = funcs[key]; };
Object.keys(funcs).forEach(copier);
Object.getOwnPropertySymbols && Object.getOwnPropertySymbols(funcs).forEach(copier);
return ctor;
}
export function createClass(ctor, superClass) {
if (superClass) {
ctor.prototype = Object.create(superClass.prototype);
}
ctor.prototype.constructor = ctor;
}