UNPKG

@legion-ui-kit/react-core

Version:

To install the package into your project:

1 lines 4 kB
{"version":3,"file":"typography.cjs","sources":["../../../src/helpers/typography.ts"],"sourcesContent":["/**\r\n * Remove punctuation from a string. Optionally replace with a custom string.\r\n * @param str String to remove punctuation from\r\n * @param opt Object contains options for the function\r\n * @param opt.replacement String to replace punctuation with\r\n * @param opt.noTrim Do not trim the string before removing punctuation\r\n */\r\nexport const removePunctuation = (str: string = '', opt?: { replacement?: string, noTrim?: boolean }): string => {\r\n\tconst { noTrim = false, replacement = '' } = opt || {};\r\n\r\n\tconst word = noTrim ? str : str.trim();\r\n\r\n\treturn word.replace(/[^a-zA-Z0-9\\s]+/gi, replacement);\r\n};\r\n\r\n/**\r\n * Remove whitespace from a string. Optionally replace with a custom string.\r\n * @param str String to remove whitespace from\r\n * @param replacement String to replace whitespace with\r\n */\r\nexport const removeWhitespace = (str: string = '', replacement: string = ''): string => {\r\n\treturn str.trim().replace(/\\s+/g, replacement);\r\n};\r\n\r\n/**\r\n * Remove non-word characters from the end of a string.\r\n * @param str String to remove non-word characters from\r\n */\r\nexport const trimNonWord = (str: string = ''): string => {\r\n\treturn str.replace(/[^a-zA-Z0-9\\s]+$/g, '');\r\n};\r\n\r\n/**\r\n * Convert a string to camelCase.\r\n * @param str String to convert to camelCase\r\n * @param noTrim Do not trim the string before converting to camelCase\r\n */\r\nexport const camelCase = (str: string = '', noTrim?: boolean): string => {\r\n\tconst camel = str.replace(/[\\s\\W\\d_]+[a-z]/g, (w) => w.toUpperCase());\r\n\treturn removePunctuation(camel, { noTrim });\r\n};\r\n\r\n/**\r\n * Convert a string to PascalCase.\r\n * @param str String to convert to PascalCase\r\n * @param noTrim Do not trim the string before converting to PascalCase\r\n */\r\nexport const pascalCase = (str: string = '', noTrim?: boolean): string => {\r\n\tconst camel = camelCase(str, noTrim);\r\n\r\n\treturn camel[0].toUpperCase() + camel.slice(1);\r\n};\r\n\r\n/**\r\n * Convert a string to Capital Case.\r\n * @param str String to convert to Capital Case\r\n * @param noTrim Do not trim the string before converting to Capitalcase\r\n */\r\nexport const capitalCase = (str: string = '', noTrim?: boolean): string => {\r\n\tconst pascal = pascalCase(str, noTrim);\r\n\r\n\treturn pascal.replace(/[a-z][A-Z0-9]|[0-9][A-Z]/g, (str) => `${str[0]} ${str[1]}`);\r\n};\r\n\r\n/**\r\n * Convert a string to kebab-case.\r\n * @param str String to convert to kebab-case\r\n * @param noTrim Do not trim the string before converting to kebab-case\r\n */\r\nexport const kebabCase = (str: string = '', noTrim: boolean = false): string => {\r\n\tconst kebab = removePunctuation(str, { replacement: '-', noTrim }).replace(/[\\s]+/g, '-');\r\n\r\n\treturn kebab.replace(/[a-z][A-Z0-9]|[0-9][a-zA-Z]/g, (str) => `${str[0]}-${str[1]}`).toLowerCase();\r\n};\r\n\r\n/**\r\n * Convert a string to snake_case.\r\n * @param str String to convert to snake_case\r\n * @param noTrim Do not trim the string before converting to snake_case\r\n */\r\nexport const snakeCase = (str: string = '', noTrim: boolean = false): string => {\r\n\tconst snake = removePunctuation(str, { replacement: '_', noTrim }).replace(/\\s+/g, '_');\r\n\r\n\treturn snake.replace(/[a-z][A-Z0-9]|[0-9][a-zA-Z]/g, (str) => `${str[0]}_${str[1]}`).toLowerCase();\r\n};\r\n"],"names":["removePunctuation","str","opt","noTrim","replacement","trim","replace","toLowerCase"],"mappings":"aAOO,MAAMA,EAAoB,CAACC,EAAc,GAAIC,KACnD,MAAMC,OAAEA,GAAS,EAAKC,YAAEA,EAAc,IAAOF,GAAO,CAAA,EAIpD,OAFaC,EAASF,EAAMA,EAAII,QAEpBC,QAAQ,oBAAqBF,sBAyDjB,CAACH,EAAc,GAAIE,GAAkB,IAC/CH,EAAkBC,EAAK,CAAEG,YAAa,IAAKD,WAAUG,QAAQ,SAAU,KAExEA,QAAQ,+BAAiCL,GAAQ,GAAGA,EAAI,MAAMA,EAAI,MAAMM,4DAQ7D,CAACN,EAAc,GAAIE,GAAkB,IAC/CH,EAAkBC,EAAK,CAAEG,YAAa,IAAKD,WAAUG,QAAQ,OAAQ,KAEtEA,QAAQ,+BAAiCL,GAAQ,GAAGA,EAAI,MAAMA,EAAI,MAAMM"}