fast-json-stringify
Advanced tools
Comparing version 1.1.1 to 1.2.0
114
index.js
@@ -93,3 +93,3 @@ 'use strict' | ||
var dependenciesName = [] | ||
if (hasAnyOf(schema)) { | ||
if (hasAnyOf(schema) || hasArrayOfTypes(schema)) { | ||
dependencies.push(new Ajv()) | ||
@@ -117,2 +117,36 @@ dependenciesName.push('ajv') | ||
function hasArrayOfTypes (schema) { | ||
if (Array.isArray(schema.type)) { return true } | ||
var i | ||
if (schema.type === 'object') { | ||
if (schema.properties) { | ||
var propertyKeys = Object.keys(schema.properties) | ||
for (i = 0; i < propertyKeys.length; i++) { | ||
if (hasArrayOfTypes(schema.properties[propertyKeys[i]])) { | ||
return true | ||
} | ||
} | ||
} | ||
} else if (schema.type === 'array') { | ||
if (Array.isArray(schema.items)) { | ||
for (i = 0; i < schema.items.length; i++) { | ||
if (hasArrayOfTypes(schema.items[i])) { | ||
return true | ||
} | ||
} | ||
} else if (schema.items) { | ||
return hasArrayOfTypes(schema.items) | ||
} | ||
} else if (Array.isArray(schema.anyOf)) { | ||
for (i = 0; i < schema.anyOf.length; i++) { | ||
if (hasArrayOfTypes(schema.anyOf[i])) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
function $asNull () { | ||
@@ -498,28 +532,3 @@ return 'null' | ||
const tmpRes = nested(laterCode, name, accessor, item, externalSchema, fullSchema, i) | ||
var condition = `i === ${i} && ` | ||
switch (item.type) { | ||
case 'null': | ||
condition += `obj${accessor} === null` | ||
break | ||
case 'string': | ||
condition += `typeof obj${accessor} === 'string'` | ||
break | ||
case 'integer': | ||
condition += `Number.isInteger(obj${accessor})` | ||
break | ||
case 'number': | ||
condition += `Number.isFinite(obj${accessor})` | ||
break | ||
case 'boolean': | ||
condition += `typeof obj${accessor} === 'boolean'` | ||
break | ||
case 'object': | ||
condition += `obj${accessor} && typeof obj${accessor} === 'object' && obj${accessor}.constructor === Object` | ||
break | ||
case 'array': | ||
condition += `Array.isArray(obj${accessor})` | ||
break | ||
default: | ||
throw new Error(`${item.type} unsupported`) | ||
} | ||
var condition = `i === ${i} && ${buildArrayTypeCondition(item.type, accessor)}` | ||
return { | ||
@@ -567,2 +576,39 @@ code: `${res.code} | ||
function buildArrayTypeCondition (type, accessor) { | ||
var condition | ||
switch (type) { | ||
case 'null': | ||
condition = `obj${accessor} === null` | ||
break | ||
case 'string': | ||
condition = `typeof obj${accessor} === 'string'` | ||
break | ||
case 'integer': | ||
condition = `Number.isInteger(obj${accessor})` | ||
break | ||
case 'number': | ||
condition = `Number.isFinite(obj${accessor})` | ||
break | ||
case 'boolean': | ||
condition = `typeof obj${accessor} === 'boolean'` | ||
break | ||
case 'object': | ||
condition = `obj${accessor} && typeof obj${accessor} === 'object' && obj${accessor}.constructor === Object` | ||
break | ||
case 'array': | ||
condition = `Array.isArray(obj${accessor})` | ||
break | ||
default: | ||
if (Array.isArray(type)) { | ||
var conditions = type.map((subType) => { | ||
return buildArrayTypeCondition(subType, accessor) | ||
}) | ||
condition = `(${conditions.join(' || ')})` | ||
} else { | ||
throw new Error(`${type} unsupported`) | ||
} | ||
} | ||
return condition | ||
} | ||
function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKey) { | ||
@@ -629,3 +675,15 @@ var code = '' | ||
default: | ||
throw new Error(`${type} unsupported`) | ||
if (Array.isArray(type)) { | ||
type.forEach((type, index) => { | ||
var tempSchema = {type: type} | ||
var nestedResult = nested(laterCode, name, key, tempSchema, externalSchema, fullSchema, subKey) | ||
code += ` | ||
${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(tempSchema, {depth: null})}, obj${accessor})) | ||
${nestedResult.code} | ||
` | ||
laterCode = nestedResult.laterCode | ||
}) | ||
} else { | ||
throw new Error(`${type} unsupported`) | ||
} | ||
} | ||
@@ -632,0 +690,0 @@ |
{ | ||
"name": "fast-json-stringify", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -29,3 +29,3 @@ "main": "index.js", | ||
"benchmark": "^2.1.4", | ||
"is-my-json-valid": "^2.17.1", | ||
"is-my-json-valid": "^2.17.2", | ||
"long": "^4.0.0", | ||
@@ -35,4 +35,4 @@ "pre-commit": "^1.2.2", | ||
"standard": "^11.0.0", | ||
"tap": "^11.0.0", | ||
"uglify-es": "^3.2.2" | ||
"tap": "^11.1.1", | ||
"uglify-es": "^3.3.10" | ||
}, | ||
@@ -39,0 +39,0 @@ "dependencies": { |
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
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
73738
23
2662