Skip to content

Commit

Permalink
Add compare function
Browse files Browse the repository at this point in the history
  • Loading branch information
dleitee committed Apr 30, 2016
1 parent d754a39 commit 05dd926
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/string.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,21 @@ const format = (value, params = []) =>
);

export {format};

/**
* Compares two strings to each other. If they are equivalent, a zero is returned. Otherwise,
* most of these routines will return a positive or negative result corresponding to whether stringA
* is lexicographically greater than, or less than, respectively, than stringB.
* @param stringA
* @param stringB
* @return signed integer
*/
const compare = (stringA, stringB) => {
if(stringA === stringB){
return 0;
}

return stringA > stringB? 1 : -1;
};

export {compare};
14 changes: 13 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {isString, trim, removeSpaces, replace, removeNonChars, removeNonWords, a
endsWith, startsWith, ensureLeft, ensureRight, first, last, indexOf, lastIndexOf, insert,
length, leftPad, rightPad, prepend, removeLeft, appendArray, prependArray, removeRight,
repeat, reverse, shuffle, surround, safeTruncate, transliterate, truncate, removeEmptyStrings,
format}
format, compare}
from '../src/strman';

describe('isString function', () => {
Expand Down Expand Up @@ -885,3 +885,15 @@ describe('removeEmptyStrings function', () => {
chai.expect(removeEmptyStrings([ 'aa', '', 'bb', null, 'cc', undefined ])).to.deep.equal([ 'aa', 'bb', 'cc' ]);
});
});

describe('compare function', () => {
it('should be 1, -1, 0', () => {
chai.expect(compare('a', 'b')).to.equal(-1);
chai.expect(compare('b', 'a')).to.equal(1);
chai.expect(compare('a', 'a')).to.equal(0);
chai.expect(compare('0', '1')).to.equal(-1);
chai.expect(compare('1', '0')).to.equal(1);
chai.expect(compare('0', '0')).to.equal(0);

});
});

0 comments on commit 05dd926

Please sign in to comment.