From afe783fecdde29ea5f7490077b8ef4b0646e6b8c Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Tue, 29 Mar 2016 10:56:53 -0400 Subject: [PATCH] rebuild --- README.md | 19 +++++----- lib/array.js | 4 +-- lib/date.js | 4 +-- lib/index.js | 6 +++- lib/mixed.js | 45 +++++++++++++++++------- lib/number.js | 4 +-- lib/object.js | 48 +++++++++++++++++-------- lib/string.js | 4 +-- lib/util/condition.js | 58 ++++++++++++++++++------------ lib/util/createValidation.js | 68 +++++++++++++++++++++++++----------- lib/util/reference.js | 54 ++++++++++++++++++++++------ lib/util/set.js | 17 +++++---- 12 files changed, 224 insertions(+), 107 deletions(-) diff --git a/README.md b/README.md index 0927e45a9..da2fc46d6 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ json separate from validating it, via the `cast` method. - [`.reach(Schema schema, String path, Object options)`](#reachschema-schema-string-path-object-options) - [`.addMethod(schemaType, name, method)`](#addmethodschematype-name-method) - [`ValidationError(String|Array errors, Any value, String path)`](#validationerrorstringarraystring-errors-any-value-string-path) + - [`ref(String path, Object options)`](#refstring-path-object-options) - [mixed](#mixed) - [`mixed.clone()`](#mixedclone) - [`mixed.label(String label)`](#mixedlabelstring-label) @@ -39,14 +40,14 @@ json separate from validating it, via the `cast` method. - [`mixed.typeError(String message)`](#mixedtypeerrorstring-message) - [`mixed.oneOf(Array arrayOfValues, [String message])` Alias: `equals`](#mixedoneofarrayany-arrayofvalues-string-message-alias-equals) - [`mixed.notOneOf(Array arrayOfValues, [String message])`](#mixednotoneofarrayany-arrayofvalues-string-message) - - [`mixed.when(String key, Object options | Function func)`](#mixedwhenstring-key-object-options--function-func) + - [`mixed.when(String|Array keys, Object options | Function func)`](#mixedwhenstringarraystring-keys-object-options--function-func) - [`mixed.test(String name, String message, Function fn, [Bool callbackStyleAsync])`](#mixedteststring-name-string-message-function-fn-bool-callbackstyleasync) - [`mixed.test(Object options)`](#mixedtestobject-options) - [`mixed.transform(Function fn)`](#mixedtransformfunction-fn) - [string](#string) - [`string.required([String message])`](#stringrequiredstring-message) - - [`string.min(Number limit, [String message])`](#stringminnumber-limit-string-message) - - [`string.max(Number limit, [String message])`](#stringmaxnumber-limit-string-message) + - [`string.min(Number|Ref limit, [String message])`](#stringminnumberref-limit-string-message) + - [`string.max(Number|Ref limit, [String message])`](#stringmaxnumberref-limit-string-message) - [`string.matches(Regex regex, [String message])`](#stringmatchesregex-regex-string-message) - [`string.email([String message])`](#stringemailstring-message) - [`string.url([String message])`](#stringurlstring-message) @@ -54,21 +55,21 @@ json separate from validating it, via the `cast` method. - [`string.lowercase([String message])`](#stringlowercasestring-message) - [`string.uppercase([String message])`](#stringuppercasestring-message) - [number](#number) - - [`number.min(Number limit, [String message])`](#numberminnumber-limit-string-message) - - [`number.max(Number limit, [String message])`](#numbermaxnumber-limit-string-message) + - [`number.min(Number|Ref limit, [String message])`](#numberminnumberref-limit-string-message) + - [`number.max(Number|Ref limit, [String message])`](#numbermaxnumberref-limit-string-message) - [`number.positive([String message])`](#numberpositivestring-message) - [`number.negative([String message])`](#numbernegativestring-message) - [`number.integer([String message])`](#numberintegerstring-message) - [`round(String type)` - 'floor', 'ceil', 'round'](#roundstring-type---floor-ceil-round) - [boolean](#boolean) - [date](#date) - - [`date.min(Date|String limit, [String message])`](#datemindatestring-limit-string-message) - - [`date.max(Date|String limit, [String message])`](#datemaxdatestring-limit-string-message) + - [`date.min(Date|String|Ref limit, [String message])`](#datemindatestringref-limit-string-message) + - [`date.max(Date|String|Ref limit, [String message])`](#datemaxdatestringref-limit-string-message) - [array](#array) - [`array.of(Schema type)`](#arrayofschema-type) - [`array.required([String message])`](#arrayrequiredstring-message) - - [`array.min(Number limit, [String message])`](#arrayminnumber-limit-string-message) - - [`array.max(Number limit, [String message])`](#arraymaxnumber-limit-string-message) + - [`array.min(Number|Ref limit, [String message])`](#arrayminnumberref-limit-string-message) + - [`array.max(Number|Ref limit, [String message])`](#arraymaxnumberref-limit-string-message) - [`array.compact(Function rejector)`](#arraycompactfunction-rejector) - [object](#object) - [`object.shape(Object schemaHash, [noSortEdges])`](#objectshapeobject-schemahash-nosortedges) diff --git a/lib/array.js b/lib/array.js index 29bfcdcfa..4f351e3fd 100644 --- a/lib/array.js +++ b/lib/array.js @@ -128,7 +128,7 @@ inherits(ArraySchema, MixedSchema, { exclusive: true, params: { min: _min }, test: function test(value) { - return isAbsent(value) || value.length >= _min; + return isAbsent(value) || value.length >= this.resolve(_min); } }); }, @@ -141,7 +141,7 @@ inherits(ArraySchema, MixedSchema, { exclusive: true, params: { max: _max }, test: function test(value) { - return isAbsent(value) || value.length <= _max; + return isAbsent(value) || value.length <= this.resolve(_max); } }); }, diff --git a/lib/date.js b/lib/date.js index 80c161101..e94f63a91 100644 --- a/lib/date.js +++ b/lib/date.js @@ -47,7 +47,7 @@ inherits(DateSchema, MixedSchema, { message: msg || locale.min, params: { min: _min }, test: function test(value) { - return isAbsent(value) || value >= limit; + return isAbsent(value) || value >= this.resolve(limit); } }); }, @@ -63,7 +63,7 @@ inherits(DateSchema, MixedSchema, { message: msg || locale.max, params: { max: _max }, test: function test(value) { - return isAbsent(value) || value <= limit; + return isAbsent(value) || value <= this.resolve(limit); } }); } diff --git a/lib/index.js b/lib/index.js index 9f7697365..ea0362933 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,7 @@ 'use strict'; var mixed = require('./mixed'), - bool = require('./boolean'); + bool = require('./boolean'), + Ref = require('./util/reference'); var isSchema = function isSchema(schema) { return schema && !!schema.__isYupSchema__; @@ -19,6 +20,9 @@ module.exports = { reach: require('./util/reach'), ValidationError: require('./util/validation-error'), + ref: function ref(key, options) { + return new Ref(key, options); + }, isSchema: isSchema, diff --git a/lib/mixed.js b/lib/mixed.js index 870049efb..25e0f497d 100644 --- a/lib/mixed.js +++ b/lib/mixed.js @@ -2,13 +2,13 @@ var Promise = require('promise/lib/es6-extensions'), Condition = require('./util/condition'), - ValidationError = require('./util/validation-error'), locale = require('./locale.js').mixed, _ = require('./util/_'), isAbsent = require('./util/isAbsent'), cloneDeep = require('./util/clone'), createValidation = require('./util/createValidation'), - BadSet = require('./util/set'); + BadSet = require('./util/set'), + Ref = require('./util/reference'); var notEmpty = function notEmpty(value) { return !isAbsent(value); @@ -28,6 +28,7 @@ function SchemaType() { if (!(this instanceof SchemaType)) return new SchemaType(); this._deps = []; + this._conditions = []; this._options = { abortEarly: true, recursive: true }; this._exclusive = Object.create(null); this._whitelist = new BadSet(); @@ -98,9 +99,11 @@ SchemaType.prototype = { return !this._typeCheck || this._typeCheck(v); }, - cast: function cast(_value, _opts) { - var schema = this._resolve((_opts || {}).context); - return schema._cast(_value, _opts); + cast: function cast(value) { + var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; + + var schema = this._resolve(opts.context, opts.parent); + return schema._cast(value, opts); }, _cast: function _cast(_value) { @@ -116,8 +119,8 @@ SchemaType.prototype = { }, _resolve: function _resolve(context, parent) { - if (this._deps.length) { - return this._deps.reduce(function (schema, match) { + if (this._conditions.length) { + return this._conditions.reduce(function (schema, match) { return match.resolve(schema, match.getValue(parent, context)); }, this); } @@ -262,11 +265,17 @@ SchemaType.prototype = { return next; }, - when: function when(key, options) { + when: function when(keys, options) { var next = this.clone(), - dep = new Condition(key, next._type, options); + deps = [].concat(keys).map(function (key) { + return new Ref(key); + }); + + deps.forEach(function (dep) { + if (!dep.isContext) next._deps.push(dep.key); + }); - next._deps.push(dep); + next._conditions.push(new Condition(deps, options)); return next; }, @@ -279,7 +288,9 @@ SchemaType.prototype = { name: 'typeError', test: function test(value) { if (value !== undefined && !this.schema.isType(value)) return this.createError({ - params: { type: this.schema._type } + params: { + type: this.schema._type + } }); return true; } @@ -304,7 +315,11 @@ SchemaType.prototype = { name: 'oneOf', test: function test(value) { var valids = this.schema._whitelist; - if (valids.length && !(valids.has(value) || isAbsent(value))) return this.createError({ params: { values: valids.values().join(', ') } }); + if (valids.length && !(value === undefined || valids.has(value))) return this.createError({ + params: { + values: valids.values().join(', ') + } + }); return true; } }); @@ -327,7 +342,11 @@ SchemaType.prototype = { name: 'notOneOf', test: function test(value) { var invalids = this.schema._blacklist; - if (invalids.length && invalids.has(value)) return this.createError({ params: { values: invalids.values().join(', ') } }); + if (invalids.length && invalids.has(value)) return this.createError({ + params: { + values: invalids.values().join(', ') + } + }); return true; } }); diff --git a/lib/number.js b/lib/number.js index 73d80feb0..1205cb4ab 100644 --- a/lib/number.js +++ b/lib/number.js @@ -47,7 +47,7 @@ inherits(NumberSchema, SchemaObject, { params: { min: _min }, message: msg || locale.min, test: function test(value) { - return isAbsent(value) || value >= _min; + return isAbsent(value) || value >= this.resolve(_min); } }); }, @@ -59,7 +59,7 @@ inherits(NumberSchema, SchemaObject, { params: { max: _max }, message: msg || locale.max, test: function test(value) { - return isAbsent(value) || value <= _max; + return isAbsent(value) || value <= this.resolve(_max); } }); }, diff --git a/lib/object.js b/lib/object.js index 0bb7d3066..9cf62253e 100644 --- a/lib/object.js +++ b/lib/object.js @@ -7,6 +7,7 @@ var Promise = require('promise/lib/es6-extensions'); var toposort = require('toposort'); var locale = require('./locale.js').object; var split = require('property-expr').split; +var Ref = require('./util/reference'); var c = require('case'); var _require = require('./util/_'); @@ -16,6 +17,7 @@ var transform = _require.transform; var assign = _require.assign; var inherits = _require.inherits; var collectErrors = _require.collectErrors; +var isSchema = _require.isSchema; var has = _require.has; var isRecursive = function isRecursive(schema) { @@ -30,7 +32,9 @@ c.type('altCamel', function (str) { }); var childSchema = function childSchema(field, parent) { - return isRecursive(field) ? field.of ? field.of(parent) : parent : field; + if (!isRecursive(field)) return field; + + return field.of ? field.of(parent) : parent; }; var scopeError = function scopeError(value) { @@ -85,7 +89,9 @@ inherits(ObjectSchema, MixedSchema, { return isObject(value) || typeof value === 'function'; }, - _cast: function _cast(_value, _opts) { + _cast: function _cast(_value) { + var _opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; + var schema = this, value = MixedSchema.prototype._cast.call(schema, _value); @@ -100,23 +106,33 @@ inherits(ObjectSchema, MixedSchema, { props = schema._nodes.concat(extra); schema.withMutation(function () { - var innerOptions = _extends({}, _opts, { context: {} }); + var innerOptions = _extends({}, _opts, { parent: {} }); value = transform(props, function (obj, prop) { + var field = fields[prop]; var exists = has(value, prop); - if (exists && fields[prop]) { - var fieldSchema = childSchema(fields[prop], schema['default'](undefined)); + if (Ref.isRef(field)) { + var refValue = field.getValue(obj, innerOptions.context); + + if (refValue !== undefined) obj[prop] = refValue; + } else if (exists && field) { + // ugly optimization avoiding a clone. clears default for recursive + // cast and resets it below; + var hasDflt = has(schema, '_default'), + dflt = schema._default; + + var fieldSchema = childSchema(field, schema['default'](undefined)); obj[prop] = fieldSchema.cast(value[prop], innerOptions); - } else if (exists && !strip) obj[prop] = value[prop];else if (fields[prop]) { - var fieldDefault = fields[prop]['default'] ? fields[prop]['default']() : undefined; + + if (hasDflt) schema['default'](dflt);else delete schema._default; + } else if (exists && !strip) obj[prop] = value[prop];else if (field) { + var fieldDefault = field['default'] ? field['default']() : undefined; if (fieldDefault !== undefined) obj[prop] = fieldDefault; } - }, innerOptions.context); - - delete schema._default; + }, innerOptions.parent); }); return value; @@ -257,18 +273,20 @@ function sortFields(fields) { nodes = []; for (var key in fields) if (has(fields, key)) { + var value = fields[key]; + if (! ~nodes.indexOf(key)) nodes.push(key); - fields[key]._deps && fields[key]._deps.forEach(function (dep) { + var addNode = function addNode(depPath) { //eslint-disable-line no-loop-func - if (dep.isContext) return; - - var node = split(dep.key)[0]; + var node = split(depPath)[0]; if (! ~nodes.indexOf(node)) nodes.push(node); if (! ~excludes.indexOf(key + '-' + node)) edges.push([key, node]); - }); + }; + + if (Ref.isRef(value) && !value.isContext) addNode(value.path);else if (isSchema(value)) value._deps.forEach(addNode); } return toposort.array(nodes, edges).reverse(); diff --git a/lib/string.js b/lib/string.js index b12d71878..885ac2d30 100644 --- a/lib/string.js +++ b/lib/string.js @@ -54,7 +54,7 @@ inherits(StringSchema, MixedSchema, { message: msg || locale.min, params: { min: _min }, test: function test(value) { - return isAbsent(value) || value.length >= _min; + return isAbsent(value) || value.length >= this.resolve(_min); } }); }, @@ -66,7 +66,7 @@ inherits(StringSchema, MixedSchema, { message: msg || locale.max, params: { max: _max }, test: function test(value) { - return isAbsent(value) || value.length <= _max; + return isAbsent(value) || value.length <= this.resolve(_max); } }); }, diff --git a/lib/util/condition.js b/lib/util/condition.js index eb4ae7e74..f0402a916 100644 --- a/lib/util/condition.js +++ b/lib/util/condition.js @@ -4,50 +4,62 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons var _require = require('./_'); +var transform = _require.transform; var has = _require.has; var isSchema = _require.isSchema; -var getter = require('property-expr').getter; module.exports = Conditional; var Conditional = (function () { - function Conditional(key, type, options) { + function Conditional(refs, options) { + var _this = this; + _classCallCheck(this, Conditional); var is = options.is; var then = options.then; var otherwise = options.otherwise; - var prefix = options.contextPrefix || '$'; - this.prefix = prefix; - this.key = key; - this.isContext = key.indexOf(prefix) === 0; + this.refs = [].concat(refs); if (typeof options === 'function') this.fn = options;else { - if (!has(options, 'is')) throw new TypeError('`is:` is required for `when()` conditions'); - - if (!options.then && !options.otherwise) throw new TypeError('either `then:` or `otherwise:` is required for `when()` conditions'); - - is = typeof is === 'function' ? is : (function (is, value) { - return is === value; - }).bind(null, is); - - this.fn = function (value, ctx) { - return is(value) ? ctx.concat(then) : ctx.concat(otherwise); - }; + (function () { + if (!has(options, 'is')) throw new TypeError('`is:` is required for `when()` conditions'); + + if (!options.then && !options.otherwise) throw new TypeError('either `then:` or `otherwise:` is required for `when()` conditions'); + + var isFn = typeof is === 'function' ? is : function () { + for (var _len = arguments.length, values = Array(_len), _key = 0; _key < _len; _key++) { + values[_key] = arguments[_key]; + } + + return values.every(function (value) { + return value === is; + }); + }; + + _this.fn = function () { + for (var _len2 = arguments.length, values = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { + values[_key2] = arguments[_key2]; + } + + var ctx = values.pop(); + return isFn.apply(undefined, values) ? ctx.concat(then) : ctx.concat(otherwise); + }; + })(); } } Conditional.prototype.getValue = function getValue(parent, context) { - var path = this.isContext ? this.key.slice(this.prefix.length) : this.key; - - if (this.isContext && !context || !this.isContext && !context && !parent) throw new Error('missing the context necessary to cast this value'); + var values = this.refs.map(function (r) { + return r.getValue(parent, context); + }); - return getter(path)(this.isContext ? context : parent || context); + return values; }; - Conditional.prototype.resolve = function resolve(ctx, value) { - var schema = this.fn.call(ctx, value, ctx); + Conditional.prototype.resolve = function resolve(ctx, values) { + var schema = this.fn.apply(ctx, values.concat(ctx)); if (schema !== undefined && !isSchema(schema)) throw new TypeError('conditions must return a schema object'); diff --git a/lib/util/createValidation.js b/lib/util/createValidation.js index fd8f433ab..4caf4fe06 100644 --- a/lib/util/createValidation.js +++ b/lib/util/createValidation.js @@ -4,24 +4,44 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } -var Promise = require('promise/lib/es6-extensions'), - ValidationError = require('./validation-error'); +var Promise = require('promise/lib/es6-extensions'); +var ValidationError = require('./validation-error'); +var Ref = require('./reference'); + +var _require = require('./_'); + +var transform = _require.transform; var formatError = ValidationError.formatError; -function createErrorFactory(orginalMessage, orginalPath, value, orginalParams, originalType, label) { +function resolveParams(oldParams, newParams, resolve) { + var start = _extends({}, oldParams, newParams); + return transform(start, function (obj, value, key) { + obj[key] = resolve(value); + }); +} + +function createErrorFactory(_ref) { + var value = _ref.value; + var label = _ref.label; + var resolve = _ref.resolve; + + var opts = _objectWithoutProperties(_ref, ['value', 'label', 'resolve']); + return function createError() { - var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + var _ref2 = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + var _ref2$path = _ref2.path; + var path = _ref2$path === undefined ? opts.path : _ref2$path; + var _ref2$message = _ref2.message; + var message = _ref2$message === undefined ? opts.message : _ref2$message; + var _ref2$type = _ref2.type; + var type = _ref2$type === undefined ? opts.name : _ref2$type; + var params = _ref2.params; - var _ref$path = _ref.path; - var path = _ref$path === undefined ? orginalPath : _ref$path; - var _ref$message = _ref.message; - var message = _ref$message === undefined ? orginalMessage : _ref$message; - var _ref$type = _ref.type; - var type = _ref$type === undefined ? originalType : _ref$type; - var params = _ref.params; + params = resolveParams(opts.params, params, resolve); - return new ValidationError(formatError(message, _extends({ path: path, value: value, label: label }, orginalParams, params)), value, path, type); + return new ValidationError(formatError(message, _extends({ path: path, value: value, label: label }, params)), value, path, type); }; } @@ -32,16 +52,24 @@ module.exports = function createValidation(options) { var params = options.params; var useCallback = options.useCallback; - function validate(_ref2) { - var value = _ref2.value; - var path = _ref2.path; - var label = _ref2.label; - var parent = _ref2.state.parent; + function validate(_ref3) { + var value = _ref3.value; + var path = _ref3.path; + var label = _ref3.label; + var parent = _ref3.state.parent; + + var rest = _objectWithoutProperties(_ref3, ['value', 'path', 'label', 'state']); - var rest = _objectWithoutProperties(_ref2, ['value', 'path', 'label', 'state']); + var resolve = function resolve(value) { + return Ref.isRef(value) ? value.getValue(parent, rest.options.context) : value; + }; + + var createError = createErrorFactory({ + message: message, path: path, value: value, params: params, + label: label, resolve: resolve, name: name + }); - var createError = createErrorFactory(message, path, value, params, name, label); - var ctx = _extends({ path: path, parent: parent, createError: createError, type: name }, rest); + var ctx = _extends({ path: path, parent: parent, type: name, createError: createError, resolve: resolve }, rest); return new Promise(function (resolve, reject) { !useCallback ? resolve(test.call(ctx, value)) : test.call(ctx, value, function (err, valid) { diff --git a/lib/util/reference.js b/lib/util/reference.js index f2f88b755..6c346ef82 100644 --- a/lib/util/reference.js +++ b/lib/util/reference.js @@ -1,19 +1,51 @@ -"use strict"; +'use strict'; -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +exports.__esModule = true; -var Reference = (function () { - function Reference(string) { - _classCallCheck(this, Reference); +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } - this._deps = []; +var getter = require('property-expr').getter; + +var validateName = function validateName(d) { + if (typeof d !== 'string') throw new TypeError('ref\'s must be strings, got: ' + d); +}; + +var Ref = (function () { + Ref.isRef = function isRef(value) { + return !!(value && (value.__isYupRef || value instanceof Ref)); + }; + + function Ref(key, mapFn) { + var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; + + _classCallCheck(this, Ref); + + validateName(key); + var prefix = options.contextPrefix || '$'; + + this.key = key; + this.prefix = prefix; + this.isContext = key.indexOf(prefix) === 0; + this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key; + this.map = mapFn || function (value) { + return value; + }; } - Reference.prototype["default"] = function _default() {}; + Ref.prototype.getValue = function getValue(parent, context) { + var isContext = this.isContext; + + if (isContext && !context || !isContext && !context && !parent) throw new Error('missing the context necessary to cast this value'); + + var value = getter(this.path)(isContext ? context : parent || context); - Reference.prototype.cast = function cast(value, parent, options) { - return parent["default"](undefined).cast(value, options); + return this.map(value); }; - return Reference; -})(); \ No newline at end of file + return Ref; +})(); + +exports['default'] = Ref; + +Ref.prototype.__isYupRef = true; +module.exports = exports['default']; \ No newline at end of file diff --git a/lib/util/set.js b/lib/util/set.js index ab7c67587..ea05c8591 100644 --- a/lib/util/set.js +++ b/lib/util/set.js @@ -1,16 +1,19 @@ -"use strict"; +'use strict'; -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 _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; }; })(); -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } -var hasOwnProperty = Object.prototype.hasOwnProperty; +var _require = require('./_'); + +var _has = _require.has; module.exports = (function () { function BadSet() { _classCallCheck(this, BadSet); this._map = Object.create(null); + this._refs = Object.create(null); } BadSet.prototype.values = function values() { @@ -25,16 +28,16 @@ module.exports = (function () { this._map[stringify(item)] = item; }; - BadSet.prototype["delete"] = function _delete(item) { + BadSet.prototype['delete'] = function _delete(item) { delete this._map[stringify(item)]; }; BadSet.prototype.has = function has(item) { - return hasOwnProperty.call(this._map, stringify(item)); + return _has(this._map, stringify(item)); }; _createClass(BadSet, [{ - key: "length", + key: 'length', get: function get() { return Object.keys(this._map).length; }