UNPKG

@ukbhra/common-utils

Version:

A collection of reusable utility functions for string, object, validation, and general operations in Node.js projects.

15 lines 1.25 kB
export const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1); export const camelToSnake = (str) => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`); export const snakeToCamel = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()); export const truncate = (str, length) => str.length > length ? str.slice(0, length) + '...' : str; export const escapeRegex = (str) => str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); export const slugify = (str) => str.toLowerCase().replace(/[\s\W-]+/g, '-').replace(/^-+|-+$/g, ''); export const isEmpty = (str) => !str || str.trim().length === 0; export const padLeft = (str, length, char = ' ') => str.length >= length ? str : char.repeat(length - str.length) + str; export const padRight = (str, length, char = ' ') => str.length >= length ? str : str + char.repeat(length - str.length); export const removeWhitespace = (str) => str.replace(/\s+/g, ''); export const reverse = (str) => str.split('').reverse().join(''); export const contains = (str, substring) => str.indexOf(substring) !== -1; export const startsWith = (str, prefix) => str.startsWith(prefix); export const endsWith = (str, suffix) => str.endsWith(suffix); //# sourceMappingURL=stringUtil.js.map