index.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. export = utility;
  2. export as namespace utility;
  3. // ---------------------------------
  4. /**
  5. * @description Types definition by github@ddzy
  6. * @see https://github.com/node-modules/utility
  7. */
  8. // ------------------------------------
  9. declare namespace utility {
  10. /**
  11. * ---------------0_0----------------
  12. * @description Defines For Array
  13. * @see https://github.com/node-modules/utility
  14. * ---------------0^0----------------
  15. */
  16. /**
  17. * Static object define
  18. */
  19. type ObjStatic = { [key: string]: any };
  20. /**
  21. * Array random slice with items count.
  22. * @param {Array} arr
  23. * @param {Number} num, number of sub items.
  24. * @return {Array}
  25. */
  26. function randomSlice(
  27. arr: any[],
  28. num?: number,
  29. ): any[];
  30. /**
  31. * Remove one exists element from an array
  32. * @param {Array} arr
  33. * @param {Number} index - remove element index
  34. * @return {Array} the array instance
  35. */
  36. function spliceOne(
  37. arr: any[],
  38. index: number,
  39. ): any[];
  40. /**
  41. * --------------------0_0----------------
  42. * @description Defines For Crypto
  43. * @see https://github.com/node-modules/utility#md5
  44. * --------------0^0------------------
  45. */
  46. /**
  47. * hash
  48. *
  49. * @param {String} method hash method, e.g.: 'md5', 'sha1'
  50. * @param {String|Buffer|Object} s
  51. * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
  52. * @return {String} md5 hash string
  53. * @public
  54. */
  55. function hash(
  56. method: 'md5' | 'sha1',
  57. s: string | Buffer | Object,
  58. format?: 'hex' | 'base64',
  59. ): string;
  60. /**
  61. * md5 hash
  62. *
  63. * @param {String|Buffer|Object} s
  64. * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
  65. * @return {String} md5 hash string
  66. * @public
  67. */
  68. function md5(
  69. s: string | Buffer | Object,
  70. format?: 'hex' | 'base64',
  71. ): string;
  72. /**
  73. * sha1 hash
  74. *
  75. * @param {String|Buffer|Object} s
  76. * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
  77. * @return {String} sha1 hash string
  78. * @public
  79. */
  80. function sha1(
  81. s: string | Buffer | Object,
  82. format?: 'hex' | 'base64',
  83. ): string;
  84. /**
  85. * sha256 hash
  86. *
  87. * @param {String|Buffer|Object} s
  88. * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
  89. * @return {String} sha256 hash string
  90. * @public
  91. */
  92. function sha256(
  93. s: string | Buffer | Object,
  94. format?: 'hex' | 'base64',
  95. ): string;
  96. /**
  97. * HMAC algorithm.
  98. *
  99. * Equal bash:
  100. *
  101. * ```bash
  102. * $ echo -n "$data" | openssl dgst -binary -$algorithm -hmac "$key" | openssl $encoding
  103. * ```
  104. *
  105. * @param {String} algorithm, dependent on the available algorithms supported by the version of OpenSSL on the platform.
  106. * Examples are 'sha1', 'md5', 'sha256', 'sha512', etc.
  107. * On recent releases, `openssl list-message-digest-algorithms` will display the available digest algorithms.
  108. * @param {String} key, the hmac key to be used.
  109. * @param {String|Buffer} data, content string.
  110. * @param {String} [encoding='base64']
  111. * @return {String} digest string.
  112. */
  113. function hmac(
  114. algorithm: string,
  115. key: string,
  116. data: string | Buffer,
  117. encoding?: 'base64' | string,
  118. ): string;
  119. /**
  120. * Base64 encode string.
  121. *
  122. * @param {String|Buffer} s
  123. * @param {Boolean} [urlsafe=false] Encode string s using a URL-safe alphabet,
  124. * which substitutes - instead of + and _ instead of / in the standard Base64 alphabet.
  125. * @return {String} base64 encode format string.
  126. */
  127. function base64encode(
  128. s: string | Buffer,
  129. urlsafe?: boolean,
  130. ): string;
  131. /**
  132. * Base64 string decode.
  133. *
  134. * @param {String} encode, base64 encoding string.
  135. * @param {Boolean} [urlsafe=false] Decode string s using a URL-safe alphabet,
  136. * which substitutes - instead of + and _ instead of / in the standard Base64 alphabet.
  137. * @param {encoding} [encoding=utf8] if encoding = buffer, will return Buffer instance
  138. * @return {String|Buffer} plain text.
  139. */
  140. function base64decode(
  141. encode: string,
  142. urlsafe?: boolean,
  143. encoding?: 'utf8' | 'buffer',
  144. ): string | Buffer;
  145. /**
  146. * ----------------0_0-----------------
  147. * @description Defines For Date
  148. * @see https://github.com/node-modules/utility#date-utils
  149. * ---------------0^0------------------
  150. */
  151. interface IYYYYMMDDHHmmssStaticOptions {
  152. dateSep?: string,
  153. timeSep?: string,
  154. }
  155. interface IDateStructStaticReturns {
  156. YYYYMMDD: number,
  157. H: number,
  158. }
  159. /**
  160. * Access log format date. format: `moment().format('DD/MMM/YYYY:HH:mm:ss ZZ')`
  161. *
  162. * @return {String}
  163. */
  164. function accessLogDate(d: Date): string;
  165. /**
  166. * Normal log format date. format: `moment().format('YYYY-MM-DD HH:mm:ss.SSS')`
  167. *
  168. * @return {String}
  169. */
  170. function logDate(
  171. d: string | Date,
  172. msSep?: string,
  173. ): string;
  174. /**
  175. * `moment().format('YYYY-MM-DD HH:mm:ss')` format date string.
  176. *
  177. * @return {String}
  178. */
  179. function YYYYMMDDHHmmss(
  180. d: Date | string,
  181. options?: IYYYYMMDDHHmmssStaticOptions,
  182. ): string;
  183. /**
  184. * `moment().format('YYYY-MM-DD')` format date string.
  185. *
  186. * @return {String}
  187. */
  188. function YYYYMMDD(
  189. d: string | Date,
  190. sep?: string,
  191. ): string;
  192. /**
  193. * return datetime struct.
  194. *
  195. * @return {Object} date
  196. * - {Number} YYYYMMDD, 20130401
  197. * - {Number} H, 0, 1, 9, 12, 23
  198. */
  199. function datestruct(
  200. now?: Date,
  201. ): IDateStructStaticReturns;
  202. /**
  203. * Get Unix's timestamp in seconds.
  204. * @return {Number}
  205. */
  206. function timestamp(
  207. t?: string | number,
  208. ): number | Date;
  209. /**
  210. * ---------------0_0-------------------
  211. * @description Defines For Function Method
  212. * @see https://github.com/node-modules/utility#others
  213. * ---------------0^0--------------------
  214. */
  215. /**
  216. * A empty function.
  217. *
  218. * @return {Function}
  219. * @public
  220. */
  221. function noop(): () => any;
  222. /**
  223. * Get a function parameter's names.
  224. *
  225. * @param {Function} func
  226. * @param {Boolean} [useCache], default is true
  227. * @return {Array} names
  228. */
  229. function getParamNames(
  230. func: (...args: any[]) => any,
  231. cache?: boolean,
  232. ): string[];
  233. /**
  234. * ----------------0_0-----------------------
  235. * @description Defines For JSON methods
  236. * @see https://github.com/node-modules/utility#json
  237. * -----------------0^0-----------------------
  238. */
  239. interface IJSONStaticOptions {
  240. space?: number | string,
  241. replacer?: (
  242. key: string,
  243. value: any,
  244. ) => any,
  245. }
  246. function strictJSONParse(
  247. str: string,
  248. ): ObjStatic;
  249. function readJSONSync(
  250. filepath: string,
  251. ): ObjStatic;
  252. function writeJSONSync(
  253. filepath: string,
  254. str: string | ObjStatic,
  255. options?: IJSONStaticOptions,
  256. ): void;
  257. function readJSON(
  258. filepath: string,
  259. ): Promise<any>;
  260. function writeJSON(
  261. filepath: string,
  262. str: string | ObjStatic,
  263. options?: IJSONStaticOptions,
  264. ): Promise<any>;
  265. function mkdir(
  266. dir: string,
  267. ): Promise<any>;
  268. /**
  269. * ------------------0_0------------------------
  270. * @description Defines For Number Methods
  271. * @see https://github.com/node-modules/utility#number-utils
  272. * --------------------0^0----------------------
  273. */
  274. /**
  275. * CONSTANTS STATIC
  276. */
  277. const MAX_SAFE_INTEGER: number;
  278. const MIN_SAFE_INTEGER: number;
  279. const MAX_SAFE_INTEGER_STR: string;
  280. const MAX_SAFE_INTEGER_STR_LENGTH: number;
  281. /**
  282. * Detect a number string can safe convert to Javascript Number.
  283. *
  284. * @param {String} s number format string, like `"123"`, `"-1000123123123123123123"`
  285. * @return {Boolean}
  286. */
  287. function isSafeNumberString(
  288. s: string,
  289. ): boolean;
  290. /**
  291. * Convert string to Number if string in safe Number scope.
  292. *
  293. * @param {String} s number format string.
  294. * @return {Number|String} success will return Number, otherise return the original string.
  295. */
  296. function toSafeNumber(
  297. s: string | number,
  298. ): number | string;
  299. /**
  300. * Produces a random integer between the inclusive `lower` and `upper` bounds.
  301. *
  302. * @param {Number} lower The lower bound.
  303. * @param {Number} upper The upper bound.
  304. * @return {Number} Returns the random number.
  305. */
  306. function random(
  307. lower?: number,
  308. upper?: number,
  309. ): number;
  310. /**
  311. * ------------------0_0--------------------------
  312. * @description Defines For Object Methods
  313. * @see https://github.com/node-modules/utility#objectassign
  314. * -------------------0^0------------------------
  315. */
  316. /**
  317. * High performance assign before node6
  318. * @param {Object} target - target object
  319. * @param {Object | Array} objects - object assign from
  320. * @return {Object} - return target object
  321. */
  322. function assign(
  323. target: ObjStatic,
  324. objects: ObjStatic | any[],
  325. ): ObjStatic;
  326. function has(
  327. obj: ObjStatic,
  328. prop: string,
  329. ): boolean;
  330. function getOwnEnumerables(
  331. obj: ObjStatic,
  332. ignoreNull?: boolean,
  333. ): string[];
  334. /**
  335. * generate a real map object(clean object), no constructor, no __proto__
  336. * @param {Object} [obj] - init object, optional
  337. * @return {Object}
  338. */
  339. function map(
  340. obj?: ObjStatic,
  341. ): ObjStatic;
  342. /**
  343. * -----------------0_0---------------------------
  344. * @description Defines For Optimize Methods
  345. * @see https://github.com/node-modules/utility#argumentstoarray
  346. * -----------------0^0------------------------
  347. */
  348. interface ITryStaticReturns {
  349. error: Error | undefined,
  350. value: any,
  351. }
  352. const UNSTABLE_METHOD: {
  353. /**
  354. * optimize try catch
  355. * @param {Function} fn
  356. * @return {Object}
  357. * - {Error} error
  358. * - {Mix} value
  359. */
  360. try: (
  361. fn: (...args: any[]) => any,
  362. ) => ITryStaticReturns,
  363. };
  364. /**
  365. * avoid if (a && a.b && a.b.c)
  366. * @param {Object} obj
  367. * @param {...String} keys
  368. * @return {Object}
  369. */
  370. function dig(
  371. obj: ObjStatic,
  372. ...args: any[],
  373. ): any;
  374. /**
  375. * optimize arguments to array
  376. * @param {Arguments} args
  377. * @return {Array}
  378. */
  379. function argumentsToArray(
  380. ...args: any[],
  381. ): any[];
  382. /**
  383. * -------------------0_0---------------------
  384. * @description Defines For Polyfill Methods
  385. * @see https://github.com/node-modules/utility#timers
  386. * -------------------0^0-------------------
  387. */
  388. function setImmediate(
  389. callback: (...args: any[]) => void,
  390. ...args: any[],
  391. ): NodeJS.Immediate;
  392. function setImmediate(
  393. fn: (...args: any[]) => any,
  394. ...args: any[],
  395. ): void;
  396. /**
  397. * ------------------0_0--------------------
  398. * @description Defines For String Methods
  399. * @see https://github.com/node-modules/utility#others
  400. * -------------------0^0---------------------
  401. */
  402. interface IReplaceInvalidHttpHeaderCharReturns {
  403. val: string,
  404. invalid: boolean,
  405. }
  406. function randomString(
  407. length?: number,
  408. charSet?: string | string[],
  409. ): string;
  410. /**
  411. * split string to array
  412. * @param {String} str
  413. * @param {String} [sep] default is ','
  414. * @return {Array}
  415. */
  416. function split(
  417. str: string,
  418. sep?: string,
  419. ): string[];
  420. /**
  421. * always optimized
  422. */
  423. function splitAlwaysOptimized(
  424. ...args: any[],
  425. ): string[];
  426. /**
  427. * Replace string
  428. *
  429. * @param {String} str
  430. * @param {String|RegExp} substr
  431. * @param {String|Function} newSubstr
  432. * @return {String}
  433. */
  434. function replace(
  435. str: string,
  436. substr: string | RegExp,
  437. newSubstr: string | ((...args: any[]) => any),
  438. ): string;
  439. /**
  440. * Replace invalid http header characters with replacement
  441. *
  442. * @param {String} val
  443. * @param {String|Function} replacement - can be `function(char)`
  444. * @return {Object}
  445. */
  446. function replaceInvalidHttpHeaderChar(
  447. val: string,
  448. replacement?: string | ((...args: any[]) => any)
  449. ): IReplaceInvalidHttpHeaderCharReturns;
  450. /**
  451. * Detect invalid http header characters in a string
  452. *
  453. * @param {String} val
  454. * @return {Boolean}
  455. */
  456. function includesInvalidHttpHeaderChar(
  457. val: string,
  458. ): boolean;
  459. /**
  460. * ------------------0_0----------------------
  461. * @description Defines For Web Methods
  462. * @see https://github.com/node-modules/utility#decode-and-encode
  463. * ------------------0^0------------------------
  464. */
  465. /**
  466. * Escape the given string of `html`.
  467. *
  468. * @param {String} html
  469. * @return {String}
  470. * @public
  471. */
  472. function escape(
  473. test: string,
  474. ): string;
  475. /**
  476. * Unescape the given string from html
  477. * @param {String} html
  478. * @param {String} type
  479. * @return {String}
  480. * @public
  481. */
  482. function unescape(
  483. html: string,
  484. type?: string,
  485. ): string | ObjStatic;
  486. /**
  487. * Safe encodeURIComponent, won't throw any error.
  488. * If `encodeURIComponent` error happen, just return the original value.
  489. *
  490. * @param {String} text
  491. * @return {String} URL encode string.
  492. */
  493. function encodeURIComponent(
  494. text: string,
  495. ): string;
  496. /**
  497. * Safe decodeURIComponent, won't throw any error.
  498. * If `decodeURIComponent` error happen, just return the original value.
  499. *
  500. * @param {String} encodeText
  501. * @return {String} URL decode original string.
  502. */
  503. function decodeURIComponent(
  504. encodeText: string,
  505. ): string;
  506. }