From 9f9018ccd97ca94c951bd656fde1e7c0c699d56f Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 3 Feb 2020 13:39:45 -0500 Subject: [PATCH] feat: doesNotEqual util --- lib/__tests__/utils.test.js | 11 +++++++++++ lib/index.js | 2 ++ lib/utils.js | 14 +++++++++----- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/__tests__/utils.test.js b/lib/__tests__/utils.test.js index 5f54f99..e1905dd 100644 --- a/lib/__tests__/utils.test.js +++ b/lib/__tests__/utils.test.js @@ -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()); diff --git a/lib/index.js b/lib/index.js index b7b2b6a..283046d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,6 +2,7 @@ const flat = require('flat'); const { equals, + doesNotEqual, isGreaterThan, isLessThan, isGreaterThanOrEqualTo, @@ -16,6 +17,7 @@ const ops = { '>': isGreaterThan, '<': isLessThan, '=': equals, + '!=': doesNotEqual, }; const getAssignment = { diff --git a/lib/utils.js b/lib/utils.js index 5db3ae7..d89aedc 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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', @@ -103,6 +106,7 @@ const isEmpty = (v) => module.exports = { equals, + doesNotEqual, isGreaterThan, isLessThan, isGreaterThanOrEqualTo,