mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 01:47:00 +01:00
feat(mme-17212): convert shared/module/string-utils.js -> Typescript (#17320)
This commit is contained in:
parent
92f6ea6f6b
commit
b310c6dcca
@ -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');
|
|
||||||
}
|
|
27
shared/modules/string-utils.test.js
Normal file
27
shared/modules/string-utils.test.js
Normal file
@ -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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
29
shared/modules/string-utils.ts
Normal file
29
shared/modules/string-utils.ts
Normal file
@ -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');
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user