Fixed word formatters
This commit is contained in:
parent
a08b0c4eea
commit
c06538a725
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.25.7",
|
"version": "0.25.8",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -21,11 +21,13 @@ export const CHAR_LIST = LETTER_LIST + LETTER_LIST.toLowerCase() + NUMBER_LIST +
|
|||||||
/**
|
/**
|
||||||
* Converts text to camelCase
|
* Converts text to camelCase
|
||||||
*/
|
*/
|
||||||
export function camelCase(str?: string) {
|
export function camelCase(str?: string): string {
|
||||||
const text = pascalCase(str);
|
if(!str) return '';
|
||||||
return !text ? '' : text[0].toLowerCase() + text.slice(1);
|
const pascal = pascalCase(str);
|
||||||
|
return pascal.charAt(0).toLowerCase() + pascal.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert number of bytes into a human-readable size
|
* Convert number of bytes into a human-readable size
|
||||||
*
|
*
|
||||||
@ -75,14 +77,12 @@ export function insertAt(target: string, str: string, index: number): String {
|
|||||||
/**
|
/**
|
||||||
* Converts text to kebab-case
|
* Converts text to kebab-case
|
||||||
*/
|
*/
|
||||||
export function kebabCase(str: string) {
|
export function kebabCase(str?: string): string {
|
||||||
if(!str) return '';
|
if(!str) return '';
|
||||||
return str.replaceAll(/(^[^a-zA-Z]+|[^a-zA-Z0-9-_])/g, '')
|
return wordSegments(str).map(w => w.toLowerCase()).join("-");
|
||||||
.replaceAll(/([A-Z]|[0-9]+)/g, (...args) => `-${args[0].toLowerCase()}`)
|
|
||||||
.replaceAll(/([0-9])([a-z])/g, (...args) => `${args[1]}-${args[2]}`)
|
|
||||||
.replaceAll(/[^a-z0-9]+(\w?)/g, (...args) => `-${args[1] ?? ''}`).toLowerCase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add padding to string
|
* Add padding to string
|
||||||
*
|
*
|
||||||
@ -112,12 +112,13 @@ export function pad(text: any, length: number, char: string = ' ', start = true)
|
|||||||
*/
|
*/
|
||||||
export function pascalCase(str?: string): string {
|
export function pascalCase(str?: string): string {
|
||||||
if(!str) return '';
|
if(!str) return '';
|
||||||
return str.match(/[a-zA-Z0-9]+/g)
|
return wordSegments(str)
|
||||||
?.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
.map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
|
||||||
.join('') ?? '';
|
.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a random hexadecimal value
|
* Generate a random hexadecimal value
|
||||||
*
|
*
|
||||||
@ -184,14 +185,12 @@ export function randomStringBuilder(length: number, letters = false, numbers = f
|
|||||||
/**
|
/**
|
||||||
* Converts text to snake_case
|
* Converts text to snake_case
|
||||||
*/
|
*/
|
||||||
export function snakeCase(str?: string) {
|
export function snakeCase(str?: string): string {
|
||||||
if(!str) return '';
|
if(!str) return '';
|
||||||
return str.replaceAll(/(^[^a-zA-Z]+|[^a-zA-Z0-9-_])/g, '')
|
return wordSegments(str).map(w => w.toLowerCase()).join("_");
|
||||||
.replaceAll(/([A-Z]|[0-9]+)/g, (...args) => `_${args[0].toLowerCase()}`)
|
|
||||||
.replaceAll(/([0-9])([a-z])/g, (...args) => `${args[1]}_${args[2]}`)
|
|
||||||
.replaceAll(/[^a-z0-9]+(\w?)/g, (...args) => `_${args[1] ?? ''}`).toLowerCase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splice a string together (Similar to Array.splice)
|
* Splice a string together (Similar to Array.splice)
|
||||||
*
|
*
|
||||||
@ -298,6 +297,23 @@ export function md5(d: string) {
|
|||||||
function bit_rol(d:any,_:any){return d<<_|d>>>32-_
|
function bit_rol(d:any,_:any){return d<<_|d>>>32-_
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Splits a string into logical word segments
|
||||||
|
*/
|
||||||
|
export function wordSegments(str?: string): string[] {
|
||||||
|
if (!str) return [];
|
||||||
|
return str
|
||||||
|
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
||||||
|
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
||||||
|
.replace(/([0-9]+)([a-zA-Z])/g, "$1 $2")
|
||||||
|
.replace(/([a-zA-Z])([0-9]+)/g, "$1 $2")
|
||||||
|
.replace(/[_\-\s]+/g, " ")
|
||||||
|
.trim()
|
||||||
|
.split(/\s+/)
|
||||||
|
.filter(Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if email is valid
|
* Check if email is valid
|
||||||
*
|
*
|
||||||
|
@ -59,7 +59,8 @@ describe('String Utilities', () => {
|
|||||||
|
|
||||||
describe('kebabCase', () => {
|
describe('kebabCase', () => {
|
||||||
it('converts to kebab-case', () => {
|
it('converts to kebab-case', () => {
|
||||||
expect(kebabCase('HelloWorldTest')).toContain('-hello');
|
expect(kebabCase('hello world')).toBe('hello-world');
|
||||||
|
expect(kebabCase('HelloWorld')).toContain('hello-world');
|
||||||
expect(kebabCase('')).toBe('');
|
expect(kebabCase('')).toBe('');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user