Skip to content

Commit 6f9c869

Browse files
authored
feat: Added the ability to populate truncated data with a default value.
Add optional replacement value for truncated values
2 parents c0fb13f + 41e5032 commit 6f9c869

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ npm install json-truncate --save
2626
JSON.truncate = require('json-truncate');
2727

2828
console.log(JSON.truncate(SomeDeepObject, 10));
29+
30+
//OR specify a replacement string for truncated values
31+
32+
console.log(JSON.truncate(SomeDeepObject, 10, {replace: '[Truncated]'}));
2933
```
3034

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

3741
* `obj` - The Object that will be truncated.
3842
* `maxDepth` - (optional) The depth at which to stop building the valid json. Defaults to `10`.
43+
* `options` - (optional) An option object to customize the behavior of the utility. Defaults to `{}`.
44+
45+
**Current Option Properties**
46+
47+
|Option|Description|
48+
|:--|:--|
49+
|**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`|
3950

4051

4152
## Licence

src/json-truncate.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ const isFlat = val => {
1010
return !isDefined(val) || flatTypes.indexOf(val.constructor) !== -1
1111
}
1212

13-
const truncate = (obj, maxDepth, curDepth) => {
13+
const truncate = (obj, maxDepth, options, curDepth) => {
1414
curDepth = curDepth || 0
1515
maxDepth = (isDefined(maxDepth)) ? maxDepth : 10
16+
options = (typeof options === 'object') ? options : {}
17+
options.replace = (typeof options.replace === 'string') ? options.replace : undefined
1618

1719
if (curDepth < maxDepth) {
1820
const newDepth = curDepth + 1
@@ -25,7 +27,7 @@ const truncate = (obj, maxDepth, curDepth) => {
2527
if (isFlat(value)) {
2628
newArr.push(value)
2729
} else {
28-
newArr.push(truncate(value, maxDepth, newDepth))
30+
newArr.push(truncate(value, maxDepth, options, newDepth))
2931
}
3032
})
3133
return newArr
@@ -35,12 +37,13 @@ const truncate = (obj, maxDepth, curDepth) => {
3537
if (isFlat(obj[key])) {
3638
newObj[key] = obj[key]
3739
} else {
38-
newObj[key] = truncate(obj[key], maxDepth, newDepth)
40+
newObj[key] = truncate(obj[key], maxDepth, options, newDepth)
3941
}
4042
}
4143
return newObj
4244
}
4345
}
46+
return options.replace
4447
}
4548

4649
export default truncate

test/mocha.spec.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import dist from '../dist/json-truncate'
1313
const entry = require('../')
1414

1515
// Helper
16-
const createDeep = levels => {
16+
const createDeep = (levels, replace) => {
1717
const createALevel = (obj, level) => {
1818
obj.bool = true
1919
obj.num = 10
@@ -35,8 +35,8 @@ const createDeep = levels => {
3535
if (levelsCopy > 0) {
3636
refobj = refobj.sub
3737
} else {
38-
refobj.sub = undefined
39-
refobj.arr = undefined
38+
refobj.sub = replace
39+
refobj.arr = replace
4040
}
4141
}
4242

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

60+
it('should truncate arrays and nested objects with replacement string', () => {
61+
const replacement = '[replaced]'
62+
m([createDeep(3, replacement)], 2, {replace: replacement}).should.deep.equal([createDeep(1, replacement)])
63+
})
64+
65+
it('should replace truncated values with undefined when replace prop is not a string', () => {
66+
const replacement = 3
67+
m([createDeep(3)], 2, {replace: replacement}).should.deep.equal([createDeep(1)])
68+
})
69+
6070
it('should return flat objects', () => {
6171
;[5, true, false, 'hello'].map(val => {
6272
m(val, 5).should.equal(val)

0 commit comments

Comments
 (0)