json-truncate
Advanced tools
Comparing version 1.1.0 to 1.1.1
# 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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8857
0
6
10
104
- Removedchai@^3.5.0
- Removedistanbul@^0.4.4
- Removedmocha@^2.5.3
- Removedabbrev@1.0.9(transitive)
- Removedamdefine@1.0.1(transitive)
- Removedargparse@1.0.10(transitive)
- Removedassertion-error@1.1.0(transitive)
- Removedasync@1.5.2(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedchai@3.5.0(transitive)
- Removedcommander@0.6.12.3.0(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removeddebug@2.2.0(transitive)
- Removeddeep-eql@0.1.3(transitive)
- Removeddeep-is@0.1.4(transitive)
- Removeddiff@1.4.0(transitive)
- Removedescape-string-regexp@1.0.2(transitive)
- Removedescodegen@1.8.1(transitive)
- Removedesprima@2.7.34.0.1(transitive)
- Removedestraverse@1.9.3(transitive)
- Removedesutils@2.0.3(transitive)
- Removedfast-levenshtein@2.0.6(transitive)
- Removedglob@3.2.115.0.15(transitive)
- Removedgrowl@1.9.2(transitive)
- Removedhandlebars@4.7.8(transitive)
- Removedhas-flag@1.0.0(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisexe@2.0.0(transitive)
- Removedistanbul@0.4.5(transitive)
- Removedjade@0.26.3(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedlevn@0.3.0(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedminimatch@0.3.03.1.2(transitive)
- Removedminimist@0.0.81.2.8(transitive)
- Removedmkdirp@0.3.00.5.10.5.6(transitive)
- Removedmocha@2.5.3(transitive)
- Removedms@0.7.1(transitive)
- Removedneo-async@2.6.2(transitive)
- Removednopt@3.0.6(transitive)
- Removedonce@1.4.0(transitive)
- Removedoptionator@0.8.3(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedprelude-ls@1.1.2(transitive)
- Removedresolve@1.1.7(transitive)
- Removedsigmund@1.0.1(transitive)
- Removedsource-map@0.2.00.6.1(transitive)
- Removedsprintf-js@1.0.3(transitive)
- Removedsupports-color@1.2.03.2.3(transitive)
- Removedto-iso-string@0.0.2(transitive)
- Removedtype-check@0.3.2(transitive)
- Removedtype-detect@0.1.11.0.0(transitive)
- Removeduglify-js@3.19.3(transitive)
- Removedwhich@1.3.1(transitive)
- Removedword-wrap@1.2.5(transitive)
- Removedwordwrap@1.0.0(transitive)
- Removedwrappy@1.0.2(transitive)