From a21cf02f433b7db05614244ade0992a02e87a58e Mon Sep 17 00:00:00 2001 From: will Farrell Date: Tue, 26 Jul 2016 14:43:20 -0600 Subject: [PATCH] update `==`, `object <-> array` --- lib/dot/coerce.def | 32 ++++++++++++++++---------------- spec/coercion.spec.js | 10 +++++----- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/dot/coerce.def b/lib/dot/coerce.def index b6f6c99..f3ff718 100644 --- a/lib/dot/coerce.def +++ b/lib/dot/coerce.def @@ -4,8 +4,8 @@ , $coerced = 'coerced' + $lvl; }} var {{=$dataType}} = typeof {{=$data}}; - {{? it.opts.coerceTypes === 'array'}} - if ({{=$dataType}} === 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array'; + {{? it.opts.coerceTypes == 'array'}} + if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array'; {{?}} var {{=$coerced}} = undefined; @@ -17,34 +17,34 @@ {{ $bracesCoercion += '}'; }} {{?}} - {{? it.opts.coerceTypes === 'array' && $type !== 'array' }} - if ({{=$dataType}} === 'array' && {{=$data}}.length === 1) { + {{? it.opts.coerceTypes == 'array' && $type != 'array' }} + if ({{=$dataType}} == 'array' && {{=$data}}.length == 1) { {{=$coerced}} = {{=$data}} = {{=$data}}[0]; {{=$dataType}} = typeof {{=$data}}; - /*if ({{=$dataType}} === 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';*/ + /*if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';*/ } {{?}} - {{? $type === 'string' }} - if ({{=$dataType}} === 'number' || {{=$dataType}} === 'boolean') + {{? $type == 'string' }} + if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean') {{=$coerced}} = '' + {{=$data}}; else if ({{=$data}} === null) {{=$coerced}} = ''; - {{?? $type === 'number' || $type === 'integer' }} - if ({{=$dataType}} === 'boolean' || {{=$data}} === null - || ({{=$dataType}} === 'string' && {{=$data}} && {{=$data}} == +{{=$data}} - {{? $type === 'integer' }} && !({{=$data}} % 1){{?}})) + {{?? $type == 'number' || $type == 'integer' }} + if ({{=$dataType}} == 'boolean' || {{=$data}} === null + || ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}} + {{? $type == 'integer' }} && !({{=$data}} % 1){{?}})) {{=$coerced}} = +{{=$data}}; - {{?? $type === 'boolean' }} + {{?? $type == 'boolean' }} if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null) {{=$coerced}} = false; else if ({{=$data}} === 'true' || {{=$data}} === 1) {{=$coerced}} = true; - {{?? $type === 'null' }} + {{?? $type == 'null' }} if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false) {{=$coerced}} = null; - {{?? it.opts.coerceTypes === 'array' && $type === 'array' }} - if ({{=$dataType}} !== 'array') - {{=$coerced}} = [{{=$data}}]; + {{?? it.opts.coerceTypes == 'array' && $type == 'array' }} + if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null) + {{=$coerced}} = [{{=$data}}]; {{?}} {{~}} diff --git a/spec/coercion.spec.js b/spec/coercion.spec.js index 031282b..35a2225 100644 --- a/spec/coercion.spec.js +++ b/spec/coercion.spec.js @@ -156,9 +156,9 @@ coercionArrayRules.boolean.array = [ coercionArrayRules.null.array = [ { from: [null], to: null } ]; -/*coercionArrayRules.object.array = [ - { from: [{}], to: {} } -];*/ +coercionArrayRules.object.array = [ + { from: [{}], to: undefined } +]; coercionArrayRules.array = { 'string': [ @@ -174,7 +174,7 @@ coercionArrayRules.array = { {from: null, to: [null]} ], 'object': [ - {from: {}, to: [{}]} + {from: {}, to: undefined} ] }; @@ -206,7 +206,7 @@ describe('Type coercion', function () { testRules(coercionArrayRules, function (test, schema, canCoerce, toType, fromType) { instances.forEach(function (ajv) { var valid = ajv.validate(schema, test.from); - //if (valid !== canCoerce) console.log(toType, '.', fromType, test, schema, ajv.errors); + if (valid !== canCoerce) console.log(toType, '.', fromType, test, schema, ajv.errors); valid. should.equal(canCoerce); }); });