diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-08-14 05:01:11 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-08-14 05:02:09 +0200 |
commit | 363723fc84f7b8477592e0105aeb331ec9a017af (patch) | |
tree | 29f92724f34131bac64d6a318dd7e30612e631c7 /node_modules/stringify-object | |
parent | 5634e77ad96bfe1818f6b6ee70b7379652e5487f (diff) |
node_modules
Diffstat (limited to 'node_modules/stringify-object')
-rw-r--r-- | node_modules/stringify-object/index.js | 90 | ||||
-rw-r--r-- | node_modules/stringify-object/package.json | 15 | ||||
-rw-r--r-- | node_modules/stringify-object/readme.md | 68 |
3 files changed, 115 insertions, 58 deletions
diff --git a/node_modules/stringify-object/index.js b/node_modules/stringify-object/index.js index 502982de5..4a55bd9ec 100644 --- a/node_modules/stringify-object/index.js +++ b/node_modules/stringify-object/index.js @@ -1,20 +1,23 @@ 'use strict'; -var isRegexp = require('is-regexp'); -var isPlainObj = require('is-plain-obj'); +const isRegexp = require('is-regexp'); +const isObj = require('is-obj'); +const getOwnEnumPropSymbols = require('get-own-enumerable-property-symbols'); -module.exports = function (val, opts, pad) { - var seen = []; +module.exports = (val, opts, pad) => { + const seen = []; return (function stringify(val, opts, pad) { opts = opts || {}; opts.indent = opts.indent || '\t'; pad = pad || ''; - var tokens; - if(opts.inlineCharacterLimit == void 0) { + + let tokens; + + if (opts.inlineCharacterLimit === undefined) { tokens = { newLine: '\n', newLineOrSpace: '\n', - pad: pad, + pad, indent: pad + opts.indent }; } else { @@ -23,23 +26,27 @@ module.exports = function (val, opts, pad) { newLineOrSpace: '@@__STRINGIFY_OBJECT_NEW_LINE_OR_SPACE__@@', pad: '@@__STRINGIFY_OBJECT_PAD__@@', indent: '@@__STRINGIFY_OBJECT_INDENT__@@' - } + }; } - var expandWhiteSpace = function(string) { - if (opts.inlineCharacterLimit == void 0) { return string; } - var oneLined = string. - replace(new RegExp(tokens.newLine, 'g'), ''). - replace(new RegExp(tokens.newLineOrSpace, 'g'), ' '). - replace(new RegExp(tokens.pad + '|' + tokens.indent, 'g'), ''); - - if(oneLined.length <= opts.inlineCharacterLimit) { + + const expandWhiteSpace = string => { + if (opts.inlineCharacterLimit === undefined) { + return string; + } + + const oneLined = string + .replace(new RegExp(tokens.newLine, 'g'), '') + .replace(new RegExp(tokens.newLineOrSpace, 'g'), ' ') + .replace(new RegExp(tokens.pad + '|' + tokens.indent, 'g'), ''); + + if (oneLined.length <= opts.inlineCharacterLimit) { return oneLined; - } else { - return string. - replace(new RegExp(tokens.newLine + '|' + tokens.newLineOrSpace, 'g'), '\n'). - replace(new RegExp(tokens.pad, 'g'), pad). - replace(new RegExp(tokens.indent, 'g'), pad + opts.indent); } + + return string + .replace(new RegExp(tokens.newLine + '|' + tokens.newLineOrSpace, 'g'), '\n') + .replace(new RegExp(tokens.pad, 'g'), pad) + .replace(new RegExp(tokens.indent, 'g'), pad + opts.indent); }; if (seen.indexOf(val) !== -1) { @@ -51,12 +58,13 @@ module.exports = function (val, opts, pad) { typeof val === 'number' || typeof val === 'boolean' || typeof val === 'function' || + typeof val === 'symbol' || isRegexp(val)) { return String(val); } if (val instanceof Date) { - return 'new Date(\'' + val.toISOString() + '\')'; + return `new Date('${val.toISOString()}')`; } if (Array.isArray(val)) { @@ -66,9 +74,13 @@ module.exports = function (val, opts, pad) { seen.push(val); - var ret = '[' + tokens.newLine + val.map(function (el, i) { - var eol = val.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; - return tokens.indent + stringify(el, opts, pad + opts.indent) + eol; + const ret = '[' + tokens.newLine + val.map((el, i) => { + const eol = val.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; + let value = stringify(el, opts, pad + opts.indent); + if (opts.transform) { + value = opts.transform(val, i, value); + } + return tokens.indent + value + eol; }).join('') + tokens.pad + ']'; seen.pop(val); @@ -76,8 +88,8 @@ module.exports = function (val, opts, pad) { return expandWhiteSpace(ret); } - if (isPlainObj(val)) { - var objKeys = Object.keys(val); + if (isObj(val)) { + const objKeys = Object.keys(val).concat(getOwnEnumPropSymbols(val)); if (objKeys.length === 0) { return '{}'; @@ -85,14 +97,20 @@ module.exports = function (val, opts, pad) { seen.push(val); - var ret = '{' + tokens.newLine + objKeys.map(function (el, i) { + const ret = '{' + tokens.newLine + objKeys.map((el, i) => { if (opts.filter && !opts.filter(val, el)) { return ''; } - var eol = objKeys.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; - var key = /^[a-z$_][a-z$_0-9]*$/i.test(el) ? el : stringify(el, opts); - return tokens.indent + key + ': ' + stringify(val[el], opts, pad + opts.indent) + eol; + const eol = objKeys.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace; + const isSymbol = typeof el === 'symbol'; + const isClassic = !isSymbol && /^[a-z$_][a-z$_0-9]*$/i.test(el); + const key = isSymbol || isClassic ? el : stringify(el, opts); + let value = stringify(val[el], opts, pad + opts.indent); + if (opts.transform) { + value = opts.transform(val, el, value); + } + return tokens.indent + String(key) + ': ' + value + eol; }).join('') + tokens.pad + '}'; seen.pop(val); @@ -100,14 +118,14 @@ module.exports = function (val, opts, pad) { return expandWhiteSpace(ret); } - val = String(val).replace(/[\r\n]/g, function (x) { - return x === '\n' ? '\\n' : '\\r'; - }); + val = String(val).replace(/[\r\n]/g, x => x === '\n' ? '\\n' : '\\r'); if (opts.singleQuotes === false) { - return '"' + val.replace(/"/g, '\\\"') + '"'; + val = val.replace(/"/g, '\\"'); + return `"${val}"`; } - return '\'' + val.replace(/'/g, '\\\'') + '\''; + val = val.replace(/\\?'/g, '\\\''); + return `'${val}'`; })(val, opts, pad); }; diff --git a/node_modules/stringify-object/package.json b/node_modules/stringify-object/package.json index d4a1265ae..625864260 100644 --- a/node_modules/stringify-object/package.json +++ b/node_modules/stringify-object/package.json @@ -1,6 +1,6 @@ { "name": "stringify-object", - "version": "2.4.0", + "version": "3.2.0", "description": "Stringify an object/array like JSON.stringify just without all the double-quotes", "license": "BSD-2-Clause", "repository": "yeoman/stringify-object", @@ -10,10 +10,10 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=0.10.0" + "node": ">=4" }, "scripts": { - "test": "mocha" + "test": "xo && mocha" }, "files": [ "index.js" @@ -29,10 +29,15 @@ "json" ], "dependencies": { - "is-plain-obj": "^1.0.0", + "get-own-enumerable-property-symbols": "^1.0.1", + "is-obj": "^1.0.1", "is-regexp": "^1.0.0" }, "devDependencies": { - "mocha": "*" + "mocha": "*", + "xo": "*" + }, + "xo": { + "esnext": true } } diff --git a/node_modules/stringify-object/readme.md b/node_modules/stringify-object/readme.md index 57c5bae9e..177f05445 100644 --- a/node_modules/stringify-object/readme.md +++ b/node_modules/stringify-object/readme.md @@ -1,6 +1,6 @@ # stringify-object [![Build Status](https://secure.travis-ci.org/yeoman/stringify-object.svg?branch=master)](http://travis-ci.org/yeoman/stringify-object) -> Stringify an object/array like JSON.stringify just without all the double-quotes. +> Stringify an object/array like JSON.stringify just without all the double-quotes Useful for when you want to get the string representation of an object in a formatted way. @@ -17,13 +17,15 @@ $ npm install --save stringify-object ## Usage ```js -var obj = { +const stringifyObject = require('stringify-object'); + +const obj = { foo: 'bar', 'arr': [1, 2, 3], nested: { hello: "world" } }; -var pretty = stringifyObject(obj, { +const pretty = stringifyObject(obj, { indent: ' ', singleQuotes: false }); @@ -53,49 +55,81 @@ Circular references will be replaced with `"[Circular]"`. #### input -*Required* -Type: `object`, `array` +Type: `Object` `Array` #### options ##### indent -Type: `string` +Type: `string`<br> Default: `'\t'` -Choose the indentation you prefer. +Preferred indentation. ##### singleQuotes -Type: `boolean` +Type: `boolean`<br> Default: `true` Set to false to get double-quoted strings. ##### filter(obj, prop) -Type: `function` +Type: `Function` + +Expected to return a `boolean` of whether to include the property `prop` of the object `obj` in the output. + +##### transform(obj, prop, originalResult) + +Type: `Function`<br> +Default: `undefined` + +Expected to return a `string` that transforms the string that resulted from stringifying `obj[prop]`. This can be used to detect special types of objects that need to be stringified in a particular way. The `transform` function might return an alternate string in this case, otherwise returning the `originalResult`. + +Here's an example that uses the `transform` option to mask fields named "password": + +```js +const obj = { + user: 'becky', + password: 'secret' +} + +const pretty = stringifyObject(obj, { + transform: (obj, prop, originalResult) => { + if (prop === 'password') { + return originalResult.replace(/\w/g, '*'); + } else { + return originalResult; + } + } +}); + +console.log(pretty); +/* +{ + user: 'becky', + password: '******' +} +*/ +``` -Expected to return a boolean of whether to keep the object. ##### inlineCharacterLimit Type: `number` -Default: undefined -When set, will inline values up to `inlineCharacterLimit` length for the sake -of more terse output. +When set, will inline values up to `inlineCharacterLimit` length for the sake of more terse output. For example, given the example at the top of the README: ```js -var obj = { +const obj = { foo: 'bar', 'arr': [1, 2, 3], nested: { hello: "world" } }; -var pretty = stringifyObject(obj, { +const pretty = stringifyObject(obj, { indent: ' ', singleQuotes: false, inlineCharacterLimit: 12 @@ -113,8 +147,8 @@ console.log(pretty); */ ``` -As you can see, `arr` was printed as a one-liner because its string was shorter -than 12 characters. +As you can see, `arr` was printed as a one-liner because its string was shorter than 12 characters. + ## License |