pretty-format
Advanced tools
Comparing version 3.3.2 to 3.4.0
291
index.js
@@ -1,54 +0,39 @@ | ||
var isArguments = require('lodash/isArguments'); | ||
var isArray = require('lodash/isArray'); | ||
var isArrayBuffer = require('lodash/isArrayBuffer'); | ||
var isBoolean = require('lodash/isBoolean'); | ||
var isDate = require('lodash/isDate'); | ||
var isError = require('lodash/isError'); | ||
var isFinite = require('lodash/isFinite'); | ||
var isFunction = require('lodash/isFunction'); | ||
var isMap = require('lodash/isMap'); | ||
var isNaN = require('lodash/isNaN'); | ||
var isNull = require('lodash/isNull'); | ||
var isObject = require('lodash/isObject'); | ||
var isRegExp = require('lodash/isRegExp'); | ||
var isSet = require('lodash/isSet'); | ||
var isString = require('lodash/isString'); | ||
var isSymbol = require('lodash/isSymbol'); | ||
var isTypedArray = require('lodash/isTypedArray'); | ||
var isUndefined = require('lodash/isUndefined'); | ||
var isWeakMap = require('lodash/isWeakMap'); | ||
var isWeakSet = require('lodash/isWeakSet'); | ||
'use strict'; | ||
var toString = Object.prototype.toString; | ||
var toISOString = Date.prototype.toISOString; | ||
var errorToString = Error.prototype.toString; | ||
var regExpToString = RegExp.prototype.toString; | ||
var symbolToString = Symbol.prototype.toString; | ||
var SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/; | ||
var NEWLINE_REGEXP = /\n/ig; | ||
var SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/; | ||
function isArrayish(val) { | ||
return isArray(val) || isTypedArray(val) || isArrayBuffer(val); | ||
} | ||
var getSymbols = Object.getOwnPropertySymbols || function(obj) { | ||
return []; | ||
}; | ||
function isNumber(val) { | ||
return isFinite(val); | ||
function isToStringedArrayType(toStringed) { | ||
return ( | ||
toStringed === '[object Array]' || | ||
toStringed === '[object ArrayBuffer]' || | ||
toStringed === '[object DataView]' || | ||
toStringed === '[object Float32Array]' || | ||
toStringed === '[object Float64Array]' || | ||
toStringed === '[object Int8Array]' || | ||
toStringed === '[object Int16Array]' || | ||
toStringed === '[object Int32Array]' || | ||
toStringed === '[object Uint8Array]' || | ||
toStringed === '[object Uint8ClampedArray]' || | ||
toStringed === '[object Uint16Array]' || | ||
toStringed === '[object Uint32Array]' | ||
); | ||
} | ||
function isInfinity(val) { | ||
return val === Infinity || val === -Infinity; | ||
function printNumber(val) { | ||
if (val != +val) return 'NaN'; | ||
var isNegativeZero = val === 0 && (1 / val) < 0; | ||
return isNegativeZero ? '-0' : '' + val; | ||
} | ||
function isNegativeZero(val) { | ||
return val === 0 && (1 / val) < 0; | ||
} | ||
function getSymbols(obj) { | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
return Object.getOwnPropertySymbols(obj); | ||
} else { | ||
return []; | ||
} | ||
} | ||
function indent(str, opts) { | ||
var indentation = new Array(opts.indent + 1).join(' '); | ||
return indentation + str.replace(NEWLINE_REGEXP, '\n' + indentation); | ||
} | ||
function printFunction(val) { | ||
@@ -62,3 +47,40 @@ if (val.name === '') { | ||
function printList(list, refs, opts, state) { | ||
function printSymbol(val) { | ||
return symbolToString.call(val).replace(SYMBOL_REGEXP, 'Symbol($1)'); | ||
} | ||
function printError(val) { | ||
return '[' + errorToString.call(val) + ']'; | ||
} | ||
function printBasicValue(val) { | ||
if (val === true || val === false) return '' + val; | ||
if (val === undefined) return 'undefined'; | ||
if (val === null) return 'null'; | ||
var typeOf = typeof val; | ||
if (typeOf === 'number') return printNumber(val); | ||
if (typeOf === 'string') return '"' + val + '"'; | ||
if (typeOf === 'function') return printFunction(val); | ||
if (typeOf === 'symbol') return printSymbol(val); | ||
var toStringed = toString.call(val); | ||
if (toStringed === '[object WeakMap]') return 'WeakMap {}'; | ||
if (toStringed === '[object WeakSet]') return 'WeakSet {}'; | ||
if (toStringed === '[object Function]' || toStringed === '[object GeneratorFunction]') return printFunction(val); | ||
if (toStringed === '[object Symbol]') return printSymbol(val); | ||
if (toStringed === '[object Date]') return toISOString.call(val); | ||
if (toStringed === '[object Error]') return printError(val); | ||
if (toStringed === '[object RegExp]') return regExpToString.call(val); | ||
if (toStringed === '[object Arguments]' && val.length === 0) return 'Arguments []'; | ||
if (isToStringedArrayType(toStringed) && val.length === 0) return val.constructor.name + ' []'; | ||
if (val instanceof Error) return printError(val); | ||
return false; | ||
} | ||
function printList(list, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
var body = ''; | ||
@@ -69,4 +91,6 @@ | ||
var innerIndent = prevIndent + indent; | ||
for (var i = 0; i < list.length; i++) { | ||
body += indent(print(list[i], refs, opts, state), opts); | ||
body += innerIndent + print(list[i], indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
@@ -78,3 +102,3 @@ if (i < list.length - 1) { | ||
body += '\n'; | ||
body += '\n' + prevIndent; | ||
} | ||
@@ -85,11 +109,11 @@ | ||
function printArray(val, refs, opts, state) { | ||
return val.constructor.name + ' ' + printList(val, refs, opts, state); | ||
function printArguments(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
return 'Arguments ' + printList(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
} | ||
function printArguments(val, refs, opts, state) { | ||
return 'Arguments ' + printList(val, refs, opts, state); | ||
function printArray(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
return val.constructor.name + ' ' + printList(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
} | ||
function printMap(val, refs, opts, state) { | ||
function printMap(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
var result = 'Map {'; | ||
@@ -102,7 +126,9 @@ var iterator = val.entries(); | ||
var innerIndent = prevIndent + indent; | ||
while (!current.done) { | ||
var key = print(current.value[0], refs, opts, state); | ||
var value = print(current.value[1], refs, opts, state); | ||
var key = print(current.value[0], indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
var value = print(current.value[1], indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
result += indent(key + ' => ' + value, opts); | ||
result += innerIndent + key + ' => ' + value; | ||
@@ -116,3 +142,3 @@ current = iterator.next(); | ||
result += '\n'; | ||
result += '\n' + prevIndent; | ||
} | ||
@@ -123,3 +149,3 @@ | ||
function printObject(val, refs, opts, state) { | ||
function printObject(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
var result = val.constructor.name + ' {'; | ||
@@ -131,3 +157,3 @@ var keys = Object.keys(val).sort(); | ||
keys = keys.filter(function(key) { | ||
return !isSymbol(key); | ||
return !(typeof key === 'symbol' || toString.call(key) === '[object Symbol]'); | ||
}).concat(symbols); | ||
@@ -139,8 +165,10 @@ } | ||
var innerIndent = prevIndent + indent; | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
var name = print(key, refs, opts, state); | ||
var value = print(val[key], refs, opts, state); | ||
var name = print(key, indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
var value = print(val[key], indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
result += indent(name + ': ' + value, opts); | ||
result += innerIndent + name + ': ' + value; | ||
@@ -152,3 +180,3 @@ if (i < keys.length - 1) { | ||
result += '\n'; | ||
result += '\n' + prevIndent; | ||
} | ||
@@ -159,3 +187,3 @@ | ||
function printSet(val, refs, opts, state) { | ||
function printSet(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
var result = 'Set {'; | ||
@@ -168,7 +196,7 @@ var iterator = val.entries(); | ||
var innerIndent = prevIndent + indent; | ||
while (!current.done) { | ||
var value = print(current.value[1], refs, opts, state); | ||
result += innerIndent + print(current.value[1], indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
result += indent(value, opts); | ||
current = iterator.next(); | ||
@@ -181,3 +209,3 @@ | ||
result += '\n'; | ||
result += '\n' + prevIndent; | ||
} | ||
@@ -188,16 +216,32 @@ | ||
function printWithPlugin(plugin, val, refs, opts, state) { | ||
function boundPrint(val) { | ||
return print(val, refs, opts, state); | ||
function printComplexValue(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
var toStringed = toString.call(val); | ||
refs = refs.slice(); | ||
if (refs.indexOf(val) > -1) { | ||
return '[Circular]'; | ||
} else { | ||
refs.push(val); | ||
} | ||
function boundIndent(val) { | ||
return indent(val, opts); | ||
currentDepth++; | ||
var hitMaxDepth = currentDepth > maxDepth; | ||
if (toStringed === '[object Arguments]') { | ||
return hitMaxDepth ? '[Arguments]' : printArguments(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
} else if (isToStringedArrayType(toStringed)) { | ||
return hitMaxDepth ? '[Array]' : printArray(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
} else if (toStringed === '[object Map]') { | ||
return hitMaxDepth ? '[Map]' : printMap(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
} else if (toStringed === '[object Set]') { | ||
return hitMaxDepth ? '[Set]' : printSet(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
} else if (typeof val === 'object') { | ||
return hitMaxDepth ? '[Object]' : printObject(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
} | ||
return plugin.print(val, boundPrint, boundIndent); | ||
} | ||
function printValue(val, refs, opts, state) { | ||
var plugins = opts.plugins; | ||
function printPlugin(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
var match = false; | ||
@@ -208,47 +252,27 @@ for (var p = 0; p < plugins.length; p++) { | ||
if (plugin.test(val)) { | ||
return printWithPlugin(plugin, val, refs, opts, state); | ||
match = true; | ||
break; | ||
} | ||
} | ||
// Simple values | ||
if ( isBoolean (val) ) return Boolean.prototype.toString.call(val); | ||
if ( isDate (val) ) return Date.prototype.toISOString.call(val); | ||
if ( isError (val) ) return '[' + Error.prototype.toString.call(val) + ']'; | ||
if ( isFunction (val) ) return printFunction(val); | ||
if ( isInfinity (val) ) return Infinity.toString.call(val); | ||
if ( isNaN (val) ) return 'NaN'; | ||
if ( isNull (val) ) return 'null'; | ||
if ( isNumber (val) ) return isNegativeZero(val) ? '-0' : '' + val; | ||
if ( isRegExp (val) ) return RegExp.prototype.toString.call(val) | ||
if ( isString (val) ) return '"' + val + '"'; | ||
if ( isSymbol (val) ) return Symbol.prototype.toString.call(val).replace(SYMBOL_REGEXP, 'Symbol($1)'); | ||
if ( isUndefined (val) ) return 'undefined'; | ||
if ( isWeakMap (val) ) return 'WeakMap {}'; | ||
if ( isWeakSet (val) ) return 'WeakSet {}'; | ||
if (!match) { | ||
return false; | ||
} | ||
var stop = opts.maxDepth < state.depth; | ||
function boundPrint(val) { | ||
return print(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
} | ||
if ( isArguments (val) ) return stop ? '[Arguments]' : printArguments (val, refs, opts, state); | ||
if ( isArrayish (val) ) return stop ? '[Array]' : printArray (val, refs, opts, state); | ||
if ( isMap (val) ) return stop ? '[Map]' : printMap (val, refs, opts, state); | ||
if ( isSet (val) ) return stop ? '[Set]' : printSet (val, refs, opts, state); | ||
// purposefully last: | ||
if ( isObject (val) ) return stop ? '[Object]' : printObject (val, refs, opts, state); | ||
} | ||
function print(val, refs, opts, state) { | ||
refs = refs.slice(); // clone | ||
if (refs.indexOf(val) !== -1) { | ||
return '[Circular]'; | ||
} else { | ||
refs.push(val); | ||
function boundIndent(str) { | ||
var indentation = prevIndent + indent; | ||
return indentation + str.replace(NEWLINE_REGEXP, '\n' + indentation); | ||
} | ||
state.depth++; | ||
var result = printValue(val, refs, opts, state); | ||
state.depth--; | ||
return result; | ||
return plugin.print(val, boundPrint, boundIndent); | ||
} | ||
function print(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
return printBasicValue(val) || printComplexValue(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
} | ||
var DEFAULTS = { | ||
@@ -278,11 +302,34 @@ indent: 2, | ||
module.exports = function prettyFormat(val, opts) { | ||
opts = opts || {}; | ||
validateOptions(opts) | ||
opts = normalizeOptions(opts) | ||
plugins = opts.plugins; | ||
function createIndent(indent) { | ||
return new Array(indent + 1).join(' '); | ||
} | ||
return print(val, [], opts, { | ||
depth: 0 | ||
}); | ||
}; | ||
function prettyFormat(val, opts) { | ||
if (!opts) { | ||
opts = DEFAULTS; | ||
} else { | ||
validateOptions(opts) | ||
opts = normalizeOptions(opts); | ||
} | ||
var indent; | ||
var refs; | ||
var prevIndent = ''; | ||
var currentDepth = 0; | ||
if (opts && opts.plugins.length) { | ||
indent = createIndent(opts.indent); | ||
refs = []; | ||
var pluginsResult = printPlugin(val, indent, prevIndent, refs, opts.maxDepth, currentDepth, opts.plugins); | ||
if (pluginsResult) return pluginsResult; | ||
} | ||
var basicResult = printBasicValue(val); | ||
if (basicResult) return basicResult; | ||
if (!indent) indent = createIndent(opts.indent); | ||
if (!refs) refs = []; | ||
return printComplexValue(val, indent, prevIndent, refs, opts.maxDepth, currentDepth, opts.plugins); | ||
} | ||
module.exports = prettyFormat; |
{ | ||
"name": "pretty-format", | ||
"version": "3.3.2", | ||
"version": "3.4.0", | ||
"description": "Stringify any JavaScript value.", | ||
@@ -13,3 +13,4 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "jest" | ||
"test": "jest", | ||
"perf": "node perf/test.js" | ||
}, | ||
@@ -22,8 +23,7 @@ "jest": { | ||
"devDependencies": { | ||
"chalk": "^1.1.3", | ||
"jest": "^12.1.0", | ||
"left-pad": "^1.1.0", | ||
"react": "15.2.0-rc.1" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.13.1" | ||
} | ||
} |
@@ -5,7 +5,7 @@ # pretty-format [![Travis build status](http://img.shields.io/travis/thejameskyle/pretty-format.svg?style=flat)](https://travis-ci.org/thejameskyle/pretty-format) | ||
Supports objects, arrays (and typed arrays and array buffers), arguments, | ||
booleans, dates, errors, functions, `Infinity`, maps, `NaN`, `null`, numbers, | ||
regular expressions, sets, strings, symbols, `undefined`, weak maps, weak | ||
sets, and circular data structures. | ||
- Supports [all built-in JavaScript types](#type-support) | ||
- [Blazingly fast](https://gist.github.com/thejameskyle/2b04ffe4941aafa8f970de077843a8fd) (similar performance to v8's `JSON.stringify` and significantly faster than Node's `util.format`) | ||
- Plugin system for extending with custom types (i.e. [`ReactTestComponent`](#reacttestcomponent-plugin)) | ||
## Installation | ||
@@ -50,2 +50,6 @@ | ||
#### Type Support | ||
`Object`, `Array`, `ArrayBuffer`, `DataView`, `Float32Array`, `Float64Array`, `Int8Array`, `Int16Array`, `Int32Array`, `Uint8Array`, `Uint8ClampedArray`, `Uint16Array`, `Uint32Array`, `arguments`, `Boolean`, `Date`, `Error`, `Function`, `Infinity`, `Map`, `NaN`, `null`, `Number`, `RegExp`, `Set`, `String`, `Symbol`, `undefined`, `WeakMap`, `WeakSet` | ||
### Plugins | ||
@@ -75,3 +79,3 @@ | ||
#### ReactTestComponent plugin | ||
#### `ReactTestComponent` plugin | ||
@@ -78,0 +82,0 @@ ```js |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
0
95
14017
4
6
287
- Removedlodash@^4.13.1
- Removedlodash@4.17.21(transitive)