Skip to content

Commit

Permalink
Improvement: Using mocha & chai for tests + fixes with descending sort
Browse files Browse the repository at this point in the history
  • Loading branch information
DawnImpulse committed Aug 9, 2018
1 parent 6c5969a commit da3471a
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# json-keys-sort

[![Build Status](https://travis-ci.org/DawnImpulse/json-keys-sort.svg?branch=master)](https://travis-ci.org/DawnImpulse/json-keys-sort)
> Sorting a json object based on keys either ascending or descending & even recursively

Expand Down Expand Up @@ -54,6 +54,9 @@ Output will be -

### Versions

+ `v1.1.0`
+ Bug Fixed : descending sort fix
+ Improvement: auto build testing via travis-ci
+ `v1.0.1`
+ Bug Fixed : data null checks & default true fix
+ `v1.0.0`
Expand Down
9 changes: 9 additions & 0 deletions bash.exe.stackdump
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Stack trace:
Frame Function Args
000FFFF5E18 0018005CE9E (001802391AD, 0018021AC26, 000FFFF5E18, 000FFFF4D10)
000FFFF5E18 00180046559 (00000000000, 00000000000, 00000000000, 001005EDD44)
000FFFF5E18 00180046592 (00180239269, 000FFFF5CC8, 000FFFF5E18, 00000000000)
000FFFF5E18 001800A948F (00000000000, 00000000000, 00000000000, 00000000000)
000FFFF5E18 001800A96DD (000FFFF5E30, 00000000000, 00000000000, 00000000000)
000FFFF5EB0 001800AA98C (000FFFF5E30, 00000000000, 00000000000, 00000000000)
End of stack trace
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function jsonSort(data, sort) {
keys,
newData = {};

if (!sort)
if (sort === undefined)
sort = true;

keys = Object.keys(data).sort();
Expand Down
340 changes: 340 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,340 @@
const chai = require('chai');
const index = require('./index');
const expect = chai.expect;
chai.should();

describe('asc & desc tests', () => {
let json0, json1, json2, json3;
before(() => {
json0 = {
"go": 0,
"zebra": 0,
"apple": 0,
"mango": 0
};
json1 = {
"go": 0,
"zebra": {
"demons": 0,
"kettles": 0,
"blues": 0
},
"apple": 0,
"mango": 0
};
json2 = {
"go": 0,
"zebra": {
"demons": 0,
"kettles": 0,
"blues": {
"road": 0,
"monster": 0,
"useless": 0
}
},
"apple": 0,
"mango": 0
};
json3 = {
"go": 0,
"zebra": {
"demons": 0,
"kettles": 0,
"blues": {
"road": 0,
"monster": {
"ghost": 0,
"scotland": 0,
"looming": 0
},
"useless": 0
}
},
"apple": 0,
"mango": 0
}
});

describe('ascending tests', () => {
before(() => {
});

describe('1. sorting json with no nesting', () => {
let sorted;
before(() => {
sorted = index.sort(json0)
});
it('should have 4 keys', function () {
Object.keys(sorted).should.have.lengthOf(4)
});
it('should have first key apple', function () {
Object.keys(sorted)[0].should.equal("apple")
});
it('should have last key zebra', function () {
Object.keys(sorted)[3].should.equal("zebra")
});
});

describe('2. sorting json with single nested key', () => {
let sorted;
before(() => {
sorted = index.sort(json1)
});
it('should have 4 keys', function () {
Object.keys(sorted).should.have.lengthOf(4)
});
it('should have first key apple', function () {
Object.keys(sorted)[0].should.equal("apple")
});
it('should have last key zebra', function () {
Object.keys(sorted)[3].should.equal("zebra")
});
it('expect zebra to be an object', function () {
expect(sorted.zebra).to.be.an('object')
});
it('expect zebra to have length 3', function () {
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
});
it('expect zebra to have first key blues', function () {
Object.keys(sorted.zebra)[0].should.equal("blues")
});
it('expect zebra to have last key kettles', function () {
Object.keys(sorted.zebra)[2].should.equal("kettles")
});
});

describe('3. sorting json with 2 nesting in a key', () => {
let sorted;
before(() => {
sorted = index.sort(json2)
});
it('should have 4 keys', function () {
Object.keys(sorted).should.have.lengthOf(4)
});
it('should have first key apple', function () {
Object.keys(sorted)[0].should.equal("apple")
});
it('should have last key zebra', function () {
Object.keys(sorted)[3].should.equal("zebra")
});
it('expect zebra to be an object', function () {
expect(sorted.zebra).to.be.an('object')
});
it('expect zebra to have length 3', function () {
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
});
it('expect zebra to have first key blues', function () {
Object.keys(sorted.zebra)[0].should.equal("blues")
});
it('expect zebra to have last key kettles', function () {
Object.keys(sorted.zebra)[2].should.equal("kettles")
});
it('expect blues to be an object', function () {
expect(sorted.zebra.blues).to.be.an('object')
});
it('expect blues to have length 3', function () {
expect(Object.keys(sorted.zebra.blues)).to.have.lengthOf(3)
});
it('expect blues to have first key monster', function () {
Object.keys(sorted.zebra.blues)[0].should.equal("monster")
});
it('expect blues to have last key useless', function () {
Object.keys(sorted.zebra.blues)[2].should.equal("useless")
});
});

describe('4. sorting json with 3 nesting in a key', () => {
let sorted;
before(() => {
sorted = index.sort(json3)
});
it('should have 4 keys', function () {
Object.keys(sorted).should.have.lengthOf(4)
});
it('should have first key apple', function () {
Object.keys(sorted)[0].should.equal("apple")
});
it('should have last key zebra', function () {
Object.keys(sorted)[3].should.equal("zebra")
});
it('expect zebra to be an object', function () {
expect(sorted.zebra).to.be.an('object')
});
it('expect zebra to have length 3', function () {
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
});
it('expect zebra to have first key blues', function () {
Object.keys(sorted.zebra)[0].should.equal("blues")
});
it('expect zebra to have last key kettles', function () {
Object.keys(sorted.zebra)[2].should.equal("kettles")
});
it('expect blues to be an object', function () {
expect(sorted.zebra.blues).to.be.an('object')
});
it('expect blues to have length 3', function () {
expect(Object.keys(sorted.zebra.blues)).to.have.lengthOf(3)
});
it('expect blues to have first key monster', function () {
Object.keys(sorted.zebra.blues)[0].should.equal("monster")
});
it('expect blues to have last key useless', function () {
Object.keys(sorted.zebra.blues)[2].should.equal("useless")
});
it('expect monster to be an object', function () {
expect(sorted.zebra.blues.monster).to.be.an('object')
});
it('expect monster to have length 3', function () {
expect(Object.keys(sorted.zebra.blues.monster)).to.have.lengthOf(3)
});
it('expect monster to have first key ghost', function () {
Object.keys(sorted.zebra.blues.monster)[0].should.equal("ghost")
});
it('expect monster to have last key scotland', function () {
Object.keys(sorted.zebra.blues.monster)[2].should.equal("scotland")
});
});
});

describe('descending tests', () => {
before(() => {
});

describe('1. sorting json with no nesting', () => {
let sorted;
before(() => {
sorted = index.sort(json0,false);
});
it('should have 4 keys', function () {
Object.keys(sorted).should.have.lengthOf(4)
});
it('should have first key zebra', function () {
Object.keys(sorted)[0].should.equal("zebra")
});
it('should have last key apple', function () {
Object.keys(sorted)[3].should.equal("apple")
});
});

describe('2. sorting json with single nested key', () => {
let sorted;
before(() => {
sorted = index.sort(json1,false)
});
it('should have 4 keys', function () {
Object.keys(sorted).should.have.lengthOf(4)
});
it('should have first key zebra', function () {
Object.keys(sorted)[0].should.equal("zebra")
});
it('should have last key apple', function () {
Object.keys(sorted)[3].should.equal("apple")
});
it('expect zebra to be an object', function () {
expect(sorted.zebra).to.be.an('object')
});
it('expect zebra to have length 3', function () {
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
});
it('expect zebra to have first key kettles', function () {
Object.keys(sorted.zebra)[0].should.equal("kettles")
});
it('expect zebra to have last key blues', function () {
Object.keys(sorted.zebra)[2].should.equal("blues")
});
});

describe('3. sorting json with 2 nesting in a key', () => {
let sorted;
before(() => {
sorted = index.sort(json2,false)
});
it('should have 4 keys', function () {
Object.keys(sorted).should.have.lengthOf(4)
});
it('should have first key zebra', function () {
Object.keys(sorted)[0].should.equal("zebra")
});
it('should have last key apple', function () {
Object.keys(sorted)[3].should.equal("apple")
});
it('expect zebra to be an object', function () {
expect(sorted.zebra).to.be.an('object')
});
it('expect zebra to have length 3', function () {
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
});
it('expect zebra to have first key kettles', function () {
Object.keys(sorted.zebra)[0].should.equal("kettles")
});
it('expect zebra to have last key blues', function () {
Object.keys(sorted.zebra)[2].should.equal("blues")
});
it('expect blues to be an object', function () {
expect(sorted.zebra.blues).to.be.an('object')
});
it('expect blues to have length 3', function () {
expect(Object.keys(sorted.zebra.blues)).to.have.lengthOf(3)
});
it('expect blues to have first key useless', function () {
Object.keys(sorted.zebra.blues)[0].should.equal("useless")
});
it('expect blues to have last key monster', function () {
Object.keys(sorted.zebra.blues)[2].should.equal("monster")
});
});

describe('4. sorting json with 3 nesting in a key', () => {
let sorted;
before(() => {
sorted = index.sort(json3,false)
});
it('should have 4 keys', function () {
Object.keys(sorted).should.have.lengthOf(4)
});
it('should have first key zebra', function () {
Object.keys(sorted)[0].should.equal("zebra")
});
it('should have last key apple', function () {
Object.keys(sorted)[3].should.equal("apple")
});
it('expect zebra to be an object', function () {
expect(sorted.zebra).to.be.an('object')
});
it('expect zebra to have length 3', function () {
expect(Object.keys(sorted.zebra)).to.have.lengthOf(3)
});
it('expect zebra to have first key kettles', function () {
Object.keys(sorted.zebra)[0].should.equal("kettles")
});
it('expect zebra to have last key blues', function () {
Object.keys(sorted.zebra)[2].should.equal("blues")
});
it('expect blues to be an object', function () {
expect(sorted.zebra.blues).to.be.an('object')
});
it('expect blues to have length 3', function () {
expect(Object.keys(sorted.zebra.blues)).to.have.lengthOf(3)
});
it('expect blues to have first key useless', function () {
Object.keys(sorted.zebra.blues)[0].should.equal("useless")
});
it('expect blues to have last key monster', function () {
Object.keys(sorted.zebra.blues)[2].should.equal("monster")
});
it('expect monster to be an object', function () {
expect(sorted.zebra.blues.monster).to.be.an('object')
});
it('expect monster to have length 3', function () {
expect(Object.keys(sorted.zebra.blues.monster)).to.have.lengthOf(3)
});
it('expect monster to have first key scotland', function () {
Object.keys(sorted.zebra.blues.monster)[0].should.equal("scotland")
});
it('expect monster to have last key ghost', function () {
Object.keys(sorted.zebra.blues.monster)[2].should.equal("ghost")
});
});
});
});
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "1.0.1",
"description": "Sorting a json object based on keys either ascending or descending & even recursively",
"main": "index.js",
"scripts": {
"test": "mocha index.test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/DawnImpulse/json-keys-sort-js.git"
Expand All @@ -15,5 +18,9 @@
"key-sort-json"
],
"author": "DawnImpulse",
"license": "ISC"
"license": "ISC",
"devDependencies": {
"chai": "4.1.2",
"mocha": "5.2.0"
}
}

0 comments on commit da3471a

Please sign in to comment.