Skip to content

Commit

Permalink
feat: doesNotEqual util
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeIbberson committed Feb 3, 2020
1 parent 41cae41 commit 9f9018c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
11 changes: 11 additions & 0 deletions lib/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ describe('Utils', () => {
expect(Utils.equals('a', '*')).toBeTruthy());
});

describe('"doesNotEqual"', () => {
it('should return truthy', () =>
expect(Utils.doesNotEqual('foo', 'bar')).toBeTruthy());

it('should return falsy', () =>
expect(Utils.doesNotEqual('foo', 'foo')).toBeFalsy());

it('should return falsy', () =>
expect(Utils.doesNotEqual(2, 2)).toBeFalsy());
});

describe('isGreaterThan', () => {
it('should return truthy on numerics', () =>
expect(Utils.isGreaterThan(123, 12)).toBeTruthy());
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const flat = require('flat');

const {
equals,
doesNotEqual,
isGreaterThan,
isLessThan,
isGreaterThanOrEqualTo,
Expand All @@ -16,6 +17,7 @@ const ops = {
'>': isGreaterThan,
'<': isLessThan,
'=': equals,
'!=': doesNotEqual,
};

const getAssignment = {
Expand Down
14 changes: 9 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,19 @@ class ValidatorRunner {
}
}

const runCompare = (a, b, locale) =>
String(a).localeCompare(String(b), locale, {
sensitivity: 'base',
});

const equals = (a, b, locale) => {
if (b === '*') return a !== '' && a !== undefined && a !== null;

return (
String(a).localeCompare(String(b), locale, {
sensitivity: 'base',
}) === 0
);
return runCompare(a, b, locale) === 0;
};

const doesNotEqual = (a, b, locale) => runCompare(a, b, locale) !== 0;

const isGreaterThan = (a, b) =>
new ValidatorRunner([a, b]).sequence([
'isGreaterThanNumeric',
Expand Down Expand Up @@ -103,6 +106,7 @@ const isEmpty = (v) =>

module.exports = {
equals,
doesNotEqual,
isGreaterThan,
isLessThan,
isGreaterThanOrEqualTo,
Expand Down

0 comments on commit 9f9018c

Please sign in to comment.