import { EMOJI_REGEX, Strings } from '../src' export namespace Strings_Test { import pad = Strings.pad import parseJson = Strings.parseJson import capFirstLetter = Strings.capitalFirstLetter import formatNumber = Strings.formatNumber import combinations = Strings.combinations import permutations = Strings.permutations import sortBy = Strings.sortBy import format = Strings.format import shortest = Strings.shortest import getRomanNotation = Strings.getRomanNotation import shortenString = Strings.shortenString import stripComments = Strings.stripComments import capitalize = Strings.capitalize import quote = Strings.quote import escape2 = Strings.escape2 import stringify = Strings.stringify import joiner = Strings.joiner import encoder = Strings.encoder import replacer = Strings.replacer import matcher = Strings.matcher import lowerCase = Strings.lowerCase import trimmer = Strings.trimmer import longestSequence = Strings.longestSequence import replaceBy = Strings.replaceBy import repeat = Strings.repeat import delim = Strings.delim import generateSymbol = Strings.generateSymbol import doubleQuote = Strings.doubleQuote import joinList = Strings.joinList import htmlText = Strings.htmlText import getProjectId = Strings.getProjectId import padLeft = Strings.padLeft import htmlEncode = Strings.htmlEncode import toUTF16 = Strings.toUTF16 import removeChars = Strings.removeChars import cjk = Strings.cjk import hashCode = Strings.hashCode import escapeHtml = Strings.escapeHtml import escapeAscii = Strings.escapeAscii import escapeControl = Strings.escapeControl import maskToken = Strings.maskToken import checkAll = Strings.checkAll import checkAny = Strings.checkAny import isMatch = Strings.isMatch beforeAll(() => { console.log('Strings test suite started') console.time('Execution time took') }) afterAll(() => { console.log('Strings test suite finished') console.timeEnd('Execution time took') }) describe('Check emoji string replacer', () => { it('it should replace emojis in a string', () => { expect(replaceBy(EMOJI_REGEX, '🎉Party 🚩Flags! 🚀Rockets!')).toEqual('Party Flags! Rockets!') }) it('it should kill emojis in a string and replace by a set replacement value', () => { expect(replaceBy(EMOJI_REGEX, '🎉 Party 🚩 Flags! 🚀 Rockets!', 'X')).toEqual( 'X Party X Flags! X Rockets!', ) }) }) describe('Check convert html to text string', () => { it('it should return valid replaced html string', () => { expect(htmlText('
Info data block
')).toEqual( '<p align="center" style="text-align:center;">Info data block</p>', ) expect(escapeHtml('')).toEqual( '<div style="text-align:center;"></div>', ) expect(escapeHtml('test')).toEqual('<span>test</span>') }) }) describe('Check escaped control string', () => { it('it should return valid escaped control string', () => { expect(escapeControl('\n')).toEqual( '\\n', ) expect(escapeControl('\\n')).toEqual( '\\n', ) expect(escapeControl('HELLO,WORLD!')).toEqual('HELLO,WORLD!') }) }) describe('Check string replacement by pattern', () => { it('it should return valid string by pattern replacement', () => { expect( 'Smith, Bob; Raman, Ravi; Jones, Mary'['replaceWith'](/([\w]+), ([\w]+)/g, '$2 $1'), ).toEqual('Bob Smith; Ravi Raman; Mary Jones') expect(''['replaceWith'](/([\w]+), ([\w]+)/g, '$2 $1')).toEqual('') expect('test1, test2'['replaceWith'](/([\w]+), ([\w]+)/g, '$2 $1')).toEqual('test2 test1') expect( "California, San Francisco, O'Rourke, Gerry"['replaceWith']( /([\w'\s]+), ([\w'\s]+), ([\w'\s]+), ([\w'\s]+)/, '$4 $3 lives in $2, $1', ), ).toEqual("Gerry O'Rourke lives in San Francisco, California") expect( String['__replaceWith__']( 'Smith, Bob; Raman, Ravi; Jones, Mary', /([\w]+), ([\w]+)/g, '$2 $1', ), ).toEqual('Bob Smith; Ravi Raman; Mary Jones') expect(String['__replaceWith__']('', /([\w]+), ([\w]+)/g, '$2 $1')).toEqual('') expect(String['__replaceWith__']('test1, test2', /([\w]+), ([\w]+)/g, '$2 $1')).toEqual( 'test2 test1', ) expect( String['__replaceWith__']( "California, San Francisco, O'Rourke, Gerry", /([\w'\s]+), ([\w'\s]+), ([\w'\s]+), ([\w'\s]+)/, '$4 $3 lives in $2, $1', ), ).toEqual("Gerry O'Rourke lives in San Francisco, California") }) }) describe('Check string replacement by regex', () => { it('it should return valid string by regex replacement', () => { expect( '72 101 108 108 111 87 111 114 108 100 33'['replaceByRegex'](/(\d+)(\s?)/gi, (_, $1) => String.fromCharCode($1), ), ).toEqual('Hello World!') expect('hello, world!'['replaceByRegex'](/(\S+)(\s?)/gi, (_, $1) => $1.toUpperCase())).toEqual( 'HELLO,WORLD!', ) expect('HELLO,WORLD!'['replaceByRegex'](/(\S+)(\s?)/gi, (_, $1) => $1.toLowerCase())).toEqual( 'hello,world!', ) expect( String['__replaceByRegex__']( '72 101 108 108 111 87 111 114 108 100 33', /(\d+)(\s?)/gi, (_, $1) => String.fromCharCode($1), ), ).toEqual('Hello World!') expect( String['__replaceByRegex__']('hello, world!', /(\S+)(\s?)/gi, (_, $1) => $1.toUpperCase()), ).toEqual('HELLO,WORLD!') expect( String['__replaceByRegex__']('HELLO,WORLD!', /(\S+)(\s?)/gi, (_, $1) => $1.toLowerCase()), ).toEqual('hello,world!') }) }) describe('Check string escaped symbols', () => { it('it should return valid string with escaped symbols', () => { expect('\n'['escapeSpecialChars']()).toEqual( '\\n', ) expect('\\n'['escapeSpecialChars']()).toEqual( '\\n', ) expect('HELLO,WORLD!'['escapeSpecialChars']()).toEqual('HELLO,WORLD!') expect(String['__escapeSpecialChars__']('\n')).toEqual( '\\n', ) expect(String['__escapeSpecialChars__']('\\n')).toEqual( '\\n', ) expect(String['__escapeSpecialChars__']('HELLO,WORLD!')).toEqual('HELLO,WORLD!') }) }) describe('Check longest string sequence', () => { it('it should return valid longest string sequence', () => { expect(JSON.stringify(longestSequence((a, b) => a === b, 'skiing'))).toEqual( '{"member":"i","count":2}', ) expect(JSON.stringify(longestSequence((a, b) => a === b, 'test'))).toEqual( '{"member":null,"count":0}', ) expect(JSON.stringify(longestSequence((a, b) => a === b, ''))).toEqual( '{"member":null,"count":0}', ) }) }) describe('Check replace string operation', () => { it('it should return valid replaced string', () => { expect(replacer(['argentina', 'brazil', 'chile']).join('-')).toEqual('Argentina-Brazil-Chile') expect(replacer(Array('a', 'b', 'c')).join('-')).toEqual('A-B-C') expect(replacer(['gender']).join('-')).toEqual('Gender') expect(replacer(['a16', 'b44', 'b bar']).join('-')).toEqual('A16-B44-B Bar') }) }) describe('Check match string operation', () => { it('it should return valid matched string', () => { expect(matcher(['argentina', 'brazil', 'chile'])).toEqual(null) expect(matcher(Array('a', 'b', 'c'))).toEqual(null) expect(matcher(['gender13']).join('-')).toEqual('r13') expect(matcher(['a16', 'b44', 'b bar']).join('-')).toEqual('a16-b44') }) }) describe('Check lower case string operation', () => { it('it should return valid lower case string', () => { expect(lowerCase('Argentina', 'Brazil', 'Chile').join('-')).toEqual('argentina-brazil-chile') expect(lowerCase('a', 'B', 'c').join('-')).toEqual('a-b-c') expect(lowerCase('Gender13').join('-')).toEqual('gender13') expect(lowerCase('a16', 'b44', 'b bar').join('-')).toEqual('a16-b44-b bar') expect(lowerCase('DIV', 'H1', 'SPAN').join('-')).toEqual('div-h1-span') }) }) describe('Check trim string operation', () => { it('it should return valid trimmed string', () => { expect(trimmer('Argentina ', ' Brazil', 'Chile').join('-')).toEqual('Argentina-Brazil-Chile') expect(trimmer('a', 'B ', 'c').join('-')).toEqual('a-B-c') expect(trimmer(' Gender13 ').join('-')).toEqual('Gender13') expect(trimmer(' a16', 'b44 ', 'b bar').join('-')).toEqual('a16-b44-b bar') expect(trimmer(' d ', ' h ', ' s').join('-')).toEqual('d-h-s') expect(trimmer(' a', ' b ').join('-')).toEqual('a-b') }) }) describe('Check double quotes', () => { it('it should return valid string with double quotes', () => { let cmd = quote(['echo', "hell'o1 ${HOME}"], true) cmd = ['bash', '-c', quote(cmd)].join(' ') expect(quote(cmd)).toEqual('"bash -c \\"echo \\\\"hell\'o1 \\\\${HOME}\\\\"\\""') expect(quote(['a', 'b', 'c d'])).toEqual("a b 'c d'") expect(quote(['a', 'b', 'it\'s a "neat thing"'])).toEqual('a b "it\'s a \\"neat thing\\""') expect(quote(['$', '`', "'"])).toEqual('\\$ \\` "\'"') expect(quote([])).toEqual('') expect(quote([''])).toEqual("''") expect(quote(['a\nb'])).toEqual("'a\nb'") expect(quote([' #(){}*|][!'])).toEqual("' #(){}*|][!'") expect(quote(["'#(){}*|][!"])).toEqual('"\'#(){}*|][\\!"') expect(quote(['X#(){}*|][!'])).toEqual('X\\#\\(\\)\\{\\}\\*\\|\\]\\[\\!') expect(quote(['a\n#\nb'])).toEqual("'a\n#\nb'") expect(quote(['><;{}'])).toEqual('\\>\\<\\;\\{\\}') expect(quote(['a', 1, true, false])).toEqual('a 1 true false') expect(quote(['a', 1, null, undefined])).toEqual('a 1 null undefined') expect(quote(['a\\x'])).toEqual('a\\\\x') }) it('it should return valid string with single quotes', () => { expect(quote(['a', '|', 'b'])).toEqual('a \\| b') expect(quote(['a', '&&', 'b', ';', 'c'])).toEqual('a \\&\\& b \\; c') }) }) describe('Check string padding', () => { it('it should add zero padding to numbers', () => { expect(pad(7, 2)).toEqual('07') }) it('it should add zero padding to numbers', () => { expect(pad(998, 4)).toEqual('0998') }) it('it should not add 2 zero pad to numbers when no size is set', () => { expect(pad(7)).toEqual('07') }) }) describe('Check parsing input json', () => { const validString = `{"firstName":"Daphne","lastName":"Smit"}` const invalidString = `{"firstName":"Daphne",,"lastName":"Smit"}` it('it should parseJson a valid json without errors', () => { expect(parseJson(validString)).toEqual({ firstName: 'Daphne', lastName: 'Smit', }) }) it('it should parseJson an invalid json without errors and return undefined', () => { expect(parseJson(invalidString)).toEqual(undefined) }) it('it should parseJson an invalid json without errors and return default value when set', () => { expect(parseJson(invalidString, { failed: true })).toEqual({ failed: true }) }) }) describe('Check capitalize first letter', () => { it('it should uppercase the first character of a string', () => { expect(capFirstLetter('daphne')).toEqual('Daphne') }) it('it should uppercase the first character of a string', () => { expect(capFirstLetter('people are crazy!')).toEqual('People are crazy!') }) it('it should uppercase the first character of a string', () => { expect(capFirstLetter('typescript: you are so powerful!')).toEqual( 'Typescript: you are so powerful!', ) }) }) }