-
Notifications
You must be signed in to change notification settings - Fork 6
/
json-truncate.js
79 lines (67 loc) · 2.07 KB
/
json-truncate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict'
// configurables
let maxDepth
let replace
const flatTypes = [String, Number, Boolean]
const isDefined = val => val !== null && val !== undefined
const isFlat = val => !isDefined(val) || ~flatTypes.indexOf(val.constructor)
/**
* Truncates variables.
* @param {Object} obj - The object to truncate.
* @param {Object|Number} [options={}] - If a number, the maxDepth, otherwise 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 = isNaN(options) ? options : { maxDepth: options }
options.maxDepth = options.maxDepth || maxDepth
options.replace = options.replace || replace
if (curDepth < options.maxDepth) {
const newDepth = curDepth + 1
if (isFlat(obj)) {
return obj
} else if (Array.isArray(obj)) {
const newArr = []
obj.forEach(value => {
if (isFlat(value)) {
newArr.push(value)
} else {
newArr.push(truncate(value, options, newDepth))
}
})
return newArr
} else {
const newObj = {}
for (const key in obj) {
if (isFlat(obj[key])) {
newObj[key] = obj[key]
} else {
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.
*/
truncate.config = (obj = {}) => {
maxDepth = obj.maxDepth || maxDepth
replace = obj.replace || replace
}
/**
* Allows you to reset the variables (mainly for testing).
*/
truncate.reset = () => {
maxDepth = 10
replace = undefined
}
truncate.reset()
module.exports = truncate