Skip to content

Commit

Permalink
refactor!: Update code to use ES2015 syntax
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This module will need to be transpiled if used in browsers that do not support ES2015 syntax.
  • Loading branch information
nwoltman committed Oct 26, 2019
1 parent 8fa4781 commit 6d9d8c0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 62 deletions.
9 changes: 3 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
{
"root": true,
"extends": "@nwoltman/eslint-config",
"rules": {
"object-shorthand": 0,
"padded-blocks": 0,
"prefer-arrow-callback": 0
},
"overrides": [
{
"files": ["test/*.js"],
Expand All @@ -15,7 +10,9 @@
"rules": {
"brace-style": 0,
"max-len": 0,
"max-nested-callbacks": 0
"max-nested-callbacks": 0,
"padded-blocks": 0,
"prefer-arrow-callback": 0
}
}
]
Expand Down
52 changes: 26 additions & 26 deletions benchmark/run.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
/* eslint-disable no-console */
'use strict';

var Benchmark = require('benchmark');
const Benchmark = require('benchmark');

var naturalCompareMaster = require('string-natural-compare');
var naturalCompareLocal = require('../');
const naturalCompareMaster = require('string-natural-compare');
const naturalCompareLocal = require('../');

var config = new Set(
const config = new Set(
process.argv.length > 2 ? process.argv[2].split(',') : '1234567'
);

var suite = new Benchmark.Suite();
const suite = new Benchmark.Suite();

if (config.has('1')) {
suite
.add('1) no numbers master', function() {
.add('1) no numbers master', () => {
naturalCompareMaster('fileA.txt', 'fileB.txt');
naturalCompareMaster('fileB.txt', 'fileA.txt');
})
.add('1) no numbers local', function() {
.add('1) no numbers local', () => {
naturalCompareLocal('fileA.txt', 'fileB.txt');
naturalCompareLocal('fileB.txt', 'fileA.txt');
});
}

if (config.has('2')) {
suite
.add('2) common numbers different lengths master', function() {
.add('2) common numbers different lengths master', () => {
naturalCompareMaster('2.txt', '10.txt');
naturalCompareMaster('10.txt', '2.txt');
})
.add('2) common numbers different lengths local', function() {
.add('2) common numbers different lengths local', () => {
naturalCompareLocal('2.txt', '10.txt');
naturalCompareLocal('10.txt', '2.txt');
});
}

if (config.has('3')) {
suite
.add('3) common numbers same length master', function() {
.add('3) common numbers same length master', () => {
naturalCompareMaster('01.txt', '05.txt');
naturalCompareMaster('05.txt', '01.txt');
})
.add('3) common numbers same length local', function() {
.add('3) common numbers same length local', () => {
naturalCompareLocal('01.txt', '05.txt');
naturalCompareLocal('05.txt', '01.txt');
});
}

if (config.has('4')) {
suite
.add('4) big numbers different lengths master', function() {
.add('4) big numbers different lengths master', () => {
naturalCompareMaster(
'1165874568735487968325787328996865',
'265812277985321589735871687040841'
Expand All @@ -60,7 +60,7 @@ if (config.has('4')) {
'1165874568735487968325787328996865'
);
})
.add('4) big numbers different lengths local', function() {
.add('4) big numbers different lengths local', () => {
naturalCompareLocal(
'1165874568735487968325787328996865',
'265812277985321589735871687040841'
Expand All @@ -74,7 +74,7 @@ if (config.has('4')) {

if (config.has('5')) {
suite
.add('5) big numbers same length master', function() {
.add('5) big numbers same length master', () => {
naturalCompareMaster(
'1165874568735487968325787328996865',
'1165874568735487989735871687040841'
Expand All @@ -84,7 +84,7 @@ if (config.has('5')) {
'1165874568735487968325787328996865'
);
})
.add('5) big numbers same length local', function() {
.add('5) big numbers same length local', () => {
naturalCompareLocal(
'1165874568735487968325787328996865',
'1165874568735487989735871687040841'
Expand All @@ -98,7 +98,7 @@ if (config.has('5')) {

if (suite.length) {
suite
.on('cycle', function(event) {
.on('cycle', (event) => {
console.log(String(event.target));
})
.run();
Expand All @@ -107,35 +107,35 @@ if (suite.length) {
naturalCompareMaster.alphabet = 'ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy';
naturalCompareLocal.alphabet = 'ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy';

suite = new Benchmark.Suite();
const alphabetSuite = new Benchmark.Suite();

if (config.has('6')) {
suite
.add('6) custom alphabet included characters master', function() {
alphabetSuite
.add('6) custom alphabet included characters master', () => {
naturalCompareMaster('š.txt', 'z.txt');
naturalCompareMaster('z.txt', 'š.txt');
})
.add('6) custom alphabet included characters local', function() {
.add('6) custom alphabet included characters local', () => {
naturalCompareLocal('š.txt', 'z.txt');
naturalCompareLocal('z.txt', 'š.txt');
});
}

if (config.has('7')) {
suite
.add('7) custom alphabet missing characters master', function() {
alphabetSuite
.add('7) custom alphabet missing characters master', () => {
naturalCompareMaster('é.txt', 'à.txt');
naturalCompareMaster('à.txt', 'é.txt');
})
.add('7) custom alphabet missing characters local', function() {
.add('7) custom alphabet missing characters local', () => {
naturalCompareLocal('é.txt', 'à.txt');
naturalCompareLocal('à.txt', 'é.txt');
});
}

if (suite.length) {
suite
.on('cycle', function(event) {
if (alphabetSuite.length) {
alphabetSuite
.on('cycle', (event) => {
console.log(String(event.target));
})
.run();
Expand Down
4 changes: 2 additions & 2 deletions natural-compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ naturalCompare.caseInsensitive = naturalCompare.i = function(a, b) {

Object.defineProperties(naturalCompare, {
alphabet: {
get: function() {
get() {
return alphabet;
},

set: function(value) {
set(value) {
alphabet = value;
alphabetIndexMap = [];

Expand Down
56 changes: 28 additions & 28 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

require('should');

var naturalCompare = require('../');
const naturalCompare = require('../');

function verify(testData) {
var a = testData[0];
var b = testData[2];
var failMessage = 'failure on input: [' + testData.join(' ') + ']';
const a = testData[0];
const b = testData[2];
const failMessage = 'failure on input: [' + testData.join(' ') + ']';

switch (testData[1]) {
case '=':
Expand All @@ -27,9 +27,9 @@ function verify(testData) {
}
}

describe('naturalCompare() and naturalCompare.caseInsensitive()', function() {
describe('naturalCompare() and naturalCompare.caseInsensitive()', () => {

it('should compare strings that do not contain numbers', function() {
it('should compare strings that do not contain numbers', () => {
[
['a', '=', 'a'],
['a', '<', 'b'],
Expand All @@ -45,7 +45,7 @@ describe('naturalCompare() and naturalCompare.caseInsensitive()', function() {
].forEach(verify);
});

it('should compare integer substrings by their numeric value', function() {
it('should compare integer substrings by their numeric value', () => {
[
['1', '=', '1'],
['50', '=', '50'],
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('naturalCompare() and naturalCompare.caseInsensitive()', function() {
].forEach(verify);
});

it('should work with 0 in the string', function() {
it('should work with 0 in the string', () => {
[
['a00', '<', 'a000'],
['a 0 a', '<', 'a 0 b'],
Expand All @@ -88,7 +88,7 @@ describe('naturalCompare() and naturalCompare.caseInsensitive()', function() {
].forEach(verify);
});

it('should compare integer substrings with leading 0s by their numeric value', function() {
it('should compare integer substrings with leading 0s by their numeric value', () => {
[
['000', '=', '000'],
['001', '=', '001'],
Expand All @@ -99,7 +99,7 @@ describe('naturalCompare() and naturalCompare.caseInsensitive()', function() {
].forEach(verify);
});

it('should not consider a decimal point surrounded by integers as a floating point number', function() {
it('should not consider a decimal point surrounded by integers as a floating point number', () => {
[
['0.01', '<', '0.001'],
['0.001', '>', '0.01'],
Expand All @@ -108,7 +108,7 @@ describe('naturalCompare() and naturalCompare.caseInsensitive()', function() {
].forEach(verify);
});

it('should not consider an integer preceeded by a minus sign as a negative number', function() {
it('should not consider an integer preceeded by a minus sign as a negative number', () => {
[
['-1', '<', '-2'],
['-2', '<', '-10'],
Expand All @@ -118,7 +118,7 @@ describe('naturalCompare() and naturalCompare.caseInsensitive()', function() {
].forEach(verify);
});

it('should compare non-string inputs as strings', function() {
it('should compare non-string inputs as strings', () => {
[
[1, '<', 2],
[2, '>', 1],
Expand All @@ -127,14 +127,14 @@ describe('naturalCompare() and naturalCompare.caseInsensitive()', function() {
[null, '<', undefined],
[{}, '=', {}],
[
{toString: function() { return 'a'; }},
{toString: () => 'a'},
'<',
{toString: function() { return 'b'; }},
{toString: () => 'b'},
],
].forEach(verify);
});

it('should correctly compare strings containing very large numbers', function() {
it('should correctly compare strings containing very large numbers', () => {
[
[
'1165874568735487968325787328996864',
Expand All @@ -157,14 +157,14 @@ describe('naturalCompare() and naturalCompare.caseInsensitive()', function() {
});


describe('naturalCompare()', function() {
describe('naturalCompare()', () => {

it('should perform case-sensitive comparisons', function() {
it('should perform case-sensitive comparisons', () => {
naturalCompare('a', 'A').should.be.greaterThan(0);
naturalCompare('b', 'C').should.be.greaterThan(0);
});

it('should function correctly as the callback to array.sort()', function() {
it('should function correctly as the callback to array.sort()', () => {
['a', 'c', 'b', 'd']
.sort(naturalCompare)
.should.deepEqual(['a', 'b', 'c', 'd']);
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('naturalCompare()', function() {
]);
});

it('should compare strings using the provided alphabet', function() {
it('should compare strings using the provided alphabet', () => {
naturalCompare.alphabet = 'ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy';

['Д', 'a', 'ä', 'B', 'Š', 'X', 'A', 'õ', 'u', 'z', '1', '2', '9', '10']
Expand All @@ -215,20 +215,20 @@ describe('naturalCompare()', function() {
});


describe('naturalCompare.caseInsensitive()', function() {
describe('naturalCompare.caseInsensitive()', () => {

it('should perform case-insensitive comparisons', function() {
it('should perform case-insensitive comparisons', () => {
naturalCompare.caseInsensitive('a', 'A').should.equal(0);
naturalCompare.caseInsensitive('b', 'C').should.be.lessThan(0);
});

it('should function correctly as the callback to array.sort()', function() {
it('should function correctly as the callback to array.sort()', () => {
['C', 'B', 'a', 'd']
.sort(naturalCompare.caseInsensitive)
.should.deepEqual(['a', 'B', 'C', 'd']);
});

it('should compare strings using the provided alphabet', function() {
it('should compare strings using the provided alphabet', () => {
naturalCompare.alphabet = 'ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy';

['Д', 'a', 'ä', 'B', 'Š', 'X', 'Ü', 'õ', 'u', 'z', '1', '2', '9', '10']
Expand All @@ -239,23 +239,23 @@ describe('naturalCompare.caseInsensitive()', function() {
});


describe('naturalCompare.i', function() {
describe('naturalCompare.i', () => {

it('is an alias for naturalCompare.caseInsensitive', function() {
it('is an alias for naturalCompare.caseInsensitive', () => {
naturalCompare.i.should.equal(naturalCompare.caseInsensitive);
});

});


describe('naturalCompare.alphabet', function() {
describe('naturalCompare.alphabet', () => {

it('can be set and retrieved', function() {
it('can be set and retrieved', () => {
naturalCompare.alphabet = 'cba';
naturalCompare.alphabet.should.equal('cba');
});

it('can be set to null', function() {
it('can be set to null', () => {
naturalCompare.alphabet = null;
});

Expand Down

0 comments on commit 6d9d8c0

Please sign in to comment.