type-definitions
Version:
Define and validate types in js
948 lines (733 loc) • 36.3 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["type-definitions"] = factory();
else
root["type-definitions"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 7);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AnyType = exports.UnionType = exports.BaseType = undefined;
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.optional = optional;
exports.coerce = coerce;
exports.unionOf = unionOf;
exports.defaultValue = defaultValue;
var _nativeTypeMap = __webpack_require__(1);
var _nativeTypeMap2 = _interopRequireDefault(_nativeTypeMap);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var BaseType = exports.BaseType = function () {
function BaseType() {
_classCallCheck(this, BaseType);
}
_createClass(BaseType, null, [{
key: 'isOfType',
value: function isOfType(val) {
console.error('An isOfType function needs to be defined for "' + this.name + '"');
return false;
}
}, {
key: 'create',
value: function create() {
var val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.defaultValue;
// console.log(val, typeof val)
if (!this.isOfType(val)) {
throw 'The provided default value ' + JSON.stringify(val) + ' does not match the type definition';
}
return val;
}
}, {
key: 'withDefault',
value: function withDefault(val) {
return defaultValue(this, val);
}
}, {
key: 'optional',
get: function get() {
return optional(this);
}
}, {
key: 'defaultValue',
get: function get() {
return undefined;
}
}]);
return BaseType;
}();
var UnionType = exports.UnionType = function (_BaseType) {
_inherits(UnionType, _BaseType);
function UnionType() {
_classCallCheck(this, UnionType);
return _possibleConstructorReturn(this, (UnionType.__proto__ || Object.getPrototypeOf(UnionType)).apply(this, arguments));
}
_createClass(UnionType, null, [{
key: 'ofTypes',
value: function ofTypes() {
return unionOf.apply(undefined, arguments);
}
}, {
key: 'isOfType',
value: function isOfType(val) {
return this.types.some(function (type) {
return type.isOfType(val);
});
}
}, {
key: 'types',
get: function get() {
console.error('This type is meant to be extended with an overrided static `types` property. It\'s suggested to use `Union.ofTypes(...types)` to generate a new union');
return [];
}
}]);
return UnionType;
}(BaseType);
var AnyType = exports.AnyType = function (_BaseType2) {
_inherits(AnyType, _BaseType2);
function AnyType() {
_classCallCheck(this, AnyType);
return _possibleConstructorReturn(this, (AnyType.__proto__ || Object.getPrototypeOf(AnyType)).apply(this, arguments));
}
_createClass(AnyType, null, [{
key: 'isOfType',
value: function isOfType(val) {
return val !== undefined;
}
}, {
key: 'defaultValue',
get: function get() {
return null;
}
}]);
return AnyType;
}(BaseType);
function optional(type) {
return function (_coerce) {
_inherits(OptionalType, _coerce);
function OptionalType() {
_classCallCheck(this, OptionalType);
return _possibleConstructorReturn(this, (OptionalType.__proto__ || Object.getPrototypeOf(OptionalType)).apply(this, arguments));
}
_createClass(OptionalType, null, [{
key: 'isOfType',
value: function isOfType(val) {
return _get(OptionalType.__proto__ || Object.getPrototypeOf(OptionalType), 'isOfType', this).call(this, val) || val === undefined || val === null;
}
}, {
key: 'defaultValue',
get: function get() {
return _get(OptionalType.__proto__ || Object.getPrototypeOf(OptionalType), 'definedDefaultValue', this) === undefined ? null : _get(OptionalType.__proto__ || Object.getPrototypeOf(OptionalType), 'defaultValue', this);
}
}]);
return OptionalType;
}(coerce(type));
}
function coerce(type) {
if (typeof type.isOfType === 'function') {
if (type.prototype instanceof BaseType) {
return type;
} else {
return function (_BaseType3) {
_inherits(CoercedType, _BaseType3);
function CoercedType() {
_classCallCheck(this, CoercedType);
return _possibleConstructorReturn(this, (CoercedType.__proto__ || Object.getPrototypeOf(CoercedType)).apply(this, arguments));
}
_createClass(CoercedType, null, [{
key: 'isOfType',
value: function isOfType(val) {
return type.isOfType(val);
}
}, {
key: 'create',
value: function create() {
var _get2;
console.warn('.create() doesn\'t work well with custom classes, this probably isn\'t doing what you expect');
for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
params[_key] = arguments[_key];
}
return (_get2 = _get(CoercedType.__proto__ || Object.getPrototypeOf(CoercedType), 'create', this)).call.apply(_get2, [this].concat(params));
}
}, {
key: 'defaultValue',
get: function get() {
return Object.create(type);
}
}]);
return CoercedType;
}(BaseType);
}
}
var derivedType = _nativeTypeMap2.default.get(type);
if (!derivedType) {
if (Array.isArray(type)) {
derivedType = _nativeTypeMap2.default.get(Array);
// Arrays of length 1, all items of the array should be of that type
if (type.length === 1) {
derivedType = derivedType.ofType(type[0]);
// Array of greater lengths, type should be a union of all types
} else if (type.length > 1) {
derivedType = derivedType.ofType(unionOf.apply(undefined, _toConsumableArray(type)));
}
} else if (type.constructor === Object) {
derivedType = _nativeTypeMap2.default.get(Object);
// If there are keys, add a definition to the object
if (Object.keys(type).length) {
derivedType = derivedType.withDefinition(type);
}
} else {
derivedType = function (_BaseType4) {
_inherits(InsatanceOfType, _BaseType4);
function InsatanceOfType() {
_classCallCheck(this, InsatanceOfType);
return _possibleConstructorReturn(this, (InsatanceOfType.__proto__ || Object.getPrototypeOf(InsatanceOfType)).apply(this, arguments));
}
_createClass(InsatanceOfType, null, [{
key: 'isOfType',
value: function isOfType(val) {
return val instanceof type;
}
}, {
key: 'create',
value: function create() {
var _get3;
console.warn('.create() doesn\'t work well with custom classes, this probably isn\'t doing what you expect');
for (var _len2 = arguments.length, params = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
params[_key2] = arguments[_key2];
}
return (_get3 = _get(InsatanceOfType.__proto__ || Object.getPrototypeOf(InsatanceOfType), 'create', this)).call.apply(_get3, [this].concat(params));
}
}, {
key: 'defaultValue',
get: function get() {
return Object.create(type);
}
}]);
return InsatanceOfType;
}(BaseType);
}
}
return derivedType;
}
function unionOf() {
for (var _len3 = arguments.length, unionTypes = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
unionTypes[_key3] = arguments[_key3];
}
var coercedTypes = unionTypes.map(coerce);
return function (_UnionType) {
_inherits(DefinedUnionType, _UnionType);
function DefinedUnionType() {
_classCallCheck(this, DefinedUnionType);
return _possibleConstructorReturn(this, (DefinedUnionType.__proto__ || Object.getPrototypeOf(DefinedUnionType)).apply(this, arguments));
}
_createClass(DefinedUnionType, null, [{
key: 'types',
get: function get() {
return coercedTypes;
}
}, {
key: 'defaultValue',
get: function get() {
var val = _get(DefinedUnionType.__proto__ || Object.getPrototypeOf(DefinedUnionType), 'defaultValue', this);
if (val === undefined) {
val = this.types.map(function (t) {
return t.defaultValue;
}).find(function (tv) {
return tv !== undefined;
});
}
return val;
}
}]);
return DefinedUnionType;
}(UnionType);
}
function defaultValue(type, val) {
if (!type.isOfType(val)) {
throw 'Supplied default ' + JSON.stringify(val) + ' is not a valid value for type';
}
return function (_type) {
_inherits(TypeWithDefault, _type);
function TypeWithDefault() {
_classCallCheck(this, TypeWithDefault);
return _possibleConstructorReturn(this, (TypeWithDefault.__proto__ || Object.getPrototypeOf(TypeWithDefault)).apply(this, arguments));
}
_createClass(TypeWithDefault, null, [{
key: 'definedDefaultValue',
get: function get() {
return val;
}
}, {
key: 'defaultValue',
get: function get() {
return this.definedDefaultValue;
}
}]);
return TypeWithDefault;
}(type);
}
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = new Map();
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _base = __webpack_require__(0);
var _nativeTypeMap = __webpack_require__(1);
var _nativeTypeMap2 = _interopRequireDefault(_nativeTypeMap);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ArrayType = function (_BaseType) {
_inherits(ArrayType, _BaseType);
function ArrayType() {
_classCallCheck(this, ArrayType);
return _possibleConstructorReturn(this, (ArrayType.__proto__ || Object.getPrototypeOf(ArrayType)).apply(this, arguments));
}
_createClass(ArrayType, null, [{
key: 'isOfType',
value: function isOfType(val) {
return Array.isArray(val);
}
}, {
key: 'ofType',
value: function ofType() {
var coercedType = arguments.length === 1 ? (0, _base.coerce)(arguments.length <= 0 ? undefined : arguments[0]) : _base.unionOf.apply(undefined, arguments);
return function (_ref) {
_inherits(ArrayOfType, _ref);
function ArrayOfType() {
_classCallCheck(this, ArrayOfType);
return _possibleConstructorReturn(this, (ArrayOfType.__proto__ || Object.getPrototypeOf(ArrayOfType)).apply(this, arguments));
}
_createClass(ArrayOfType, null, [{
key: 'isOfType',
value: function isOfType(val) {
var _this3 = this;
return _get(ArrayOfType.__proto__ || Object.getPrototypeOf(ArrayOfType), 'isOfType', this).call(this, val) && val.every(function (itemVal) {
return _this3.type.isOfType(itemVal);
});
}
}, {
key: 'type',
get: function get() {
return coercedType;
}
}]);
return ArrayOfType;
}(this);
}
}, {
key: 'defaultValue',
get: function get() {
return [];
}
}]);
return ArrayType;
}(_base.BaseType);
exports.default = ArrayType;
_nativeTypeMap2.default.set(Array, ArrayType);
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _base = __webpack_require__(0);
var _nativeTypeMap = __webpack_require__(1);
var _nativeTypeMap2 = _interopRequireDefault(_nativeTypeMap);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var BooleanType = function (_BaseType) {
_inherits(BooleanType, _BaseType);
function BooleanType() {
_classCallCheck(this, BooleanType);
return _possibleConstructorReturn(this, (BooleanType.__proto__ || Object.getPrototypeOf(BooleanType)).apply(this, arguments));
}
_createClass(BooleanType, null, [{
key: 'isOfType',
value: function isOfType(val) {
return typeof val === 'boolean';
}
}, {
key: 'defaultValue',
get: function get() {
return false;
}
}]);
return BooleanType;
}(_base.BaseType);
exports.default = BooleanType;
_nativeTypeMap2.default.set(Boolean, BooleanType);
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _base = __webpack_require__(0);
var _nativeTypeMap = __webpack_require__(1);
var _nativeTypeMap2 = _interopRequireDefault(_nativeTypeMap);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var NumberType = function (_BaseType) {
_inherits(NumberType, _BaseType);
function NumberType() {
_classCallCheck(this, NumberType);
return _possibleConstructorReturn(this, (NumberType.__proto__ || Object.getPrototypeOf(NumberType)).apply(this, arguments));
}
_createClass(NumberType, null, [{
key: 'isOfType',
value: function isOfType(val) {
return typeof val === 'number';
}
}, {
key: 'defaultValue',
get: function get() {
return 0;
}
}]);
return NumberType;
}(_base.BaseType);
exports.default = NumberType;
_nativeTypeMap2.default.set(Number, NumberType);
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.strict = strict;
var _base = __webpack_require__(0);
var _nativeTypeMap = __webpack_require__(1);
var _nativeTypeMap2 = _interopRequireDefault(_nativeTypeMap);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ObjectType = function (_BaseType) {
_inherits(ObjectType, _BaseType);
function ObjectType() {
_classCallCheck(this, ObjectType);
return _possibleConstructorReturn(this, (ObjectType.__proto__ || Object.getPrototypeOf(ObjectType)).apply(this, arguments));
}
_createClass(ObjectType, null, [{
key: 'isOfType',
value: function isOfType(val) {
return (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && !Array.isArray(val) && val !== null;
}
}, {
key: 'withDefinition',
value: function withDefinition(definition) {
var coercedDefinition = Object.keys(definition).reduce(function (o, key) {
o[key] = (0, _base.coerce)(definition[key]);
return o;
}, {});
return function (_ref) {
_inherits(ObjectWithDefinition, _ref);
function ObjectWithDefinition() {
_classCallCheck(this, ObjectWithDefinition);
return _possibleConstructorReturn(this, (ObjectWithDefinition.__proto__ || Object.getPrototypeOf(ObjectWithDefinition)).apply(this, arguments));
}
_createClass(ObjectWithDefinition, null, [{
key: 'create',
value: function create() {
var _this3 = this;
var val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var base = this.defaultValue;
if ((typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object') {
Object.keys(val).forEach(function (key) {
if (_this3.properties.hasOwnProperty(key)) {
Object.assign(base, _defineProperty({}, key, _this3.properties[key].create(val[key])));
} else {
Object.assign(base, _defineProperty({}, key, val[key]));
}
});
}
return _get(ObjectWithDefinition.__proto__ || Object.getPrototypeOf(ObjectWithDefinition), 'create', this).call(this, base);
}
}, {
key: 'isOfType',
value: function isOfType(val) {
var _this4 = this;
return _get(ObjectWithDefinition.__proto__ || Object.getPrototypeOf(ObjectWithDefinition), 'isOfType', this).call(this, val) && Object.keys(definition).every(function (key) {
return _this4.properties[key].isOfType(val[key]);
});
}
}, {
key: 'properties',
get: function get() {
return coercedDefinition;
}
}, {
key: 'strict',
get: function get() {
return strict(this);
}
}, {
key: 'defaultValue',
get: function get() {
var _this5 = this;
var base = _get(ObjectWithDefinition.__proto__ || Object.getPrototypeOf(ObjectWithDefinition), 'defaultValue', this);
Object.keys(this.properties).forEach(function (key) {
// console.log(key, this.properties[key].defaultValue)
Object.assign(base, _defineProperty({}, key, _this5.properties[key].defaultValue));
});
return base;
}
}]);
return ObjectWithDefinition;
}(this);
}
}, {
key: 'keyValuePair',
value: function keyValuePair(valueType) {
var coercedValueType = (0, _base.coerce)(valueType);
return function (_ref2) {
_inherits(ObjectKeyValuePair, _ref2);
function ObjectKeyValuePair() {
_classCallCheck(this, ObjectKeyValuePair);
return _possibleConstructorReturn(this, (ObjectKeyValuePair.__proto__ || Object.getPrototypeOf(ObjectKeyValuePair)).apply(this, arguments));
}
_createClass(ObjectKeyValuePair, null, [{
key: 'isOfType',
value: function isOfType(val) {
var _this7 = this;
return _get(ObjectKeyValuePair.__proto__ || Object.getPrototypeOf(ObjectKeyValuePair), 'isOfType', this).call(this, val) && Object.keys(val).every(function (key) {
return _this7.valueType.isOfType(val[key]);
});
}
}, {
key: 'valueType',
get: function get() {
return coercedValueType;
}
}]);
return ObjectKeyValuePair;
}(this);
}
}, {
key: 'defaultValue',
get: function get() {
return {};
}
}]);
return ObjectType;
}(_base.BaseType);
exports.default = ObjectType;
function strict(type) {
var coercedType = (0, _base.coerce)(type);
if (!(coercedType.prototype instanceof ObjectType)) {
console.error('strict() is only intended for use with objects');
return type;
}
return function (_type) {
_inherits(ObjectWithDefinitionStrict, _type);
function ObjectWithDefinitionStrict() {
_classCallCheck(this, ObjectWithDefinitionStrict);
return _possibleConstructorReturn(this, (ObjectWithDefinitionStrict.__proto__ || Object.getPrototypeOf(ObjectWithDefinitionStrict)).apply(this, arguments));
}
_createClass(ObjectWithDefinitionStrict, null, [{
key: 'isOfType',
value: function isOfType(val) {
var _this9 = this;
return _get(ObjectWithDefinitionStrict.__proto__ || Object.getPrototypeOf(ObjectWithDefinitionStrict), 'isOfType', this).call(this, val) && Object.keys(val).every(function (key) {
return !!_this9.properties[key];
});
}
}, {
key: 'create',
value: function create() {
var _this10 = this;
var val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _get(ObjectWithDefinitionStrict.__proto__ || Object.getPrototypeOf(ObjectWithDefinitionStrict), 'create', this).call(this, Object.keys(val).reduce(function (o, key) {
if (_this10.properties.hasOwnProperty(key)) {
o[key] = val[key];
}
return o;
}, {}));
}
}]);
return ObjectWithDefinitionStrict;
}(type);
}
_nativeTypeMap2.default.set(Object, ObjectType);
/***/ }),
/* 6 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _base = __webpack_require__(0);
var _nativeTypeMap = __webpack_require__(1);
var _nativeTypeMap2 = _interopRequireDefault(_nativeTypeMap);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var StringType = function (_BaseType) {
_inherits(StringType, _BaseType);
function StringType() {
_classCallCheck(this, StringType);
return _possibleConstructorReturn(this, (StringType.__proto__ || Object.getPrototypeOf(StringType)).apply(this, arguments));
}
_createClass(StringType, null, [{
key: 'isOfType',
value: function isOfType(val) {
return typeof val === 'string';
}
}, {
key: 'defaultValue',
get: function get() {
return "";
}
}]);
return StringType;
}(_base.BaseType);
exports.default = StringType;
_nativeTypeMap2.default.set(String, StringType);
/***/ }),
/* 7 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.types = exports.defaultValue = exports.strict = exports.optional = exports.unionOf = exports.defineType = undefined;
var _base = __webpack_require__(0);
var _Array = __webpack_require__(2);
var _Array2 = _interopRequireDefault(_Array);
var _Boolean = __webpack_require__(3);
var _Boolean2 = _interopRequireDefault(_Boolean);
var _Number = __webpack_require__(4);
var _Number2 = _interopRequireDefault(_Number);
var _Object = __webpack_require__(5);
var _Object2 = _interopRequireDefault(_Object);
var _String = __webpack_require__(6);
var _String2 = _interopRequireDefault(_String);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var types = {
Base: _base.BaseType,
Union: _base.UnionType,
Any: _base.AnyType,
Object: _Object2.default,
Array: _Array2.default,
String: _String2.default,
Number: _Number2.default,
Boolean: _Boolean2.default
};
exports.defineType = _base.coerce;
exports.unionOf = _base.unionOf;
exports.optional = _base.optional;
exports.strict = _Object.strict;
exports.defaultValue = _base.defaultValue;
exports.types = types;
/***/ })
/******/ ]);
});