Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-truncate

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-truncate - npm Package Compare versions

Comparing version 1.2.2 to 1.3.0

58

dist/json-truncate.js

@@ -1,2 +0,2 @@

'use strict';
"use strict";

@@ -6,5 +6,6 @@ Object.defineProperty(exports, "__esModule", {

});
// configurables
var maxDepth = void 0;
var replace = void 0;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var flatTypes = [String, Number, Boolean];

@@ -20,9 +21,20 @@

var truncate = function truncate(obj, maxDepth, options, curDepth) {
curDepth = curDepth || 0;
maxDepth = isDefined(maxDepth) ? maxDepth : 10;
options = (typeof options === 'undefined' ? 'undefined' : _typeof(options)) === 'object' ? options : {};
options.replace = typeof options.replace === 'string' ? options.replace : undefined;
/**
* 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.
*/
var truncate = function truncate(obj) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var curDepth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
if (curDepth < maxDepth) {
options = isNaN(options) ? options : { maxDepth: options };
options.maxDepth = options.maxDepth || maxDepth;
options.replace = options.replace || replace;
if (curDepth < options.maxDepth) {
var newDepth = curDepth + 1;

@@ -38,3 +50,3 @@

} else {
newArr.push(truncate(value, maxDepth, options, newDepth));
newArr.push(truncate(value, options, newDepth));
}

@@ -49,3 +61,3 @@ });

} else {
newObj[key] = truncate(obj[key], maxDepth, options, newDepth);
newObj[key] = truncate(obj[key], options, newDepth);
}

@@ -56,5 +68,29 @@ }

}
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 = function () {
var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
maxDepth = obj.maxDepth || maxDepth;
replace = obj.replace || replace;
};
/**
* Allows you to reset the variables (mainly for testing).
*/
truncate.reset = function () {
maxDepth = 10;
replace = undefined;
};
truncate.reset();
exports.default = truncate;

13

package.json
{
"name": "json-truncate",
"version": "1.2.2",
"version": "1.3.0",
"description": "A way to truncate a json object.",

@@ -12,4 +12,6 @@ "main": "index.js",

"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",

@@ -48,9 +50,10 @@ "travis": "npm run precommit",

"babel-register": "^6.9.0",
"chai": "^3.5.0",
"chai": "^4.0.2",
"commitizen": "^2.8.2",
"cz-conventional-changelog": "^2.0.0",
"mocha": "^3.3.0",
"nyc": "^10.3.0",
"esdoc": "^0.5.2",
"mocha": "^3.4.1",
"nyc": "^11.0.1",
"rimraf": "^2.5.2",
"semantic-release": "^6.3.2",
"semantic-release": "^6.3.6",
"standard": "^10.0.2"

@@ -57,0 +60,0 @@ },

@@ -17,2 +17,8 @@ # json-truncate

## About
If you need to write data to a file or output an object to an api endpoint that has circular references I recommend you give `json-trucnate` a try.
By removing deeply nested data to maintain simple copies of the circular references you can keep most of the data you might be interested in.
## Install

@@ -26,21 +32,78 @@

Below are examples of how to use `json-truncate`
#### Including
You can include with regular node require:
```javascript
// You can add this as a static function on JSON.
JSON.truncate = require('json-truncate');
JSON.truncate = require('json-truncate')
```
console.log(JSON.truncate(SomeDeepObject, 10));
or es6 import
//OR specify a replacement string for truncated values
```javascript
import JSONTruncate from 'json-truncate'
```
console.log(JSON.truncate(SomeDeepObject, 10, {replace: '[Truncated]'}));
#### Usage
**Figure 1.0** - A basic example with default options.
```javascript
JSON.truncate(SomeDeepObject)
```
## Returns
**Figure 1.1** - An example of setting the `maxDepth` property.
You will get a proper truncated object that can now be written to a file if needed.
```javascript
JSON.truncate(SomeDeepObject, 5)
```
**Figure 1.2** - An example of all configurable options.
```javascript
console.log(JSON.truncate({
data: 'foo',
level1: {
data: 'bar',
level2: {
level3: {}
}
}
}, {
maxDepth: 2,
replace: '[Truncated]'
}))
/**
* Output:
{
"data": "foo",
"level1": {
"data": "bar",
"level2": "[Truncated]"
}
}
**/
```
### Configuration
By default, there are two configurable variables to keep in mind when using `json-truncate`:
1. `maxDepth (Number)` = `10`
2. `replace (Any)` = `undefined`
If you would you can configure these either individually with each request, or globally with the configuration function. The following example mimics figure 1.2 above.
```javascript
JSON.truncate.configure({
maxDepth: 2,
replace: '[Truncated]'
})
```
#### Arguments
* `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 `{}`.

@@ -52,2 +115,4 @@

|:--|:--|
|**maxDepth**|The default maxDepth to use for nested and deep properties on an object. Defaults to `10`|
|:--|:--|
|**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`|

@@ -54,0 +119,0 @@

@@ -1,20 +0,26 @@

'use strict'
// configurables
let maxDepth
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|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 < maxDepth) {
if (curDepth < options.maxDepth) {
const newDepth = curDepth + 1

@@ -30,3 +36,3 @@

} else {
newArr.push(truncate(value, maxDepth, options, newDepth))
newArr.push(truncate(value, options, newDepth))
}

@@ -41,3 +47,3 @@ })

} else {
newObj[key] = truncate(obj[key], maxDepth, options, newDepth)
newObj[key] = truncate(obj[key], options, newDepth)
}

@@ -48,5 +54,27 @@ }

}
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()
export default truncate
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc