UNPKG

atomic-fns

Version:

Like Lodash, but for ESNext and with types. Stop shipping code built for browsers from 2015.

286 lines (284 loc) 11.3 kB
/** * This module includes global functions and constants. * * @module globals */ /** * A generic function type with arbitrary arguments and return. */ export declare type Function<TReturn = any> = (...args: any) => TReturn; /** This is just a noop function. */ export declare function noop(): void; /** A function that always returns `true`. */ export declare const True: () => boolean; /** A function that always returns `false`. */ export declare const False: () => boolean; /** * A custom error class that inherits from the Error object. * @class CustomError * @extends {Error} */ export declare class CustomError extends Error { /** * Returns a new CustomError with the specified message. * @param {string} message * @constructor */ constructor(message?: string); } /** * Custom error to signal an invalid index access. * @extends {CustomError} */ export declare class IndexError extends CustomError { } /** * Raised when a system function returns an I/O failure such as "file not found" * or "disk full". * @extends {CustomError} */ export declare class IOError extends CustomError { } /** * Custom error to signal an invalid key access. * @extends {CustomError} */ export declare class KeyError extends CustomError { } /** * Custom error to signal an invalid operation. * @extends {CustomError} */ export declare class NotImplementedError extends CustomError { } /** * Custom error to signal an invalid value. * @extends {CustomError} */ export declare class ValueError extends CustomError { } /** * Raised when the second argument of a division or modulo operation is zero. * @extends {CustomError} */ export declare class ZeroDivisionError extends CustomError { } /** * Returns the true type of any value with correct detection for `null`, `Array`, * `Object`, `Promise`, `Symbol`, and `NaN`. The correct values are inferred from {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag | Symbol.toStringTag} and the value's prototype. * @param value * @returns The value type. */ export declare function type(value: any): string; /** * Returns a string representing the object. * @param obj * @returns `obj.toString()` or `''` if `obj` is `null` or `undefined`. * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString | Object.toString } */ export declare function str(obj: any): any; /** Check if value is a boolean type. */ export declare function isBool(x: any): x is boolean; /** Check if value is an `Error` type. */ export declare function isError(x: any): x is Error; /** Check if value is an iterable type. */ export declare function isIterable(x: any): x is IterableIterator<any>; /** Check if value is a native `Date` type. */ export declare function isDate(x: any): x is Date; /** Check if value is an object type. */ export declare function isObject(x: any): x is Object; /** Check if value is a string type. */ export declare function isString(x: any): x is string; /** Check if value is an Array type. */ export declare function isArray(x: any): x is any[]; /** * Check if value is Array-like type. * A value is considered array-like if it's not a function and has a `.length` number property. */ export declare function isArrayLike(x: any): boolean; /** Check if value is a number type. */ export declare function isNumber(x: any): x is number; /** Check if value is an integer number type. */ export declare function isInteger(x: any): x is number; /** Check if value is a bigint type. */ export declare function isBigint(x: any): x is BigInt; /** Check if value is a Promise type. */ export declare function isPromise(x: any): x is Promise<any>; /** Check if value is a function type. */ export declare function isFunction(x: any): x is Function; /** Check if value is an async function type. */ export declare function isAsyncFunction(x: any): boolean; /** Check if value is a generator function type. */ export declare function isGenerator(x: any): x is Generator; /** Check if value is `null` or `undefined`. */ export declare function isNull(x: any): x is null | undefined; /** Check if value === `undefined`. */ export declare function isUndefined(x: any): x is undefined; /** Check if value is not `null` or `undefined`. */ export declare function notNull(x: any): boolean; /** Returns `true` for objects without length or falsy values. */ export declare function isEmpty(x: any): boolean; /** Check if value is a `RegExp` type */ export declare function isRegExp(x: any): x is RegExp; /** Check if value is a `Symbol` type */ export declare function isSymbol(x: any): x is symbol; /** Check if value is a `Set` type. */ export declare function isSet<T>(x: any): x is Set<T>; /** Check if value is a `Map` type. */ export declare function isMap<K, V>(x: any): x is Map<K, V>; /** Check if value is a `WeakSet` type. */ export declare function isWeakSet<T extends object>(x: any): x is WeakSet<T>; /** Check if value is a `WeakMap` type. */ export declare function isWeakMap<K extends object, V>(x: any): x is WeakMap<K, V>; /** * Returns the number of elements in a collection type. * * If `value.length` or `value.size()` is defined, this will be returned. * If `value` is an `Object`, it returns the number of keys. * @param value * @returns The number of elements in the collection or `undefined`. */ export declare function len(value: any): number; /** * Generates a unique ID using random numbers. If a prefix is given, the ID is appended to it and returned as a string. * @param {string} [pre=''] The id prefix. * @returns {number|string} Returns the numeric or string ID. * @example ```js uniqueId() // 3445556877 uniqueId('user_') // 'user_1033763188' ``` */ export declare function uniqueId(pre?: string): string; /** * Generates a monotonically increasing, pseudo-random based uuid-v4 string. These are sortable, url-friendly and are 100% unique if the IDs are generated more than 1 millisecond apart. If two IDs are generated at the same millisecond, the chance of collision is still pretty low (1 in 10^15). * @returns Returns a GUID formatted string. * @example ```js uuid() // '183f8ef3-b8f0-4000-81f5-89234c836d00' ``` */ export declare function uuid(): string; /** * Checks if `obj.key` is a function, and calls it with any given `args`. * @param {*} obj * @param {PropertyKey} key * @param {...any[]} args * @return `obj.key(...args)` or `undefined`. */ export declare function call(obj: any, key: PropertyKey, ...args: any[]): any; /** Convert number to unicode character */ export declare function chr(x: number): string; /** Convert character to Unicode code point */ export declare function ord(x: string): number; /** * Returns an array of the own enumerable property names of `object`. * @param object The given object. * @returns The array of object keys. * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys Object.keys()} */ export declare function keys<T>(object: T): (keyof T)[]; /** * Returns an array of the own enumerable property values of `object`. * @param object The given object. * @returns The array of object keys. * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values Object.values()} */ export declare function values(object: any): unknown[]; /** * Check if a given property is present in a given object. * @param obj The object to check. * @param attr A property key name. * @returns `true` if the object has the property name. */ export declare function has(obj: any, attr: PropertyKey): boolean; /** * Check if the attribute is present in the object or return a default value instead. * @param {PropertyKey} key * @param {*} obj * @param {*} [value=undefined] * @return The property value or default value. */ export declare function get<T>(key: PropertyKey, obj: T, value?: any): any; export declare function get<T>(key: PropertyKey[], obj: T, value?: any): any; /** * Deletes a given property key from an object. * @param attr The property name. * @param x The given object. * @returns The modified object. */ export declare function del<T>(attr: PropertyKey, x: T): T; /** * Adds a property to an object, or modifies the existing value. * @param attr The name or Symbol of the property to be defined or modified. * @param obj The container object. * @param value The value associated with the property * @param {boolean} [writable=false] If `true` the value may be changed later. Defaults to `false`. * @param {boolean} [enumerable=false] If `true` this property shows up during enumeration of the properties in the object. Defaults to `false`. * @returns */ export declare function set(attr: PropertyKey, obj: any, value: any, writable?: boolean, enumerable?: boolean): any; export declare const HASH_KEY: unique symbol; /** * Return a hash value for a given object. The hash will be the same for subsequent invocations. * @param obj The given object. * @returns The hash value. */ export declare function hash(obj: string): number; export declare function hash<T>(obj: T): T; /** * A hash value generator for strings. * @param {string} str The given string. * @returns {number} The hash value of the string. * @see {@link http://isthe.com/chongo/tech/comp/fnv/ FNV hash} */ export declare function hashCode(str: string): number; /** * Converts a number to a binary string. * @param n The given number. * @returns The binary string representation of `n`. */ export declare function bin(n: number): string; /** * Converts a number to a hexadecimal string. * @param n The given number. * @returns The hexadecimal string representation of `n`. */ export declare function hex(n: number): string; /** * Converts a number to an octal string. * @param n The given number. * @returns The octal string representation of `n`. */ export declare function oct(n: number): string; /** * Returns a floating point number constructed from a number or string `x`. * @param x The number or string. * @returns A floating point number parsed from the given value or `NaN`. * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat parseFloat} */ export declare function float(x: string): number; /** * Returns an integer number constructed from a number or string `x`. * @param x The number or string. * @returns An integer number parsed from the given value or `NaN`. * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt parseInt} */ export declare function int(x: string, base?: number): number; /** * Converts the value to an array using `Array.from()`. * @param value * @returns A new array value. * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from Array.from()} */ export declare function list<T>(value?: any): T[]; /** * Retrieve the next item from the iterator by calling its `next()` method. * @param iter The iterator. * @returns The next value from the iterator. */ export declare function next<T>(iter: Iterator<T>): T | undefined; export declare function next<T>(iter: Generator<any, T>): T | undefined;