javascript-stringify
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -96,3 +96,3 @@ (function (root, stringify) { | ||
* | ||
* @param {Array} path | ||
* @param {Array} path | ||
* @return {string} | ||
@@ -115,55 +115,71 @@ */ | ||
/** | ||
* Convert JavaScript objects into strings. | ||
* Stringify an array of values. | ||
* | ||
* @param {Array} array | ||
* @param {string} indent | ||
* @param {Function} next | ||
* @return {string} | ||
*/ | ||
var OBJECT_TYPES = { | ||
'[object Array]': function (array, indent, next) { | ||
// Map array values to their stringified values with correct indentation. | ||
var values = array.map(function (value, index) { | ||
var str = next(value, index); | ||
function stringifyArray (array, indent, next) { | ||
// Map array values to their stringified values with correct indentation. | ||
var values = array.map(function (value, index) { | ||
var str = next(value, index); | ||
if (str === undefined) { | ||
return String(str) | ||
} | ||
if (str === undefined) { | ||
return String(str); | ||
} | ||
return indent + str.split('\n').join('\n' + indent); | ||
}).join(indent ? ',\n' : ','); | ||
return indent + str.split('\n').join('\n' + indent); | ||
}).join(indent ? ',\n' : ','); | ||
// Wrap the array in newlines if we have indentation set. | ||
if (indent && values) { | ||
return '[\n' + values + '\n]'; | ||
} | ||
// Wrap the array in newlines if we have indentation set. | ||
if (indent && values) { | ||
return '[\n' + values + '\n]'; | ||
} | ||
return '[' + values + ']'; | ||
}, | ||
'[object Object]': function (object, indent, next) { | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(object)) { | ||
return 'new Buffer(' + stringify(object.toString()) + ')'; | ||
return '[' + values + ']'; | ||
} | ||
/** | ||
* Stringify a map of values. | ||
* | ||
* @param {Object} object | ||
* @param {string} indent | ||
* @param {Function} next | ||
* @return {string} | ||
*/ | ||
function stringifyObject (object, indent, next) { | ||
// Iterate over object keys and concat string together. | ||
var values = Object.keys(object).reduce(function (values, key) { | ||
var value = next(object[key], key); | ||
// Omit `undefined` object values. | ||
if (value === undefined) { | ||
return values; | ||
} | ||
// Iterate over object keys and concat string together. | ||
var values = Object.keys(object).reduce(function (values, key) { | ||
var value = next(object[key], key); | ||
// String format the key and value data. | ||
key = isValidVariableName(key) ? key : stringify(key); | ||
value = String(value).split('\n').join('\n' + indent); | ||
// Omit `undefined` object values. | ||
if (value === undefined) { | ||
return values; | ||
} | ||
// Push the current object key and value into the values array. | ||
values.push(indent + key + ':' + (indent ? ' ' : '') + value); | ||
// String format the key and value data. | ||
key = isValidVariableName(key) ? key : stringify(key); | ||
value = String(value).split('\n').join('\n' + indent); | ||
return values; | ||
}, []).join(indent ? ',\n' : ','); | ||
// Push the current object key and value into the values array. | ||
values.push(indent + key + ':' + (indent ? ' ' : '') + value); | ||
// Wrap the object in newlines if we have indentation set. | ||
if (indent && values) { | ||
return '{\n' + values + '\n}'; | ||
} | ||
return values; | ||
}, []).join(indent ? ',\n' : ','); | ||
return '{' + values + '}'; | ||
} | ||
// Wrap the object in newlines if we have indentation set. | ||
if (indent && values) { | ||
return '{\n' + values + '\n}'; | ||
} | ||
return '{' + values + '}'; | ||
}, | ||
/** | ||
* Convert JavaScript objects into strings. | ||
*/ | ||
var OBJECT_TYPES = { | ||
'[object Array]': stringifyArray, | ||
'[object Object]': stringifyObject, | ||
'[object Date]': function (date) { | ||
@@ -182,17 +198,3 @@ return 'new Date(' + date.getTime() + ')'; | ||
'[object Uint8Array]': function (array, indent) { | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(array)) { | ||
return 'new Buffer(' + stringify(array.toString()) + ')'; | ||
} | ||
if (indent) { | ||
var str = ''; | ||
for (var i = 0; i < array.length; i++) { | ||
str += indent + array[i] + ',\n' | ||
} | ||
return 'new Uint8Array([\n' + str + '\n])' | ||
} | ||
return 'new Uint8Array([' + array.join(indent ? ',\n' : ',') + '])' | ||
return 'new Uint8Array(' + stringifyArray(array) + ')'; | ||
}, | ||
@@ -233,2 +235,7 @@ '[object RegExp]': String, | ||
// Handle buffer objects before recursing (node < 6 was an object, node >= 6 is a `Uint8Array`). | ||
if (typeof Buffer === 'function' && Buffer.isBuffer(value)) { | ||
return 'new Buffer(' + next(value.toString()) + ')'; | ||
} | ||
// Use the internal object string to select stringification method. | ||
@@ -260,2 +267,3 @@ var toString = OBJECT_TYPES[Object.prototype.toString.call(value)]; | ||
var references = !!options.references; | ||
var skipUndefinedProperties = !!options.skipUndefinedProperties; | ||
var valueCount = Number(options.maxValues) || 100000; | ||
@@ -277,2 +285,6 @@ | ||
function next (value, key) { | ||
if (skipUndefinedProperties && value === undefined) { | ||
return undefined; | ||
} | ||
path.push(key); | ||
@@ -279,0 +291,0 @@ var result = recurse(value, stringify); |
{ | ||
"name": "javascript-stringify", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`", | ||
@@ -5,0 +5,0 @@ "main": "javascript-stringify.js", |
@@ -50,2 +50,3 @@ # JavaScript Stringify | ||
* **references** _(boolean, default: false)_ Restore circular/repeated references in the object (uses IIFE) | ||
* **skipUndefinedProperties** _(boolean, default: false)_ Omits `undefined` properties instead of restoring as `undefined` | ||
@@ -52,0 +53,0 @@ ### Examples |
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
16081
329
119