Comparing version 1.2.0 to 1.3.0
58
dist.js
@@ -15,2 +15,4 @@ | ||
var formatRef = function (formatter, path) { return annotate(formatter, 'References', '~' + path.join('.')); } | ||
var formatCollapsedObject = function (formatter, val) { return tsml(["\n ", "\n ", "\n ", "\n"], formatter.punctuation('('), formatter.string('collapsed'), formatter.punctuation(')')); } | ||
@@ -42,3 +44,27 @@ | ||
var formatWithDepth = function (obj, depth, formatter, offset) { | ||
var createRefMap = function () { | ||
var map = new Map() | ||
return function (path, val, replacer) { | ||
if (!val || typeof (val) !== 'object') { | ||
return null | ||
} | ||
var ref = map.get(val) | ||
if (ref) { return replacer(ref) } | ||
map.set(val, path) | ||
return null | ||
} | ||
} | ||
var formatWithDepth = function ( | ||
obj, | ||
formatter, | ||
ref | ||
) { | ||
var lookupRef = ref.lookupRef; | ||
var path = ref.path; | ||
var depth = ref.depth; | ||
var offset = ref.offset; | ||
var keys = Object.keys(obj) | ||
@@ -49,4 +75,11 @@ var coloredKeys = keys.map(function (key) { return formatter.property(key); }) | ||
var parts = keys.map(function (key, i) { | ||
var nextPath = path.concat([key]) | ||
var val = obj[key] | ||
var ref = lookupRef( | ||
nextPath, | ||
val, | ||
function (npath) { return formatRef(formatter, npath); } | ||
) | ||
var out = tsml(["\n ", "\n ", "\n "], lpadAlign(coloredKeys[i], coloredKeys, offset), formatter.punctuation(colon)) | ||
@@ -58,9 +91,13 @@ | ||
if (ref) { | ||
return out + ref | ||
} | ||
if (isIterableWithKeys(val)) { | ||
out += formatWithDepth( | ||
val, | ||
{ curr: depth.curr + 1, max: depth.max }, | ||
formatter, | ||
offset + longest(keys).length + colon.length | ||
) | ||
out += formatWithDepth(val, formatter, { | ||
offset: offset + longest(keys).length + colon.length, | ||
depth: { curr: depth.curr + 1, max: depth.max }, | ||
path: nextPath, | ||
lookupRef: lookupRef | ||
}) | ||
} else { | ||
@@ -95,3 +132,8 @@ out += formatValue(formatter, val) | ||
return formatWithDepth(obj, { curr: 0, max: depth }, formatter, offset); | ||
return formatWithDepth(obj, formatter, { | ||
depth: { curr: 0, max: depth }, | ||
lookupRef: createRefMap(), | ||
path: [], | ||
offset: offset | ||
}); | ||
} | ||
@@ -98,0 +140,0 @@ |
const format = require('./') | ||
console.log(format({ | ||
const a = { a: 1 } | ||
let tree = { | ||
message: 'hello world', | ||
a, | ||
b: a, | ||
dev: true, | ||
@@ -20,2 +24,6 @@ list: [ | ||
} | ||
})) | ||
} | ||
tree.body.circular = tree.body | ||
console.log(format(tree)) |
54
index.js
@@ -22,2 +22,5 @@ | ||
const formatRef = (formatter, path) => | ||
annotate(formatter, 'References', '~' + path.join('.')) | ||
const formatCollapsedObject = (formatter, val) => tsml` | ||
@@ -57,3 +60,22 @@ ${formatter.punctuation('(')} | ||
const formatWithDepth = (obj, depth, formatter, offset) => { | ||
const createRefMap = () => { | ||
let map = new Map() | ||
return (path, val, replacer) => { | ||
if (!val || typeof (val) !== 'object') { | ||
return null | ||
} | ||
const ref = map.get(val) | ||
if (ref) return replacer(ref) | ||
map.set(val, path) | ||
return null | ||
} | ||
} | ||
const formatWithDepth = ( | ||
obj, | ||
formatter, | ||
{ lookupRef, path, depth, offset } | ||
) => { | ||
const keys = Object.keys(obj) | ||
@@ -64,4 +86,11 @@ const coloredKeys = keys.map((key) => formatter.property(key)) | ||
const parts = keys.map((key, i) => { | ||
const nextPath = path.concat([key]) | ||
const val = obj[key] | ||
const ref = lookupRef( | ||
nextPath, | ||
val, | ||
(npath) => formatRef(formatter, npath) | ||
) | ||
let out = tsml` | ||
@@ -76,9 +105,13 @@ ${lpadAlign(coloredKeys[i], coloredKeys, offset)} | ||
if (ref) { | ||
return out + ref | ||
} | ||
if (isIterableWithKeys(val)) { | ||
out += formatWithDepth( | ||
val, | ||
{ curr: depth.curr + 1, max: depth.max }, | ||
formatter, | ||
offset + longest(keys).length + colon.length | ||
) | ||
out += formatWithDepth(val, formatter, { | ||
offset: offset + longest(keys).length + colon.length, | ||
depth: { curr: depth.curr + 1, max: depth.max }, | ||
path: nextPath, | ||
lookupRef | ||
}) | ||
} else { | ||
@@ -108,4 +141,9 @@ out += formatValue(formatter, val) | ||
offset = 2 | ||
) => formatWithDepth(obj, { curr: 0, max: depth }, formatter, offset) | ||
) => formatWithDepth(obj, formatter, { | ||
depth: { curr: 0, max: depth }, | ||
lookupRef: createRefMap(), | ||
path: [], | ||
offset | ||
}) | ||
module.exports = format |
{ | ||
"name": "fmt-obj", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Prettifies any javascript object in your console", | ||
"main": "dist.js", | ||
"license": "MIT", | ||
"repository": "queckezz/fmt-obj", | ||
"author": "queckezz <fabian.eichenberger@gmail.com>", | ||
"scripts": { | ||
"release": "npm run lint && xyz -m X.Y.Z", | ||
"lint": "standard", | ||
"precommit": "npm test", | ||
"prepublish": "buble --yes dangerousTaggedTemplateString index.js > dist.js", | ||
"test": "ava" | ||
"test": "standard && ava" | ||
}, | ||
"main": "dist.js", | ||
"keywords": [ | ||
"format", | ||
"pretty", | ||
"object", | ||
"prettify", | ||
"fmt" | ||
], | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/queckezz/fmt-obj" | ||
}, | ||
"author": "queckezz <fabian.eichenberger@gmail.com>", | ||
"dependencies": { | ||
@@ -34,4 +23,12 @@ "chalk": "^1.1.3", | ||
"buble": "^0.15.2", | ||
"husky": "^0.13.1", | ||
"standard": "^8.6.0" | ||
} | ||
}, | ||
"keywords": [ | ||
"format", | ||
"pretty", | ||
"object", | ||
"prettify", | ||
"fmt" | ||
] | ||
} |
@@ -74,2 +74,3 @@ | ||
punctuation: chalk.cyan, | ||
annotation: chalk.red, | ||
property: chalk.yellow, | ||
@@ -89,2 +90,3 @@ literal: chalk.blue, | ||
punctuation: chalk.yellow, | ||
annotation: chalk.gray, | ||
property: chalk.green, | ||
@@ -127,2 +129,2 @@ literal: chalk.magenta, | ||
[license-image]: http://img.shields.io/npm/l/fmt-obj.svg?style=flat-square | ||
[license-url]: ./license | ||
[license-url]: ./license |
24
test.js
@@ -66,1 +66,25 @@ | ||
}) | ||
test('references', (t) => { | ||
const c = { prop: true } | ||
const actual = { | ||
a: c, | ||
b: c | ||
} | ||
const fmt = format(actual) | ||
t.true(fmt.includes('References ~a')) | ||
}) | ||
test('circular', (t) => { | ||
let actual = { | ||
body: { | ||
a: true | ||
} | ||
} | ||
actual.body.b = actual.body | ||
const fmt = format(actual) | ||
t.true(fmt.includes('References ~body')) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
76565
315
128
4
1