From b310c6dcca6765d66f0cf980627d46503e36a198 Mon Sep 17 00:00:00 2001 From: Danica Shen Date: Fri, 20 Jan 2023 17:40:48 +0000 Subject: [PATCH] feat(mme-17212): convert shared/module/string-utils.js -> Typescript (#17320) --- shared/modules/string-utils.js | 10 ---------- shared/modules/string-utils.test.js | 27 +++++++++++++++++++++++++++ shared/modules/string-utils.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 10 deletions(-) delete mode 100644 shared/modules/string-utils.js create mode 100644 shared/modules/string-utils.test.js create mode 100644 shared/modules/string-utils.ts diff --git a/shared/modules/string-utils.js b/shared/modules/string-utils.js deleted file mode 100644 index c8a1c0e87..000000000 --- a/shared/modules/string-utils.js +++ /dev/null @@ -1,10 +0,0 @@ -export function isEqualCaseInsensitive(value1, value2) { - if (typeof value1 !== 'string' || typeof value2 !== 'string') { - return false; - } - return value1.toLowerCase() === value2.toLowerCase(); -} - -export function prependZero(num, maxLength) { - return num.toString().padStart(maxLength, '0'); -} diff --git a/shared/modules/string-utils.test.js b/shared/modules/string-utils.test.js new file mode 100644 index 000000000..cf3ffa040 --- /dev/null +++ b/shared/modules/string-utils.test.js @@ -0,0 +1,27 @@ +import { isEqualCaseInsensitive, prependZero } from './string-utils'; + +describe('string-utils', () => { + describe('isEqualCaseInsensitive', () => { + it('should return true for FOO and foo', () => { + expect(isEqualCaseInsensitive('FOO', 'foo')).toBeTruthy(); + }); + + it('should return false for foo and Bar', () => { + expect(isEqualCaseInsensitive('foo', 'Bar')).toBeFalsy(); + }); + + it('should return false for number and string comparision', () => { + expect(isEqualCaseInsensitive('foo', 123)).toBeFalsy(); + }); + }); + + describe('prependZero', () => { + it('should return number to given max length string when digit is smaller than maxLength', () => { + expect(prependZero(123, 4)).toStrictEqual('0123'); + }); + + it('should return number to given max length string when digit is large than maxLength', () => { + expect(prependZero(123, 2)).toStrictEqual('123'); + }); + }); +}); diff --git a/shared/modules/string-utils.ts b/shared/modules/string-utils.ts new file mode 100644 index 000000000..248463dd1 --- /dev/null +++ b/shared/modules/string-utils.ts @@ -0,0 +1,29 @@ +/** + * Compare 2 given strings and return boolean + * eg: "foo" and "FOO" => true + * eg: "foo" and "bar" => false + * eg: "foo" and 123 => false + * + * @param value1 - first string to compare + * @param value2 - first string to compare + * @returns true if 2 strings are identical when they are lowercase + */ +export function isEqualCaseInsensitive( + value1: string, + value2: string, +): boolean { + if (typeof value1 !== 'string' || typeof value2 !== 'string') { + return false; + } + return value1.toLowerCase() === value2.toLowerCase(); +} + +/** + * Takes a number with max length until the resulting string reaches the given length + * + * @param num + * @param maxLength + */ +export function prependZero(num: number, maxLength: number): string { + return num.toString().padStart(maxLength, '0'); +}