123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- export = utility;
- export as namespace utility;
- // ---------------------------------
- /**
- * @description Types definition by github@ddzy
- * @see https://github.com/node-modules/utility
- */
- // ------------------------------------
- declare namespace utility {
- /**
- * ---------------0_0----------------
- * @description Defines For Array
- * @see https://github.com/node-modules/utility
- * ---------------0^0----------------
- */
- /**
- * Static object define
- */
- type ObjStatic = { [key: string]: any };
- /**
- * Array random slice with items count.
- * @param {Array} arr
- * @param {Number} num, number of sub items.
- * @return {Array}
- */
- function randomSlice(
- arr: any[],
- num?: number,
- ): any[];
- /**
- * Remove one exists element from an array
- * @param {Array} arr
- * @param {Number} index - remove element index
- * @return {Array} the array instance
- */
- function spliceOne(
- arr: any[],
- index: number,
- ): any[];
- /**
- * --------------------0_0----------------
- * @description Defines For Crypto
- * @see https://github.com/node-modules/utility#md5
- * --------------0^0------------------
- */
- /**
- * hash
- *
- * @param {String} method hash method, e.g.: 'md5', 'sha1'
- * @param {String|Buffer|Object} s
- * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
- * @return {String} md5 hash string
- * @public
- */
- function hash(
- method: 'md5' | 'sha1',
- s: string | Buffer | Object,
- format?: 'hex' | 'base64',
- ): string;
- /**
- * md5 hash
- *
- * @param {String|Buffer|Object} s
- * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
- * @return {String} md5 hash string
- * @public
- */
- function md5(
- s: string | Buffer | Object,
- format?: 'hex' | 'base64',
- ): string;
- /**
- * sha1 hash
- *
- * @param {String|Buffer|Object} s
- * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
- * @return {String} sha1 hash string
- * @public
- */
- function sha1(
- s: string | Buffer | Object,
- format?: 'hex' | 'base64',
- ): string;
- /**
- * sha256 hash
- *
- * @param {String|Buffer|Object} s
- * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
- * @return {String} sha256 hash string
- * @public
- */
- function sha256(
- s: string | Buffer | Object,
- format?: 'hex' | 'base64',
- ): string;
- /**
- * HMAC algorithm.
- *
- * Equal bash:
- *
- * ```bash
- * $ echo -n "$data" | openssl dgst -binary -$algorithm -hmac "$key" | openssl $encoding
- * ```
- *
- * @param {String} algorithm, dependent on the available algorithms supported by the version of OpenSSL on the platform.
- * Examples are 'sha1', 'md5', 'sha256', 'sha512', etc.
- * On recent releases, `openssl list-message-digest-algorithms` will display the available digest algorithms.
- * @param {String} key, the hmac key to be used.
- * @param {String|Buffer} data, content string.
- * @param {String} [encoding='base64']
- * @return {String} digest string.
- */
- function hmac(
- algorithm: string,
- key: string,
- data: string | Buffer,
- encoding?: 'base64' | string,
- ): string;
- /**
- * Base64 encode string.
- *
- * @param {String|Buffer} s
- * @param {Boolean} [urlsafe=false] Encode string s using a URL-safe alphabet,
- * which substitutes - instead of + and _ instead of / in the standard Base64 alphabet.
- * @return {String} base64 encode format string.
- */
- function base64encode(
- s: string | Buffer,
- urlsafe?: boolean,
- ): string;
- /**
- * Base64 string decode.
- *
- * @param {String} encode, base64 encoding string.
- * @param {Boolean} [urlsafe=false] Decode string s using a URL-safe alphabet,
- * which substitutes - instead of + and _ instead of / in the standard Base64 alphabet.
- * @param {encoding} [encoding=utf8] if encoding = buffer, will return Buffer instance
- * @return {String|Buffer} plain text.
- */
- function base64decode(
- encode: string,
- urlsafe?: boolean,
- encoding?: 'utf8' | 'buffer',
- ): string | Buffer;
- /**
- * ----------------0_0-----------------
- * @description Defines For Date
- * @see https://github.com/node-modules/utility#date-utils
- * ---------------0^0------------------
- */
- interface IYYYYMMDDHHmmssStaticOptions {
- dateSep?: string,
- timeSep?: string,
- }
- interface IDateStructStaticReturns {
- YYYYMMDD: number,
- H: number,
- }
- /**
- * Access log format date. format: `moment().format('DD/MMM/YYYY:HH:mm:ss ZZ')`
- *
- * @return {String}
- */
- function accessLogDate(d: Date): string;
- /**
- * Normal log format date. format: `moment().format('YYYY-MM-DD HH:mm:ss.SSS')`
- *
- * @return {String}
- */
- function logDate(
- d: string | Date,
- msSep?: string,
- ): string;
- /**
- * `moment().format('YYYY-MM-DD HH:mm:ss')` format date string.
- *
- * @return {String}
- */
- function YYYYMMDDHHmmss(
- d: Date | string,
- options?: IYYYYMMDDHHmmssStaticOptions,
- ): string;
- /**
- * `moment().format('YYYY-MM-DD')` format date string.
- *
- * @return {String}
- */
- function YYYYMMDD(
- d: string | Date,
- sep?: string,
- ): string;
- /**
- * return datetime struct.
- *
- * @return {Object} date
- * - {Number} YYYYMMDD, 20130401
- * - {Number} H, 0, 1, 9, 12, 23
- */
- function datestruct(
- now?: Date,
- ): IDateStructStaticReturns;
- /**
- * Get Unix's timestamp in seconds.
- * @return {Number}
- */
- function timestamp(
- t?: string | number,
- ): number | Date;
- /**
- * ---------------0_0-------------------
- * @description Defines For Function Method
- * @see https://github.com/node-modules/utility#others
- * ---------------0^0--------------------
- */
- /**
- * A empty function.
- *
- * @return {Function}
- * @public
- */
- function noop(): () => any;
- /**
- * Get a function parameter's names.
- *
- * @param {Function} func
- * @param {Boolean} [useCache], default is true
- * @return {Array} names
- */
- function getParamNames(
- func: (...args: any[]) => any,
- cache?: boolean,
- ): string[];
- /**
- * ----------------0_0-----------------------
- * @description Defines For JSON methods
- * @see https://github.com/node-modules/utility#json
- * -----------------0^0-----------------------
- */
- interface IJSONStaticOptions {
- space?: number | string,
- replacer?: (
- key: string,
- value: any,
- ) => any,
- }
- function strictJSONParse(
- str: string,
- ): ObjStatic;
- function readJSONSync(
- filepath: string,
- ): ObjStatic;
- function writeJSONSync(
- filepath: string,
- str: string | ObjStatic,
- options?: IJSONStaticOptions,
- ): void;
- function readJSON(
- filepath: string,
- ): Promise<any>;
- function writeJSON(
- filepath: string,
- str: string | ObjStatic,
- options?: IJSONStaticOptions,
- ): Promise<any>;
- function mkdir(
- dir: string,
- ): Promise<any>;
- /**
- * ------------------0_0------------------------
- * @description Defines For Number Methods
- * @see https://github.com/node-modules/utility#number-utils
- * --------------------0^0----------------------
- */
- /**
- * CONSTANTS STATIC
- */
- const MAX_SAFE_INTEGER: number;
- const MIN_SAFE_INTEGER: number;
- const MAX_SAFE_INTEGER_STR: string;
- const MAX_SAFE_INTEGER_STR_LENGTH: number;
- /**
- * Detect a number string can safe convert to Javascript Number.
- *
- * @param {String} s number format string, like `"123"`, `"-1000123123123123123123"`
- * @return {Boolean}
- */
- function isSafeNumberString(
- s: string,
- ): boolean;
- /**
- * Convert string to Number if string in safe Number scope.
- *
- * @param {String} s number format string.
- * @return {Number|String} success will return Number, otherise return the original string.
- */
- function toSafeNumber(
- s: string | number,
- ): number | string;
- /**
- * Produces a random integer between the inclusive `lower` and `upper` bounds.
- *
- * @param {Number} lower The lower bound.
- * @param {Number} upper The upper bound.
- * @return {Number} Returns the random number.
- */
- function random(
- lower?: number,
- upper?: number,
- ): number;
- /**
- * ------------------0_0--------------------------
- * @description Defines For Object Methods
- * @see https://github.com/node-modules/utility#objectassign
- * -------------------0^0------------------------
- */
- /**
- * High performance assign before node6
- * @param {Object} target - target object
- * @param {Object | Array} objects - object assign from
- * @return {Object} - return target object
- */
- function assign(
- target: ObjStatic,
- objects: ObjStatic | any[],
- ): ObjStatic;
- function has(
- obj: ObjStatic,
- prop: string,
- ): boolean;
- function getOwnEnumerables(
- obj: ObjStatic,
- ignoreNull?: boolean,
- ): string[];
- /**
- * generate a real map object(clean object), no constructor, no __proto__
- * @param {Object} [obj] - init object, optional
- * @return {Object}
- */
- function map(
- obj?: ObjStatic,
- ): ObjStatic;
- /**
- * -----------------0_0---------------------------
- * @description Defines For Optimize Methods
- * @see https://github.com/node-modules/utility#argumentstoarray
- * -----------------0^0------------------------
- */
- interface ITryStaticReturns {
- error: Error | undefined,
- value: any,
- }
- const UNSTABLE_METHOD: {
- /**
- * optimize try catch
- * @param {Function} fn
- * @return {Object}
- * - {Error} error
- * - {Mix} value
- */
- try: (
- fn: (...args: any[]) => any,
- ) => ITryStaticReturns,
- };
- /**
- * avoid if (a && a.b && a.b.c)
- * @param {Object} obj
- * @param {...String} keys
- * @return {Object}
- */
- function dig(
- obj: ObjStatic,
- ...args: any[],
- ): any;
- /**
- * optimize arguments to array
- * @param {Arguments} args
- * @return {Array}
- */
- function argumentsToArray(
- ...args: any[],
- ): any[];
- /**
- * -------------------0_0---------------------
- * @description Defines For Polyfill Methods
- * @see https://github.com/node-modules/utility#timers
- * -------------------0^0-------------------
- */
- function setImmediate(
- callback: (...args: any[]) => void,
- ...args: any[],
- ): NodeJS.Immediate;
- function setImmediate(
- fn: (...args: any[]) => any,
- ...args: any[],
- ): void;
- /**
- * ------------------0_0--------------------
- * @description Defines For String Methods
- * @see https://github.com/node-modules/utility#others
- * -------------------0^0---------------------
- */
- interface IReplaceInvalidHttpHeaderCharReturns {
- val: string,
- invalid: boolean,
- }
- function randomString(
- length?: number,
- charSet?: string | string[],
- ): string;
- /**
- * split string to array
- * @param {String} str
- * @param {String} [sep] default is ','
- * @return {Array}
- */
- function split(
- str: string,
- sep?: string,
- ): string[];
- /**
- * always optimized
- */
- function splitAlwaysOptimized(
- ...args: any[],
- ): string[];
- /**
- * Replace string
- *
- * @param {String} str
- * @param {String|RegExp} substr
- * @param {String|Function} newSubstr
- * @return {String}
- */
- function replace(
- str: string,
- substr: string | RegExp,
- newSubstr: string | ((...args: any[]) => any),
- ): string;
- /**
- * Replace invalid http header characters with replacement
- *
- * @param {String} val
- * @param {String|Function} replacement - can be `function(char)`
- * @return {Object}
- */
- function replaceInvalidHttpHeaderChar(
- val: string,
- replacement?: string | ((...args: any[]) => any)
- ): IReplaceInvalidHttpHeaderCharReturns;
- /**
- * Detect invalid http header characters in a string
- *
- * @param {String} val
- * @return {Boolean}
- */
- function includesInvalidHttpHeaderChar(
- val: string,
- ): boolean;
- /**
- * ------------------0_0----------------------
- * @description Defines For Web Methods
- * @see https://github.com/node-modules/utility#decode-and-encode
- * ------------------0^0------------------------
- */
- /**
- * Escape the given string of `html`.
- *
- * @param {String} html
- * @return {String}
- * @public
- */
- function escape(
- test: string,
- ): string;
- /**
- * Unescape the given string from html
- * @param {String} html
- * @param {String} type
- * @return {String}
- * @public
- */
- function unescape(
- html: string,
- type?: string,
- ): string | ObjStatic;
- /**
- * Safe encodeURIComponent, won't throw any error.
- * If `encodeURIComponent` error happen, just return the original value.
- *
- * @param {String} text
- * @return {String} URL encode string.
- */
- function encodeURIComponent(
- text: string,
- ): string;
- /**
- * Safe decodeURIComponent, won't throw any error.
- * If `decodeURIComponent` error happen, just return the original value.
- *
- * @param {String} encodeText
- * @return {String} URL decode original string.
- */
- function decodeURIComponent(
- encodeText: string,
- ): string;
- }
|