Package Exports
- @gilbarbara/helpers
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@gilbarbara/helpers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@gilbarbara/helpers
Collection of useful functions
Usage
npm i @gilbarbara/helpers
import { unique } from '@gilbarbara/helpers';
const password = unique(24, { includeSymbols: true });
console.log(password); // g9HBfQeeOgrP.V1?JhETxn9P
API
Arrays
sortByLocaleCompare(key?: string, options?: Intl.CollatorOptions & { descending?: boolean })
Returns a sort function with localeCompare comparison.
sortByPrimitive<T extends number | boolean>(key?: string, descending?: boolean = false)
Returns a sort function with primitive values comparison.
Async
cors(data: any, statusCode = 200, options?: CorsOptions) Returns a CORSresponse.
type HttpMethods = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
interface CorsOptions {
headers?: string[];
methods?: HttpMethods[];
origin?: string;
}
request(url: string, options?: RequestOptions)
Perform an async request.
type HttpMethods = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
interface RequestOptions {
body?: any;
headers?: PlainObject<string>;
method?: HttpMethods;
}
sleep(seconds?: number = 1)
Block async execution for X seconds.
Misc
logger(type: string, title: string, data: any, options?: LoggerOptions)
Log grouped messages to the console.
interface LoggerOptions {
collapsed?: boolean; // true
hideTimestamp?: boolean; // false
skip?: boolean; // false
typeColor?: string; // 'gray'
}
noop()
An empty function that does nothing.
required(param?: string = 'param')
Throw an error if the parameter isn't provided.
unique(length?: number = 8, options?: UniqueOptions)
Returns a random string.
interface UniqueOptions {
includeLowercase?: boolean; // true
includeNumbers?: boolean; // true
includeSymbols?: boolean; // false
includeUppercase?: boolean; // true
}
uuid()
Returns an UUID v4 string.
Numbers
ceil(input: number, digits?: number = 2)
Ceil decimal numbers.
floor(input: number, digits?: number = 2)
Floor decimal numbers.
pad(input: number, digits?: number = 2)
Pad a number with zeros.
randomInt(min?: number = 0, max?: number = 10)
Returns a random integer.
round(input: number, digits?: number = 2) Round decimal numbers.
Objects
blacklist(input: PlainObject, ...filter)
Remove properties from an object.
invertKeys(input: PlainObject)
Invert object key and value
keyMirror(input: PlainObject)
Set the key as the value
queryStringFormat(input: PlainObject, options?: QueryStringFormatOptions)
Stringify a shallow object into a query string.
interface QueryStringFormatOptions {
addPrefix?: boolean;
encodeValuesOnly?: boolean;
encoder?: (uri: string) => string;
}
queryStringParse(input: string)
Parse a query string.
sortObjectKeys(input: PlainObject)
Sort object keys
Strings
capitalize(input: string)
Capitalize the first letter.
cleanupHTML(input: string)
Cleanup HTML content.
cleanupURI(input: string)
Cleanup URI characters.
removeAccents(input: string)
Remove accents.
removeEmojis(input: string)
Remove emojis.
removeEmptyTags(input: string)
Remove empty HTML Tags (including whitespace).
removeNonPrintableCharacters(input: string)
Remove non-printable ASCII characters.
removeTags(input: string)
Remove HTML tags.
removeWhitespace(input: string)
Remove whitespace.
slugify(input: string)
Format string to slug.