UNPKG

topkat-utils

Version:

A comprehensive collection of TypeScript/JavaScript utility functions for common programming tasks. Includes validation, object manipulation, date handling, string formatting, and more. Zero dependencies, fully typed, and optimized for performance.

97 lines (96 loc) 5.76 kB
import { ObjectGeneric } from './types'; /**Eg: camelCase('hello', 'world') => 'helloWorld' */ export declare function camelCase(...wordBits: string[] | [string[]]): string; /** Replace 'hello-world', 'hello World', 'hello_World', 'helloWorld' => 'helloWorld' */ export declare function camelCaseify(word: string): string; /**Eg: snake_case * trimmed but not lowerCased */ export declare function snakeCase(...wordBits: string[] | [string[]]): string; /**Eg: kebab-case * trimmed AND lowerCased * undefined, null are removed */ export declare function kebabCase(...wordBits: string[] | [string[]]): string; /**Eg: PascalCase * undefined, null are removed */ export declare function pascalCase(...wordBits: string[] | [string[]]): string; /**Eg: Titlecase * undefined, null are removed */ export declare function titleCase(...wordBits: string[] | [string[]]): string; export declare function capitalize1st(str?: string, lowercaseTheRest?: boolean): string; export declare function camelCaseToWords(str: string): string[]; /** GIVEN A STRING '{ blah;2}, ['nested,(what,ever)']' AND A SEPARATOR ","" * This will return the content separated by first level of separators * @return ["{ blah;2}", "['nested,(what,ever)']"] */ export declare function getValuesBetweenSeparator(str: string, separator: string, removeTrailingSpaces?: boolean): any; /** GIVEN A STRING "a: [ 'str', /[^]]/, '[aa]]]str', () => [ nestedArray ] ], b: ['arr']" * @return matching: [ "'str', /[^]]/, '[aa]]]str', () => [ nestedArray ]", "'arr'" ], between: [ "a:", ", b: " ] * @param str base string * @param openingOrSeparator opening character OR separator if closing not set * @param closing * @param ignoreBetweenOpen default ['\'', '`', '"', '/'], when reaching an opening char, it will ignore all until it find the corresponding closing char * @param ignoreBetweenClose default ['\'', '`', '"', '/'] list of corresponding closing chars */ export declare function getValuesBetweenStrings(str: string, openingOrSeparator: any, closing: any, ignoreBetweenOpen?: string[], ignoreBetweenClose?: string[], removeTrailingSpaces?: boolean): { inner: any; outer: any; }; /** Remove accentued character from string and eventually special chars and numbers * @param {String} str input string * @param {Object} config { removeSpecialChars: false, removeNumbers: false, removeSpaces: false } * @returns String with all accentued char replaced by their non accentued version + config formattting */ export declare function convertAccentedCharacters(str: string, config?: { removeNumbers?: boolean; removeSpecialChars?: boolean; removeSpaces?: boolean; }): string; /** minLength 8 if unique * @param {Number} length default: 20 * @param {Boolean} unique default: true. Generate a real unique token base on the date. min length will be min 8 in this case * @param {string} mode one of ['alphanumeric', 'hexadecimal'] * NOTE: to generate a mongoDB Random Id, use the params: 24, true, 'hexadecimal' */ export declare function generateToken(length?: number, unique?: boolean, mode?: 'alphanumeric' | 'hexadecimal'): string; export declare function generateObjectId(): string; /** Useful to join differents bits of url with normalizing slashes * * urlPathJoin('https://', 'www.kikou.lol/', '/user', '//2//') => https://www.kikou.lol/user/2 * * urlPathJoin('http:/', 'kikou.lol') => https://www.kikou.lol */ export declare function urlPathJoin(...bits: string[]): string; /** @deprecated use urlPathJoin instead // file path using ONLY SLASH and not antislash on windows. Remove also starting and trailing slashes */ export declare function pathJoinSafe(...pathBits: string[]): string; export type MiniTemplaterOptions = { /** replacer for undefined values */ valueWhenNotSet: string; /** override default regexp that match content between `{{ }}`. It must be 'g' and first capturing group matching the value to replace. Default: /{{\s*([^}]*)\s*}}/g*/ regexp: RegExp; /** replacer for undefined values */ valueWhenContentUndefined: string; }; /** Replace variables in a string like: `Hello {{userName}}!` * @param {String} content * @param {Object} varz object with key => value === toReplace => replacer * @param {Object} options * * valueWhenNotSet => replacer for undefined values. Default: '' * * regexp => must be 'g' and first capturing group matching the value to replace. Default: /{{\s*([^}]*)\s*}}/g */ export declare function miniTemplater(content: string, varz: ObjectGeneric, options?: Partial<MiniTemplaterOptions>): string; /** Clean output for outside world. All undefined / null / NaN / Infinity values are changed to '-' */ export declare function cln(val: any, replacerInCaseItIsUndefinNaN?: string): any; export declare function nbOccurenceInString(baseString: string, searchedString: string, allowOverlapping?: boolean): number; /** typed lower case. Eg: if you pass 'A' | 'B', the resulting type will be 'a' | 'b' */ export declare function lowerCase<T extends string>(string: T): Lowercase<T>; /** typed upper case. Eg: if you pass 'a' | 'b', the resulting type will be 'A' | 'B' */ export declare function upperCase<T extends string>(string: T): Uppercase<T>; /** Parse strings like 'true', 'false', '123', 'null' to their real type equivalent. Actual string is returned if nothing matches. * * /!\ for typing please profide a type parameter like `parseStringVariable<boolean>('true')` */ export declare function parseStringVariable<T = any>(val: any): T; /** return val === 'true' || val === true */ export declare function parseStringAsBoolean(val: string | boolean | undefined): boolean; /** return Number(val) */ export declare function parseStringAsNumber(val: string | number): number;