Skip to content

Commit

Permalink
feat: Added the ability to populate truncated data with a default value.
Browse files Browse the repository at this point in the history
Add optional replacement value for truncated values
  • Loading branch information
mrsteele authored Jan 11, 2017
2 parents c0fb13f + 41e5032 commit 6f9c869
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ npm install json-truncate --save
JSON.truncate = require('json-truncate');

console.log(JSON.truncate(SomeDeepObject, 10));

//OR specify a replacement string for truncated values

console.log(JSON.truncate(SomeDeepObject, 10, {replace: '[Truncated]'}));
```

## Returns
Expand All @@ -36,6 +40,13 @@ You will get a proper truncated object that can now be written to a file if need

* `obj` - The Object that will be truncated.
* `maxDepth` - (optional) The depth at which to stop building the valid json. Defaults to `10`.
* `options` - (optional) An option object to customize the behavior of the utility. Defaults to `{}`.

**Current Option Properties**

|Option|Description|
|:--|:--|
|**replace**|A string value that is used to replace all truncated values. If this value is not a string then all truncated values will be replaced with `undefined`|


## Licence
Expand Down
9 changes: 6 additions & 3 deletions src/json-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ const isFlat = val => {
return !isDefined(val) || flatTypes.indexOf(val.constructor) !== -1
}

const truncate = (obj, maxDepth, curDepth) => {
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

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

export default truncate
16 changes: 13 additions & 3 deletions test/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import dist from '../dist/json-truncate'
const entry = require('../')

// Helper
const createDeep = levels => {
const createDeep = (levels, replace) => {
const createALevel = (obj, level) => {
obj.bool = true
obj.num = 10
Expand All @@ -35,8 +35,8 @@ const createDeep = levels => {
if (levelsCopy > 0) {
refobj = refobj.sub
} else {
refobj.sub = undefined
refobj.arr = undefined
refobj.sub = replace
refobj.arr = replace
}
}

Expand All @@ -57,6 +57,16 @@ const createTestsFor = (m, name) => {
m([createDeep(3)], 2).should.deep.equal([createDeep(1)])
})

it('should truncate arrays and nested objects with replacement string', () => {
const replacement = '[replaced]'
m([createDeep(3, replacement)], 2, {replace: replacement}).should.deep.equal([createDeep(1, replacement)])
})

it('should replace truncated values with undefined when replace prop is not a string', () => {
const replacement = 3
m([createDeep(3)], 2, {replace: replacement}).should.deep.equal([createDeep(1)])
})

it('should return flat objects', () => {
;[5, true, false, 'hello'].map(val => {
m(val, 5).should.equal(val)
Expand Down

0 comments on commit 6f9c869

Please sign in to comment.