Skip to content

Commit 92daddb

Browse files
committed
New : Complete code coverage
100% code coverage & throw error if not provided object to sort fn
1 parent 45e5551 commit 92daddb

File tree

10 files changed

+2254
-80
lines changed

10 files changed

+2254
-80
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
node_modules/*
2-
exclude/*
2+
exclude/*
3+
.idea/*
4+
.nyc_output/*
5+
coverage/*

.nycrc.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"cache": false,
3+
"branches": 100,
4+
"lines": 100,
5+
"functions": 100,
6+
"statements": 100,
7+
"check-coverage": true,
8+
"reporter": ["text", "lcov"],
9+
"sourceMap": true,
10+
"instrument": true
11+
}

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ node_js:
44
- 'stable'
55
install:
66
- npm install
7+
script:
8+
- npm run coverage
79
branches:
810
only:
911
- master

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ISC License
22

3-
Copyright 2018, Saksham (DawnImpulse)
3+
Copyright 2018-2019, Saksham (DawnImpulse)
44

55
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
66
provided that the above copyright notice and this permission notice appear in all copies.

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# json-keys-sort
2-
[![Build Status](https://travis-ci.org/DawnImpulse/json-keys-sort.svg?branch=master)](https://travis-ci.org/DawnImpulse/json-keys-sort) ![wait-loop](https://img.shields.io/npm/dt/json-keys-sort.svg)
2+
[![Build Status](https://travis-ci.org/DawnImpulse/json-keys-sort.svg?branch=master)](https://travis-ci.org/DawnImpulse/json-keys-sort) [![Coverage Status](https://coveralls.io/repos/github/DawnImpulse/json-keys-sort/badge.svg?branch=master)](https://coveralls.io/github/DawnImpulse/json-keys-sort?branch=master) ![wait-loop](https://img.shields.io/npm/dt/json-keys-sort.svg)
33
> Sorting a json object based on keys either ascending or descending & even recursively
44
55
### Latest Changes
6-
- Added support for Typescript
7-
### Example -
6+
- **sort** now throws an error if not provided with an object
7+
- added support for Typescript
8+
9+
### Example -
810
> Note : focus on **keys** not values
911
1012
~~~~
@@ -53,6 +55,10 @@ Output will be -
5355
+ The function work recursively and sort all the inner json objects too.
5456

5557
### Versions
58+
+ `v2.0.0`
59+
+ New : **100%** code coverage
60+
+ Improvement : **sort** now throws an error if not provided with an object
61+
5662
+ `v1.3.1`
5763
+ Bug Fix : Fixed type of parameter in sort function from JSON to object
5864

bash.exe.stackdump

Lines changed: 0 additions & 9 deletions
This file was deleted.

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,19 @@ const typeOf = function (data) {
4545
* @return {{}} - a sorted json object
4646
*/
4747
function jsonSort(data, sort) {
48+
4849
if (typeOf(data) === "ARRAY") {
4950
let newData = [];
5051
for (let w = 0; w < data.length; w++) {
5152
let d = data[w];
5253
if (typeOf(d) === "OBJECT" || typeOf(d) === "ARRAY")
5354
newData.push(jsonSort(d, sort));
54-
else
55+
else {
5556
newData.push(d)
57+
}
5658
}
5759
return newData
58-
} else {
60+
} else if (typeOf(data)=== "OBJECT") {
5961
let newKeys = [],
6062
keys,
6163
newData = {};
@@ -90,6 +92,8 @@ function jsonSort(data, sort) {
9092

9193
return newData
9294
}
95+
else
96+
throw new Error("must be an object/array")
9397
}
9498

9599
// exporting with name as sort

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
22
"name": "json-keys-sort",
3-
"version": "1.3.1",
3+
"version": "2.0.0",
44
"description": "Sorting a json object based on keys either ascending or descending & even recursively",
55
"main": "index.js",
66
"types": "index.d.ts",
77
"scripts": {
8-
"test": "mocha index.test.js"
8+
"test": "mocha",
9+
"test:coverage" : "nyc npm test",
10+
"coverage": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
911
},
1012
"repository": {
1113
"type": "git",
12-
"url": "https://github.com/DawnImpulse/json-keys-sort-js.git"
14+
"url": "https://github.com/DawnImpulse/json-keys-sort.git"
1315
},
1416
"keywords": [
1517
"json",
@@ -26,8 +28,11 @@
2628
"author": "DawnImpulse",
2729
"license": "ISC",
2830
"devDependencies": {
29-
"chai": "^4.2.0",
30-
"mocha": "^5.2.0"
31+
"chai": "4.2.0",
32+
"coveralls": "3.0.9",
33+
"mocha": "6.2.2",
34+
"nyc": "15.0.0",
35+
"rewire": "4.0.1"
3136
},
3237
"dependencies": {}
3338
}

index.test.js renamed to test/index.test.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const chai = require('chai');
2-
const index = require('./index');
2+
const rewire = require("rewire");
3+
const index = require('../index');
4+
const main = rewire('../index');
35
const expect = chai.expect;
46
chai.should();
57

@@ -357,9 +359,11 @@ describe('json array tests', () => {
357359
{
358360
dear: 3,
359361
abe: 4
360-
}
362+
},
363+
"some string"
361364
]
362-
}
365+
},
366+
"some data"
363367
];
364368
let result;
365369

@@ -377,10 +381,41 @@ describe('json array tests', () => {
377381
expect(Object.keys(obj)[0]).to.be.equal("boy")
378382
});
379383

384+
it('expect object 1.1 key 1 to be string', function () {
385+
const key = Object.keys(result[1])[0];
386+
const actual = result[1][key][1];
387+
expect(actual).to.equal("some string")
388+
});
389+
380390
it('expect object 1.0 [0] key 0 to be abe', function () {
381391
let key = Object.keys(result[1])[0];
382392
let obj = result[1][key];
383393
expect(Object.keys(obj[0])[0]).to.be.equal("abe")
384394
});
395+
396+
it('expect key 2 to be a string', function () {
397+
expect(result[2]).to.equal("some data")
398+
});
385399
})
400+
});
401+
402+
describe('sort()', () => {
403+
it('given invalid object : should throw error', function () {
404+
const given = "string";
405+
try {
406+
const should = index.sort(given);
407+
expect.fail(should);
408+
} catch (e) {
409+
expect(e.toString()).to.equal("Error: must be an object/array")
410+
}
411+
});
412+
});
413+
414+
describe('typeOf()', () => {
415+
416+
it('given any string : should return STRING', function () {
417+
const given = "any thing";
418+
const should = main.__get__("typeOf")(given);
419+
expect(should).to.equal("STRING");
420+
});
386421
});

0 commit comments

Comments
 (0)