import { random, RANDOM_TYPE, randomAnyString, randomAlphaNumeric, randomSerialString, randomOTP, formatMoney, formatPercent, htmlEncode, firstLetter, isObject, isArray, toTitleCaseFromCamel, toTitleCase, toCamelCase, toSnakeCase, trimStr, objToString, parseTemplate, randomIdStr, normalizeObject, normalizeArrObject, removeLastComma, toUpperCase, formatHumanName, truncateStr, toDecimal, toForex, } from './string.util'; describe('random', () => { it('should generate a random alphanumeric string of the specified length', () => { const length = 10; const result = random(RANDOM_TYPE.ALPHANUMERIC, length); expect(result).toHaveLength(length); }); it('should generate a random numeric string of the specified length', () => { const length = 8; const result = random(RANDOM_TYPE.NUMERIC, length); expect(result).toHaveLength(length); }); it('should generate a random uppercase character string of the specified length', () => { const length = 5; const result = random(RANDOM_TYPE.UPPERCASE_CHARACTERS, length); expect(result).toHaveLength(length); }); it('should generate a random lowercase character string of the specified length', () => { const length = 7; const result = random(RANDOM_TYPE.LOWERCASE_CHARACTERS, length); expect(result).toHaveLength(length); }); it('should generate a random string with all case characters of the specified length', () => { const length = 6; const result = random(RANDOM_TYPE.ALL_CASE_CHARACTERS, length); expect(result).toHaveLength(length); }); it('should generate a random string with all characters of the specified length', () => { const length = 12; const result = random(RANDOM_TYPE.ALL_CHARACTERS, length); expect(result).toHaveLength(length); }); }); describe('random', () => { it('should generate a random alphanumeric string of the specified length', () => { const length = 10; const result = random(RANDOM_TYPE.ALPHANUMERIC, length); expect(result).toHaveLength(length); }); it('should generate a random numeric string of the specified length', () => { const length = 8; const result = random(RANDOM_TYPE.NUMERIC, length); expect(result).toHaveLength(length); }); it('should generate a random uppercase character string of the specified length', () => { const length = 5; const result = random(RANDOM_TYPE.UPPERCASE_CHARACTERS, length); expect(result).toHaveLength(length); }); it('should generate a random lowercase character string of the specified length', () => { const length = 7; const result = random(RANDOM_TYPE.LOWERCASE_CHARACTERS, length); expect(result).toHaveLength(length); }); it('should generate a random string with all case characters of the specified length', () => { const length = 6; const result = random(RANDOM_TYPE.ALL_CASE_CHARACTERS, length); expect(result).toHaveLength(length); }); it('should generate a random string with all characters of the specified length', () => { const length = 12; const result = random(RANDOM_TYPE.ALL_CHARACTERS, length); expect(result).toHaveLength(length); }); }); describe('randomAnyString', () => { it('should generate a random string of all characters with the specified length', () => { const length = 16; const result = randomAnyString(length); expect(result).toHaveLength(length); expect(result).toMatch(/^[a-zA-Z0-9_\-]+$/); }); it('should generate a random string of all characters with the default length', () => { const result = randomAnyString(); expect(result).toHaveLength(16); expect(result).toMatch(/^[a-zA-Z0-9_\-]+$/); }); }); describe('randomAlphaNumeric', () => { it('should generate a random alphanumeric string with the specified length', () => { const length = 32; const result = randomAlphaNumeric(length); expect(result).toHaveLength(length); expect(result).toMatch(/^[a-zA-Z0-9]+$/); }); it('should generate a random alphanumeric string with the default length', () => { const result = randomAlphaNumeric(); expect(result).toHaveLength(32); expect(result).toMatch(/^[a-zA-Z0-9]+$/); }); }); describe('randomSerialString', () => { it('should generate a random serial string in the format of XXXX-XXXX-XXXX-XXXX', () => { const result = randomSerialString(); expect(result).toMatch(/^[A-Z]{4}-[A-Z]{4}-[A-Z]{4}-[A-Z]{4}$/); }); }); describe('randomOTP', () => { it('should generate a random numeric string with the specified length', () => { const length = 6; const result = randomOTP(length); expect(result).toHaveLength(length); expect(result).toMatch(/^[0-9]+$/); }); it('should generate a random numeric string with the default length', () => { const result = randomOTP(); expect(result).toHaveLength(6); expect(result).toMatch(/^[0-9]+$/); }); }); describe('formatMoney', () => { it('should format the given amount of money with the specified currency code', () => { const amount = 1000; const currencyCode = 'USD'; const result = formatMoney(amount, currencyCode); expect(result).toBe('1,000.00 USD'); }); }); describe('formatPercent', () => { it('should format the given number as a percentage string', () => { const value = 75; const result = formatPercent(value); expect(result).toBe('75.00 %'); }); }); describe('htmlEncode', () => { it('should encode the given string to HTML entities', () => { const rawStr = ''; const result = htmlEncode(rawStr); expect(result).toBe( '<script>alert("Hello, World!")</script>' ); }); }); describe('firstLetter', () => { it('should return the first letter of the input string', () => { const str = 'Hello, World!'; const result = firstLetter(str); expect(result).toBe('Hello'); }); it('should return an empty string if the input string is empty', () => { const str = ''; const result = firstLetter(str); expect(result).toBe(''); }); it('should return an empty string if the input string contains only whitespace', () => { const str = ' '; const result = firstLetter(str); expect(result).toBe(''); }); }); describe('isObject', () => { it('should return true if the given element is an object', () => { const ele = '{"name": "John", "age": 30}'; const result = isObject(ele); expect(result).toBe(true); }); it('should return false if the given element is not an object', () => { const ele = 'John Doe'; const result = isObject(ele); expect(result).toBe(false); }); }); describe('isArray', () => { it('should return true if the given value is an array', () => { const ele = '[1, 2, 3]'; const result = isArray(ele); expect(result).toBe(true); }); it('should return false if the given value is not an array', () => { const ele = 'John Doe'; const result = isArray(ele); expect(result).toBe(false); }); }); describe('toTitleCaseFromCamel', () => { it('should convert a camel case string to title case', () => { const str = 'helloWorld'; const result = toTitleCaseFromCamel(str); expect(result).toBe('Hello World'); }); }); describe('toTitleCase', () => { it('should convert a string to title case', () => { const str = 'hello world'; const result = toTitleCase(str); expect(result).toBe('Hello World'); }); }); describe('toCamelCase', () => { it('should convert a string to camel case', () => { const str = 'hello world'; const result = toCamelCase(str); expect(result).toBe('helloWorld'); }); }); describe('toSnakeCase', () => { it('should convert a string to snake case', () => { const str = 'hello world'; const result = toSnakeCase(str); expect(result).toBe('hello_world'); }); }); describe('trimStr', () => { it('should trim the given string', () => { const str = ' Hello, World! '; const result = trimStr(str); expect(result).toBe('Hello, World!'); }); it('should return the same string if it is already trimmed', () => { const str = 'Hello, World!'; const result = trimStr(str); expect(result).toBe('Hello, World!'); }); it('should return an empty string if the input string is empty', () => { const str = ''; const result = trimStr(str); expect(result).toBe(''); }); }); describe('objToString', () => { it('should convert the given object to a string', () => { const obj = { name: 'John', age: 30 }; const result = objToString(obj); expect(result).toBe('{"name":"John","age":30}'); }); it('should return the same string if the input is already a string', () => { const obj = '{"name":"John","age":30}'; const result = objToString(obj); expect(result).toBe('{"name":"John","age":30}'); }); it('should return an empty object string if the input object is empty', () => { const obj = {}; const result = objToString(obj); expect(result).toBe('{}'); }); }); describe('parseTemplate', () => { it('should parse the given template with the provided parameters', () => { const rawTemplate = 'Hello, {{name}}!'; const params = { name: 'John' }; const result = parseTemplate(rawTemplate, params); expect(result).toBe('Hello, John!'); }); it('should return the same template if no parameters are provided', () => { const rawTemplate = 'Hello, World!'; const params = {}; const result = parseTemplate(rawTemplate, params); expect(result).toBe('Hello, World!'); }); }); describe('randomIdStr', () => { it('should generate a random ID string', () => { const result = randomIdStr(); expect(result).toHaveLength(21); expect(result).toMatch(/^[a-zA-Z0-9_-]+$/); }); }); describe('normalizeObject', () => { it('should normalize an object by removing extra whitespace from string values', () => { const obj = { name: ' John ', age: 30, address: { street: ' 123 Main St ', city: ' New York ', }, }; const result = normalizeObject(obj); expect(result).toEqual({ name: 'John', age: 30, address: { street: '123 Main St', city: 'New York', }, }); }); it('should return the same object if it is null or undefined', () => { const obj = null; const result = normalizeObject(obj); expect(result).toBeNull(); }); }); describe('normalizeArrObject', () => { it('should normalize an array of objects', () => { const arr = [ { name: ' John ', age: 30, }, { name: ' Jane ', age: 25, }, ]; const result = normalizeArrObject(arr); expect(result).toEqual([ { name: 'John', age: 30, }, { name: 'Jane', age: 25, }, ]); }); it('should return the same array if it is not an array', () => { const arr = 'John Doe'; const result = normalizeArrObject(arr as any); expect(result).toBe('John Doe'); }); }); describe('removeLastComma', () => { it('should remove the last comma from the given string', () => { const str = 'Hello, World,'; const result = removeLastComma(str); expect(result).toBe('Hello, World'); }); it('should return the same string if it does not end with a comma', () => { const str = 'Hello, World!'; const result = removeLastComma(str); expect(result).toBe('Hello, World!'); }); it('should return an empty string if the input string is empty', () => { const str = ''; const result = removeLastComma(str); expect(result).toBe(''); }); }); describe('toUpperCase', () => { it('should convert the given string to uppercase', () => { const str = 'hello, world!'; const result = toUpperCase(str); expect(result).toBe('HELLO, WORLD!'); }); it('should return an empty string if the input string is empty', () => { const str = ''; const result = toUpperCase(str); expect(result).toBe(''); }); }); describe('formatHumanName', () => { it('should format the given string as a human name', () => { const str = 'john doe'; const result = formatHumanName(str); expect(result).toBe('John Doe'); }); it('should return an empty string if the input string is empty', () => { const str = ''; const result = formatHumanName(str); expect(result).toBe(''); }); }); describe('truncateStr', () => { it('should truncate the given string to the specified size', () => { const size = 10; const str = 'Hello, World!'; const result = truncateStr(size, str); expect(result).toBe('Hello, ...'); }); it('should return the same string if it is shorter than the specified size', () => { const size = 20; const str = 'Hello, World!'; const result = truncateStr(size, str); expect(result).toBe('Hello, World!'); }); it('should return an empty string if the input string is empty', () => { const size = 10; const str = ''; const result = truncateStr(size, str); expect(result).toBe(''); }); }); describe('toDecimal', () => { it('should convert a number or string to a decimal number with the specified number of decimal places', () => { expect(toDecimal(123.4567, 2)).toBe(123.46); expect(toDecimal(123.001, 3)).toBe(123.001); expect(toDecimal('123.4567', 1)).toBe(123.5); }); }); describe('toForex', () => { it('should convert a number to a forex number with a maximum of 8 decimal places', () => { expect(toForex(0.123456)).toBe(0.1235); expect(toForex(0.000123456)).toBe(0.0001235); expect(toForex(0.00000123456)).toBe(0.00000123); expect(toForex(0.0000000123456)).toBe(0.00000001); }); });