wecui
Version:
一款基于Vue2.x版本的移动端web组件
106 lines (86 loc) • 3.1 kB
JavaScript
var assert = require('./assert');
var isTypeName = require('./isTypeName');
var String = require('./String');
var Function = require('./Function');
var isArray = require('./isArray');
var isObject = require('./isObject');
var create = require('./create');
var mixin = require('./mixin');
var isStruct = require('./isStruct');
var getTypeName = require('./getTypeName');
var dict = require('./dict');
function getDefaultName(props) {
return '{' + Object.keys(props).map(function (prop) {
return prop + ': ' + getTypeName(props[prop]);
}).join(', ') + '}';
}
function extend(mixins, name) {
if (process.env.NODE_ENV !== 'production') {
assert(isArray(mixins) && mixins.every(function (x) {
return isObject(x) || isStruct(x);
}), function () { return 'Invalid argument mixins supplied to extend(mixins, name), expected an array of objects or structs'; });
}
var props = {};
var prototype = {};
mixins.forEach(function (struct) {
if (isObject(struct)) {
mixin(props, struct);
}
else {
mixin(props, struct.meta.props);
mixin(prototype, struct.prototype);
}
});
var ret = struct(props, name);
mixin(ret.prototype, prototype);
return ret;
}
function struct(props, name) {
if (process.env.NODE_ENV !== 'production') {
assert(dict(String, Function).is(props), function () { return 'Invalid argument props ' + assert.stringify(props) + ' supplied to struct(props, [name]) combinator (expected a dictionary String -> Type)'; });
assert(isTypeName(name), function () { return 'Invalid argument name ' + assert.stringify(name) + ' supplied to struct(props, [name]) combinator (expected a string)'; });
}
var displayName = name || getDefaultName(props);
function Struct(value, path) {
if (Struct.is(value)) { // implements idempotency
return value;
}
if (process.env.NODE_ENV !== 'production') {
path = path || [displayName];
assert(isObject(value), function () { return 'Invalid value ' + assert.stringify(value) + ' supplied to ' + path.join('/') + ' (expected an object)'; });
}
if (!(this instanceof Struct)) { // `new` is optional
return new Struct(value, path);
}
for (var k in props) {
if (props.hasOwnProperty(k)) {
var expected = props[k];
var actual = value[k];
this[k] = create(expected, actual, ( process.env.NODE_ENV !== 'production' ? path.concat(k + ': ' + getTypeName(expected)) : null ));
}
}
if (process.env.NODE_ENV !== 'production') {
Object.freeze(this);
}
}
Struct.meta = {
kind: 'struct',
props: props,
name: name,
identity: false
};
Struct.displayName = displayName;
Struct.is = function (x) {
return x instanceof Struct;
};
Struct.update = function (instance, patch) {
return new Struct(assert.update(instance, patch));
};
Struct.extend = function (structs, name) {
return extend([Struct].concat(structs), name);
};
return Struct;
}
struct.getDefaultName = getDefaultName;
struct.extend = extend;
module.exports = struct;