Skip to content

Commit

Permalink
feat: Cleaning up the API to support more options.
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
mrsteele committed Jul 17, 2017
1 parent 31c6fba commit c1bfe59
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
docs

# node-waf configuration
.lock-wscript
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock = false
8 changes: 8 additions & 0 deletions doc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"source": "./src",
"destination": "./docs",
"test": {
"type": "mocha",
"source": "./test"
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"lint": "standard",
"coverage": "nyc npm t && npm run coverage-report",
"coverage-report": "nyc report --reporter=lcov",
"doc": "esdoc -c doc.json",
"test": "npm run build && _mocha --compilers js:babel-register",
"prebuild": "rimraf dist",
"predoc": "rimraf docs",
"build": "babel --copy-files --out-dir dist src",
"travis": "npm run precommit",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
Expand Down Expand Up @@ -47,6 +49,7 @@
"chai": "^4.0.2",
"commitizen": "^2.8.2",
"cz-conventional-changelog": "^2.0.0",
"esdoc": "^0.5.2",
"mocha": "^3.4.1",
"nyc": "^11.0.1",
"rimraf": "^2.5.2",
Expand Down
47 changes: 32 additions & 15 deletions src/json-truncate.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
'use strict'
// configurables
let maxDepth = 10
let replace

const flatTypes = [String, Number, Boolean]

const isDefined = val => {
return val !== null && val !== undefined
}
const isDefined = val => val !== null && val !== undefined

const isFlat = val => {
return !isDefined(val) || flatTypes.indexOf(val.constructor) !== -1
}
const isFlat = val => !isDefined(val) || flatTypes.indexOf(val.constructor) !== -1

const truncate = (obj, maxDepth, options, curDepth) => {
curDepth = curDepth || 0
maxDepth = (isDefined(maxDepth)) ? maxDepth : 10
options = (typeof options === 'object') ? options : {}
options.replace = (typeof options.replace === 'string') ? options.replace : undefined
/**
* Truncates variables.
* @param {Object} obj - The object to truncate.
* @param {Object=} options - Configurable options.
* @param {Number=} [options.maxDepth=10] - The max depth to build.
* @param {Object=} options.replace - What to replace the truncated reference to.
* @param {Number} [curDepth=0] - The current depth (used for recursive requests).
* @returns {Object} The truncated object.
*/
const truncate = (obj, options = {}, curDepth = 0) => {
options.maxDepth = options.maxDepth || maxDepth
options.replace = options.replace || replace

if (curDepth < maxDepth) {
if (curDepth < options.max) {
const newDepth = curDepth + 1

if (isFlat(obj)) {
Expand All @@ -27,7 +32,7 @@ const truncate = (obj, maxDepth, options, curDepth) => {
if (isFlat(value)) {
newArr.push(value)
} else {
newArr.push(truncate(value, maxDepth, options, newDepth))
newArr.push(truncate(value, options, newDepth))
}
})
return newArr
Expand All @@ -37,13 +42,25 @@ const truncate = (obj, maxDepth, options, curDepth) => {
if (isFlat(obj[key])) {
newObj[key] = obj[key]
} else {
newObj[key] = truncate(obj[key], maxDepth, options, newDepth)
newObj[key] = truncate(obj[key], options, newDepth)
}
}
return newObj
}
}

return options.replace
}

/**
* Configures globals and defaults.
* @param {Object=} obj - The configuration.
* @param {Number} obj.maxDepth - The default and global maxDepth for future truncations.
* @param {} obj.replace - The default and global replacement value.
*/
export const configure = (obj = {}) => {
maxDepth = obj.maxDepth || maxDepth
replace = obj.replace || replace
}

export default truncate

0 comments on commit c1bfe59

Please sign in to comment.