Socket
Socket
Sign inDemoInstall

json-truncate

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.1.1

11

CONTRIBUTE.md
# Contribute
Pull requests are accepted.
### Acceptance Criteria
Pull requests will be **considered** if they pass the following criteria:
1. Code passes all unit tests.
1. Code coverage is still above 90% accross the board.
If all of the above is passed, your request will be considered. Please allow
for a few days for follow-up to your request. Any issues with your request
will be posted directly to the request itself.

65

index.js
'use strict';
const flatTypes = ["string", "number", "boolean"];
const isFlat = val => {
return flatTypes.indexOf(typeof(val)) !== -1;
var flatTypes = ["string", "number", "boolean"];
var isFlat = function(val) {
return flatTypes.indexOf(typeof(val)) !== -1;
}
const truncate = (obj, maxDepth, curDepth) => {
curDepth = curDepth || 0;
if (curDepth < maxDepth) {
const newDepth = curDepth + 1;
var truncate = function(obj, maxDepth, curDepth) {
curDepth = curDepth || 0;
if (isFlat(obj)) {
return obj;
} else if (Array.isArray(obj)) {
let newObj = [];
obj.map(value => {
if (isFlat(value)) {
newObj.push(value);
if (curDepth < maxDepth) {
var newDepth = curDepth + 1;
if (isFlat(obj)) {
return obj;
} else if (Array.isArray(obj)) {
var newObj = [];
obj.map(function(value) {
if (isFlat(value)) {
newObj.push(value);
} else {
newObj.push(truncate(value, maxDepth, newDepth));
}
});
return newObj;
} else {
newObj.push(truncate(value, maxDepth, newDepth));
var newObj = {};
for (var key in obj) {
if (isFlat(obj[key])) {
newObj[key] = obj[key];
} else {
newObj[key] = truncate(obj[key], maxDepth, newDepth)
}
}
return newObj;
}
});
return newObj;
} else {
let newObj = {};
for (let key in obj) {
if (isFlat(obj[key])) {
newObj[key] = obj[key];
} else {
newObj[key] = truncate(obj[key], maxDepth, newDepth)
}
}
return newObj;
}
}
}
module.exports = (obj, maxDepth) => {
try {
module.exports = function(obj, maxDepth) {
return truncate(obj, maxDepth || 10);
} catch (e) {
console.log(e);
return {};
}
};
{
"name": "json-truncate",
"version": "1.1.0",
"version": "1.1.1",
"description": "A way to truncate a json object.",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha"
"commit": "./node_modules/.bin/git-cz",
"check-coverage": "./node_modules/.bin/istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100",
"test": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha && npm run check-coverage",
"semantic-release": "npm test && ./node_modules/.bin/semantic-release pre && npm publish && ./node_modules/.bin/semantic-release post"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mrsteele/json-truncate.git"
"url": "https://github.com/mrsteele/json-truncate.git"
},

@@ -17,2 +20,7 @@ "keywords": [

"truncate",
"shorten",
"limit",
"trim",
"prune",
"crop",
"stringify",

@@ -29,7 +37,15 @@ "parse",

"homepage": "https://github.com/mrsteele/json-truncate#readme",
"dependencies": {
"devDependencies": {
"chai": "^3.5.0",
"commitizen": "^2.8.2",
"cz-conventional-changelog": "^1.1.6",
"istanbul": "^0.4.4",
"mocha": "^2.5.3"
"mocha": "^2.5.3",
"semantic-release": "^4.3.5"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
}

@@ -1,7 +0,7 @@

'use strict'
'use strict';
// Tests suite
const chai = require('chai');
var chai = require('chai');
chai.should();
const expect = chai.expect;
var expect = chai.expect;

@@ -12,8 +12,8 @@ // The star of the show

// Helper
const createDeep = levels => {
var createDeep = function(levels) {
const createALevel = (obj, level) => {
var createALevel = function(obj, level) {
obj.bool = true;
obj.num = 10;
obj.str = `You are on level ${level}`;
obj.str = 'You are on level ' + level;
obj.arr = [true, 1, 'hi'];

@@ -24,6 +24,6 @@ obj.sub = {};

let rootobj = {};
let levelsCopy = levels;
var rootobj = {};
var levelsCopy = levels;
let refobj = rootobj;
var refobj = rootobj;
while (levelsCopy > 0) {

@@ -40,24 +40,23 @@ levelsCopy--;

//refobj.sub = undefined;
return rootobj;
};
describe('JSONtruncate', () => {
describe('JSONtruncate', function() {
describe('defaults', () => {
describe('defaults', function() {
it('should truncate to 1', () => {
it('should truncate to 1', function() {
JSON.truncate(createDeep(3), 1).should.deep.equal(createDeep(1));
});
it('should truncate to default (10)', () => {
it('should truncate to default (10)', function() {
JSON.truncate(createDeep(15)).should.deep.equal(createDeep(10));
});
it('should truncate arrays and nested objects', () => {
it('should truncate arrays and nested objects', function() {
JSON.truncate([createDeep(3)], 2).should.deep.equal([createDeep(1)]);
});
it('should return flat objects', () => {
[5, true, false, "hello"].map(val => {
it('should return flat objects', function() {
[5, true, false, "hello"].map(function(val) {
JSON.truncate(val, 5).should.equal(val);

@@ -67,13 +66,13 @@ });

it('should return an empty with anything not jsonable', () => {
it('should return an empty with anything not jsonable', function() {
JSON.truncate(function(){}, 5).should.deep.equal({});
});
it('should return an empty object with a bad maxDepth value', () => {
it('should return an empty object with a bad maxDepth value', function() {
expect(JSON.truncate({test: true}, {bad:true})).to.be.undefied;
});
it('should resolve recursive objects', () => {
it('should resolve recursive objects', function() {
// setting up a recursive object
const recursive = {
var recursive = {
test: true

@@ -80,0 +79,0 @@ };

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc