Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Mar 16, 2022
1 parent 2d36c9a commit 793653d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
/node_modules/
/cjs/
/cjs-test/
/coverage/
.DS_Store
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ function compare(a, b, analytical) {
const typeB = partB & 7;
const lenB = partB >> 3;

/* c8 ignore next 6 */
if (debug) {
console.log({
typeA: DEBUG_TYPE_NAME[typeA], lenA, substrA: a.substr(offsetA, lenA),
Expand Down Expand Up @@ -312,6 +313,7 @@ export function naturalCompare(a, b) {
const typeB = typeof b;
let ret = 0;

/* c8 ignore next 3 */
if (debug) {
console.log('Compare', a, b);
}
Expand All @@ -320,6 +322,7 @@ export function naturalCompare(a, b) {
ret = Math.sign(compare(String(a), String(b), false));
}

/* c8 ignore next 3 */
if (debug) {
console.log('Result:', ret);
}
Expand All @@ -332,6 +335,7 @@ export function naturalAnalyticalCompare(a, b) {
const typeB = typeof b;
let ret = 0;

/* c8 ignore next 3 */
if (debug) {
console.log('Compare', a, b);
}
Expand All @@ -340,6 +344,7 @@ export function naturalAnalyticalCompare(a, b) {
ret = Math.sign(compare(String(a), String(b), true));
}

/* c8 ignore next 3 */
if (debug) {
console.log('Result:', ret);
}
Expand Down
70 changes: 70 additions & 0 deletions test/naturalAnalyticalCompare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import assert from 'assert';
import { naturalAnalyticalCompare } from '@discoveryjs/natural-compare';

function naturalSorting(array) {
return array.slice().sort(naturalAnalyticalCompare);
}

describe('naturalAnalyticalCompare', () => {
it('basic', () => {
const input = [
'1',
'a',
'2',
5,
'0',
'-1',
'str2',
'0004',
'003',
'+0',
'-0',
-10,
'-12',
'str12',
'z',
'10',
'+11',
'str1',
'str9',
'str+11',
'str0004',
'str003',
'-5',
'bbb',
'12',
'9',
'b123'
];

assert.deepEqual(naturalSorting(input), [
'12',
'+11',
'10',
'9',
5,
'0004',
'003',
'2',
'1',
'+0',
'0',
'-0',
'-1',
'-5',
-10,
'-12',
'a',
'b123',
'bbb',
'str12',
'str9',
'str0004',
'str003',
'str2',
'str1',
'str+11',
'z'
]);
});
});
26 changes: 25 additions & 1 deletion test/index.js → test/naturalCompare.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function naturalSorting(array) {
return array.slice().sort(naturalCompare);
}

describe('natural sorting', () => {
describe('naturalCompare', () => {
it('sign', () => {
const input = [
'1',
Expand Down Expand Up @@ -320,6 +320,30 @@ describe('natural sorting', () => {
]);
});

it('should support exponent part', () => {
const input = [
'-1',
'-1.01',
'-1e2',
'-1e-2',
'1',
'1.01',
'1e2',
'1e-2'
];

assert.deepEqual(naturalSorting(input), [
'-1e2',
'-1.01',
'-1',
'-1e-2',
'1e-2',
'1',
'1.01',
'1e2'
]);
});

it('should sort lexicographic orderings in ascending direction', () => {
const input = [
'40.50.60.70',
Expand Down

0 comments on commit 793653d

Please sign in to comment.