Comparing version 0.2.4 to 0.2.5
@@ -37,3 +37,3 @@ class Codec { | ||
_isClass(object) { | ||
return !!object.prototype && !!object.prototype.constructor.name; | ||
return ![Object, Boolean, String, Number, Date].includes(object) && !!object.prototype && !!object.prototype.constructor.name; | ||
} | ||
@@ -40,0 +40,0 @@ _props(Type) { |
@@ -35,3 +35,3 @@ const Codec = require('./Codec'); | ||
if (Array.isArray(Type)) { | ||
return this._encodeArrayValue(value, Type, transform); | ||
return this.encodeArrayValue(value, Type, transform); | ||
} | ||
@@ -62,3 +62,3 @@ | ||
} | ||
_encodeArrayValue(value, Type, transform) { | ||
encodeArrayValue(value, Type, transform) { | ||
value = value || []; | ||
@@ -65,0 +65,0 @@ return Promise.map(value, item => { |
@@ -60,3 +60,3 @@ class Domain { | ||
_isClass(object) { | ||
return !!object.prototype && !!object.prototype.constructor.name; | ||
return ![Object, Boolean, String, Number, Date].includes(object) && !!object.prototype && !!object.prototype.constructor.name; | ||
} | ||
@@ -63,0 +63,0 @@ static match(Clazz) { |
@@ -82,3 +82,3 @@ class Masker { | ||
if (this._isNotLiteral(Type) && this._isClass(Type)) { | ||
if (this._isClass(Type)) { | ||
const props = this._props(Type); | ||
@@ -115,3 +115,4 @@ const touched = this._settleObject(destination[prop], source[prop], props, this._isLayered(layer, prop)); | ||
_settleArrayValue(destination, source, prop, Type, layer) { | ||
const destinationItems = destination[prop] || []; | ||
destination[prop] = destination[prop] || []; | ||
const destinationItems = destination[prop]; | ||
const sourceItems = source[prop] || []; | ||
@@ -122,8 +123,2 @@ | ||
if (isLayered && sourceItems.length > destinationItems.length) { | ||
for (let index = destinationItems.length; index < sourceItems.length; index++) { | ||
destinationItems[index] = {}; // allocate missings, so array items can be updated by reference | ||
} | ||
} | ||
const settled = {touched: []}; | ||
@@ -139,3 +134,6 @@ let untouched = true; | ||
for (let index = 0; index < length; index++) { | ||
const settledItem = this._settleValue({[prop]: destinationItems[index]}, {[prop]: sourceItems[index]}, prop, Type[0], layer); | ||
const holder = {[prop]: destinationItems[index]}; | ||
const settledItem = this._settleValue(holder, {[prop]: sourceItems[index]}, prop, Type[0], layer); | ||
destinationItems[index] = holder[prop]; | ||
if (settledItem.hasOwnProperty('value')) { | ||
@@ -164,3 +162,3 @@ if (!settled.value) { | ||
_isClass(object) { | ||
return !!object.prototype && !!object.prototype.constructor.name; | ||
return ![Object, Boolean, String, Number, Date].includes(object) && !!object.prototype && !!object.prototype.constructor.name; | ||
} | ||
@@ -167,0 +165,0 @@ _props(Type) { |
{ | ||
"name": "carnaval", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
@@ -162,38 +162,2 @@ const test = require('ava'); | ||
// class UnreferencedBoxesWithin extends Domain { | ||
// get props() { | ||
// return { | ||
// size: Number, | ||
// things: [{ | ||
// details: [String] | ||
// }] | ||
// }; | ||
// } | ||
// } | ||
// test('decode deeply and deeply through untyped mapping', t => { | ||
// const mapping = Mapping.map(UnreferencedBoxesWithin); | ||
// const json = {size: 40, things: [{details: ['Shoes', 'Small']}, {details: ['Shirt', 'Large']}]}; | ||
// return mapping.decode(json).then(box => { | ||
// t.true(box instanceof UnreferencedBoxesWithin); | ||
// t.is(box.size, json.size); | ||
// t.true(box.things instanceof Array); | ||
// t.is(box.things[0].details[0], json.things[0].details[0]); | ||
// t.is(box.things[1].details[1], json.things[1].details[1]); | ||
// }); | ||
// }); | ||
// test('encode deeply and deeply through untyped mapping', t => { | ||
// const mapping = Mapping.map(UnreferencedBoxesWithin); | ||
// const box = new UnreferencedBoxes({size: 40, things: [{details: ['Shoes', 'Small']}, {details: ['Shirt', 'Large']}]}); | ||
// return mapping.encode(box).then(json => { | ||
// t.is(json.size, box.size); | ||
// t.true(json.things instanceof Array); | ||
// t.is(json.things[0].details[0], box.things[0].details[0]); | ||
// t.is(json.things[1].details[1], box.things[1].details[1]); | ||
// }); | ||
// }); | ||
class UnknownBox extends Domain { | ||
@@ -731,2 +695,44 @@ get props() { | ||
class UnreferencedBoxesWithin extends Domain { | ||
get props() { | ||
return { | ||
size: Number, | ||
things: [{ | ||
details: [{ | ||
name: String | ||
}] | ||
}] | ||
}; | ||
} | ||
} | ||
test('decode deeply and deeply through untyped mapping', t => { | ||
const mapping = Mapping.map(UnreferencedBoxesWithin).with({ | ||
things: [{details: [{name: {alias: 'fullname'}}]}] | ||
}); | ||
const json = {size: 40, things: [{details: [{fullname: 'Shoes'}, {fullname: 'Small'}]}, {details: [{fullname: 'Shirt'}, {fullname: 'Large'}]}]}; | ||
return mapping.decode(json).then(box => { | ||
t.true(box instanceof UnreferencedBoxesWithin); | ||
t.is(box.size, json.size); | ||
t.true(box.things instanceof Array); | ||
t.is(box.things[0].details[0].name, json.things[0].details[0].fullname); | ||
t.is(box.things[1].details[1].name, json.things[1].details[1].fullname); | ||
}); | ||
}); | ||
test('encode deeply and deeply through untyped mapping', t => { | ||
const mapping = Mapping.map(UnreferencedBoxesWithin).with({ | ||
things: [{details: [{name: {alias: 'fullname'}}]}] | ||
}); | ||
const box = new UnreferencedBoxesWithin({size: 40, things: [{details: [{name: 'Shoes'}, {name: 'Small'}]}, {details: [{name: 'Shirt'}, {name: 'Large'}]}]}); | ||
return mapping.encode(box).then(json => { | ||
t.is(json.size, box.size); | ||
t.true(json.things instanceof Array); | ||
t.is(json.things[0].details[0].fullname, box.things[0].details[0].name); | ||
t.is(json.things[1].details[1].fullname, box.things[1].details[1].name); | ||
}); | ||
}); | ||
test('decode through mapping & hook', t => { | ||
@@ -733,0 +739,0 @@ const mapping = Mapping.map(Thing) |
@@ -44,2 +44,28 @@ const test = require('ava'); | ||
test('assign missing, touched & schema', t => { | ||
const mask = Mask.cover(Thing).with({ | ||
size: true, | ||
physical: true | ||
}); | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
const thing = new Thing({}); | ||
const touched = mask.settle( | ||
thing, | ||
new Thing({name: 'overriden', description, physical}) | ||
); | ||
t.is(thing.name, undefined); | ||
t.is(thing.description, undefined); | ||
t.is(thing.size, undefined); | ||
t.is(thing.physical, physical); | ||
t.true(touched.name); | ||
t.true(touched.description); | ||
t.is(touched.size, undefined); | ||
t.is(touched.physical, undefined); | ||
}); | ||
class Gift extends Domain { | ||
@@ -198,5 +224,6 @@ get props() { | ||
box, | ||
new Box({thing: {physical}}) | ||
new Box({thing: new Thing({physical})}) | ||
); | ||
t.true(box.thing instanceof Thing); | ||
t.is(box.thing.name, undefined); | ||
@@ -310,3 +337,3 @@ t.is(box.thing.description, undefined); | ||
boxes, | ||
new Boxes({things: [{name: 'overriden', description, physical}, {name: 'overriden', description, physical}]}) | ||
new Boxes(({things: [new Thing({name: 'overriden', description, physical}), new Thing({name: 'overriden', description, physical})]})) | ||
); | ||
@@ -331,2 +358,25 @@ | ||
test('assign deep array, typed', t => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: [{ | ||
physical: true | ||
}] | ||
}); | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
const boxes = new Boxes({things: []}); | ||
mask.settle( | ||
boxes, | ||
new Boxes(({things: [new Thing({name: 'overriden', description, physical})]})) | ||
); | ||
t.true(boxes.things[0] instanceof Thing); | ||
t.is(boxes.things[0].name, undefined); | ||
t.is(boxes.things[0].description, undefined); | ||
t.is(boxes.things[0].size, undefined); | ||
t.is(boxes.things[0].physical, physical); | ||
}); | ||
test('assign deep array, untouched', t => { | ||
@@ -344,3 +394,3 @@ const mask = Mask.cover(Boxes).with({ | ||
boxes, | ||
new Boxes({things: [{name, description, physical}, {name, description, physical}]}) | ||
new Boxes({things: [new Thing({name, description, physical}), new Thing({name, description, physical})]}) | ||
); | ||
@@ -358,2 +408,27 @@ | ||
test('assign missing deep array, untouched', t => { | ||
const mask = Mask.cover(Boxes).with({ | ||
things: true | ||
}); | ||
const name = 'Shoes'; | ||
const description = 'Adventure Playground'; | ||
const physical = true; | ||
const boxes = new Boxes({}); | ||
const touched = mask.settle( | ||
boxes, | ||
new Boxes({things: [new Thing({name, description, physical}), new Thing({name, description, physical})]}) | ||
); | ||
for (let i = 0; i < 2; i++) { | ||
t.is(boxes.things[i].name, name); | ||
t.is(boxes.things[i].description, description); | ||
t.is(boxes.things[i].size, undefined); | ||
t.is(boxes.things[i].physical, physical); | ||
} | ||
t.is(touched.thing, undefined); | ||
}); | ||
class Cart extends Domain { | ||
@@ -360,0 +435,0 @@ get props() { |
115210
2832