value-object
Advanced tools
Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "value-object", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "value-object.js - simple value objects", | ||
@@ -5,0 +5,0 @@ "main": "value-object.js", |
@@ -1156,2 +1156,12 @@ /* eslint-env mocha */ | ||
it('calls the original constructor', () => { | ||
class X extends ValueObject.define({ a: 'string' }) { | ||
constructor({ a }) { | ||
super({ a: a + '!' }) | ||
} | ||
} | ||
const x = new X({ a: 'G' }).with({ a: 'H' }) | ||
assert.equal(x.a, 'H!') | ||
}) | ||
it('returns instances with schemas', () => { | ||
@@ -1158,0 +1168,0 @@ class Yo extends ValueObject.define({ x: 'string' }) {} |
@@ -59,31 +59,3 @@ function ValueObject() { | ||
var Constructor = this.constructor | ||
if (!Constructor.With) { | ||
Constructor.With = function With() {} | ||
Constructor.With.prototype = Object.create(Constructor.prototype) | ||
Constructor.With.prototype.constructor = Constructor | ||
} | ||
var instance = new Constructor.With() | ||
for (var propertyName in Constructor.schema.propertyTypes) { | ||
instance[propertyName] = this[propertyName] | ||
} | ||
for (var newPropertyName in newPropertyValues) { | ||
if (Object.prototype.hasOwnProperty.call(newPropertyValues, newPropertyName)) { | ||
var property = Constructor.schema.propertyTypes[newPropertyName] | ||
if (!property) { | ||
new Constructor(extend(this, newPropertyValues)) | ||
} | ||
var coercionResult = property.coerce(newPropertyValues[newPropertyName]) | ||
if (coercionResult.failure) { | ||
new Constructor(extend(this, newPropertyValues)) | ||
} else { | ||
instance[newPropertyName] = coercionResult.value | ||
} | ||
} | ||
} | ||
if (typeof instance._init === 'function') { | ||
instance._init() | ||
} | ||
freeze(instance) | ||
return instance | ||
return new Constructor(extend(this.toPlainObject(), newPropertyValues)) | ||
} | ||
@@ -613,15 +585,18 @@ ValueObject.prototype.toJSON = function() { | ||
function extend(extendee, extender) { | ||
var extended = {}, | ||
prop | ||
for (prop in extendee) { | ||
if (Object.prototype.hasOwnProperty.call(extendee, prop)) { | ||
extended[prop] = extendee[prop] | ||
var extended = {} | ||
withOwnProperties(extendee, function(prop) { | ||
extended[prop] = extendee[prop] | ||
}) | ||
withOwnProperties(extender, function(prop) { | ||
extended[prop] = extender[prop] | ||
}) | ||
return extended | ||
} | ||
function withOwnProperties(subject, fn) { | ||
for (var prop in subject) { | ||
if (Object.prototype.hasOwnProperty.call(subject, prop)) { | ||
fn(prop) | ||
} | ||
} | ||
for (prop in extender) { | ||
if (Object.prototype.hasOwnProperty.call(extender, prop)) { | ||
extended[prop] = extender[prop] | ||
} | ||
} | ||
return extended | ||
} | ||
@@ -628,0 +603,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 11 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1864780
824
3610
602