alphabetize-object-keys
Advanced tools
Comparing version 1.0.4 to 2.0.0
32
index.js
@@ -10,3 +10,3 @@ /** | ||
* alphabetize: require('alphabetize-object-keys') | ||
* }); | ||
* }) | ||
* var obj = { | ||
@@ -16,13 +16,17 @@ * c: {b:1,a:2}, | ||
* a: 2 | ||
* }; | ||
* } | ||
* JSON.stringify(_.alphabetize(obj)) === '{"a":2,"b":1,"c":{"a":2,"b":1}}' | ||
*/ | ||
var _ = require('lodash'); | ||
var _ = require('lodash') | ||
var alphabetize = function(object) { | ||
// only act on objects! string, number, array return unchanged | ||
if(_.isArray(object) || _.isFunction(object) || !_.isObject(object)){ | ||
return object | ||
} | ||
var sortedObj = {}, | ||
keys = _.keys(object); | ||
keys = _.keys(object) | ||
keys = _.sortBy(keys, function(key){ | ||
return key; | ||
}); | ||
return key | ||
}) | ||
@@ -32,13 +36,13 @@ _.each(keys, function(key) { | ||
sortedObj[key] = object[key].map(function(val) { | ||
return _.isObject(val) ? alphabetize(val) : val; | ||
}); | ||
return _.isObject(val) ? alphabetize(val) : val | ||
}) | ||
} else if(_.isObject(object[key])){ | ||
sortedObj[key] = alphabetize(object[key]); | ||
sortedObj[key] = alphabetize(object[key]) | ||
} else { | ||
sortedObj[key] = object[key]; | ||
sortedObj[key] = object[key] | ||
} | ||
}); | ||
}) | ||
return sortedObj; | ||
}; | ||
module.exports = alphabetize; | ||
return sortedObj | ||
} | ||
module.exports = alphabetize |
{ | ||
"name": "alphabetize-object-keys", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"description": "deeply alphabetize object keys", | ||
@@ -32,8 +32,8 @@ "main": "index.js", | ||
"devDependencies": { | ||
"chai": "3.5.0", | ||
"mocha": "3.2.0" | ||
"chai": "4.2.0", | ||
"mocha": "5.2.0" | ||
}, | ||
"dependencies": { | ||
"lodash": "4.17.4" | ||
"lodash": "4.17.11" | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
[![Beerpay](https://beerpay.io/atomantic/alphabetize-object-keys/badge.svg?style=flat-square)](https://beerpay.io/atomantic/alphabetize-object-keys) | ||
[![](https://travis-ci.org/atomantic/alphabetize-object-keys.svg?branch=master)](https://travis-ci.org/atomantic/alphabetize-object-keys) | ||
@@ -61,1 +60,4 @@ [![](https://img.shields.io/npm/dm/alphabetize-object-keys.svg?style=flat)](https://www.npmjs.org/package/alphabetize-object-keys) | ||
``` | ||
# Why sort object keys? | ||
Even though objects are dynamically arranged in memory and accessible by key, when you JSON.stringify an object, you may want the output to be alphabetically organized. In my case, I built this library to validate automated gamut tests against an API where I bombarded an API with fuzzy variations from a JSON data file. My tests were then able to determine that the right output was achieved by generating the expected response and comparing the the stringified output of the API vs the spec. Additionally, the output of these tests get committed to the repo in an output folder full of JSON files. This library eliminated ugly commits where nothing changed but the ordering of the object keys, while we maintained visibility into changes to spec adherance. |
@@ -1,19 +0,37 @@ | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
var alphabetize = require('../index.js'); | ||
describe('alphabetize', function() { | ||
var chai = require('chai') | ||
var expect = chai.expect | ||
var alphabetize = require('../index.js') | ||
var _ = require('lodash') | ||
describe('Alphabetize Object Keys', function() { | ||
it('should alphabetize simple object', function() { | ||
var obj = {b:1,a:2,d:3,c:4}; | ||
var result = alphabetize(obj); | ||
var string = JSON.stringify(result); | ||
expect(string).to.equal('{"a":2,"b":1,"c":4,"d":3}'); | ||
}); | ||
var obj = {b:1,a:2,d:3,c:4} | ||
var result = alphabetize(obj) | ||
var string = JSON.stringify(result) | ||
expect(string).to.equal('{"a":2,"b":1,"c":4,"d":3}') | ||
}) | ||
it('should alphabetize deep object', function() { | ||
var obj = {b:1,a:2,d:{b:1,d:{g:1,f:2},c:3,a:4},c:4}; | ||
var result = alphabetize(obj); | ||
var string = JSON.stringify(result); | ||
expect(string).to.equal('{"a":2,"b":1,"c":4,"d":{"a":4,"b":1,"c":3,"d":{"f":2,"g":1}}}'); | ||
}); | ||
}); | ||
var obj = {b:1,a:2,d:{b:1,d:{g:1,f:2},c:3,a:4},c:4} | ||
var result = alphabetize(obj) | ||
var string = JSON.stringify(result) | ||
expect(string).to.equal('{"a":2,"b":1,"c":4,"d":{"a":4,"b":1,"c":3,"d":{"f":2,"g":1}}}') | ||
}) | ||
}) | ||
describe('Data Validation', function(){ | ||
var unchanged = { | ||
array: [5,2,3], | ||
boolean: true, | ||
string: 'this is a test', | ||
number: 653, | ||
null: null, | ||
undefined: undefined, | ||
NaN: NaN, | ||
function: function(){} | ||
} | ||
_.forEach(unchanged, function(val, key){ | ||
it('should not alter '+key+' parameters', function() { | ||
expect(alphabetize(val)).to.deep.equal(val) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
6372
77
63
+ Addedlodash@4.17.11(transitive)
- Removedlodash@4.17.4(transitive)
Updatedlodash@4.17.11