archetype-js
Advanced tools
Comparing version 0.4.2 to 0.4.3
{ | ||
"name": "archetype-js", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"author": "Valeri Karpov <val@boosterfuels.com>", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
@@ -48,1 +48,8 @@ # archetype | ||
Archetypes are composable, inspectable, and extendable via `extends`. | ||
The returned function can be called without "new" keyword | ||
```javascript | ||
const Person = new Archetype({name: 'string'}).compile('Person'); | ||
const p = Person({name: 'test'}); | ||
``` |
@@ -16,2 +16,5 @@ 'use strict'; | ||
const type = function(obj, projection) { | ||
if (!(this instanceof type)) { | ||
return new type(obj, projection); | ||
} | ||
Object.assign(this, unmarshal(_.cloneDeep(obj), _this, projection)); | ||
@@ -18,0 +21,0 @@ }; |
@@ -14,3 +14,3 @@ 'use strict'; | ||
} | ||
return res | ||
return res; | ||
}, | ||
@@ -47,4 +47,8 @@ string: v => { | ||
if (v == null) { | ||
return | ||
return; | ||
} | ||
if (type === 'number' && Number.isNaN(v)) { | ||
return CAST_PRIMITIVES[type](v); | ||
} | ||
if (typeof v === type) { | ||
@@ -51,0 +55,0 @@ return v; |
@@ -538,2 +538,9 @@ 'use strict'; | ||
it('compiled function can be called without "new" keyword', function() { | ||
const Person = new Archetype({ | ||
name: 'string' | ||
}).compile('Person'); | ||
assert.ok(Person({}) instanceof Person); | ||
}); | ||
it('validation with arrays and nested objects', function() { | ||
@@ -601,2 +608,9 @@ const Band = new Archetype({ | ||
}); | ||
it('handles NaN', function() { | ||
const Test = new Archetype({ num: 'number' }).compile('Test'); | ||
assert.throws(function() { | ||
new Test({ num: 'a' * 2 }); | ||
}, /to number/); | ||
}); | ||
}); | ||
@@ -603,0 +617,0 @@ |
47321
1147
55