index.d.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /// <reference types="node" />
  2. import { defaults, noop } from "./lodash";
  3. import { Callback } from "../types";
  4. import Debug from "./debug";
  5. /**
  6. * Convert a buffer to string, supports buffer array
  7. *
  8. * @example
  9. * ```js
  10. * const input = [Buffer.from('foo'), [Buffer.from('bar')]]
  11. * const res = convertBufferToString(input, 'utf8')
  12. * expect(res).to.eql(['foo', ['bar']])
  13. * ```
  14. */
  15. export declare function convertBufferToString(value: any, encoding?: BufferEncoding): any;
  16. /**
  17. * Convert a list of results to node-style
  18. *
  19. * @example
  20. * ```js
  21. * const input = ['a', 'b', new Error('c'), 'd']
  22. * const output = exports.wrapMultiResult(input)
  23. * expect(output).to.eql([[null, 'a'], [null, 'b'], [new Error('c')], [null, 'd'])
  24. * ```
  25. */
  26. export declare function wrapMultiResult(arr: unknown[] | null): unknown[][];
  27. /**
  28. * Detect if the argument is a int
  29. * @example
  30. * ```js
  31. * > isInt('123')
  32. * true
  33. * > isInt('123.3')
  34. * false
  35. * > isInt('1x')
  36. * false
  37. * > isInt(123)
  38. * true
  39. * > isInt(true)
  40. * false
  41. * ```
  42. */
  43. export declare function isInt(value: any): value is string;
  44. /**
  45. * Pack an array to an Object
  46. *
  47. * @example
  48. * ```js
  49. * > packObject(['a', 'b', 'c', 'd'])
  50. * { a: 'b', c: 'd' }
  51. * ```
  52. */
  53. export declare function packObject(array: any[]): Record<string, any>;
  54. /**
  55. * Return a callback with timeout
  56. */
  57. export declare function timeout<T>(callback: Callback<T>, timeout: number): Callback<T>;
  58. /**
  59. * Convert an object to an array
  60. * @example
  61. * ```js
  62. * > convertObjectToArray({ a: '1' })
  63. * ['a', '1']
  64. * ```
  65. */
  66. export declare function convertObjectToArray<T>(obj: Record<string, T>): (string | T)[];
  67. /**
  68. * Convert a map to an array
  69. * @example
  70. * ```js
  71. * > convertMapToArray(new Map([[1, '2']]))
  72. * [1, '2']
  73. * ```
  74. */
  75. export declare function convertMapToArray<K, V>(map: Map<K, V>): (K | V)[];
  76. /**
  77. * Convert a non-string arg to a string
  78. */
  79. export declare function toArg(arg: any): string;
  80. /**
  81. * Optimize error stack
  82. *
  83. * @param error actually error
  84. * @param friendlyStack the stack that more meaningful
  85. * @param filterPath only show stacks with the specified path
  86. */
  87. export declare function optimizeErrorStack(error: Error, friendlyStack: string, filterPath: string): Error;
  88. /**
  89. * Parse the redis protocol url
  90. */
  91. export declare function parseURL(url: string): Record<string, unknown>;
  92. interface TLSOptions {
  93. port: number;
  94. host: string;
  95. [key: string]: any;
  96. }
  97. /**
  98. * Resolve TLS profile shortcut in connection options
  99. */
  100. export declare function resolveTLSProfile(options: TLSOptions): TLSOptions;
  101. /**
  102. * Get a random element from `array`
  103. */
  104. export declare function sample<T>(array: T[], from?: number): T;
  105. /**
  106. * Shuffle the array using the Fisher-Yates Shuffle.
  107. * This method will mutate the original array.
  108. */
  109. export declare function shuffle<T>(array: T[]): T[];
  110. /**
  111. * Error message for connection being disconnected
  112. */
  113. export declare const CONNECTION_CLOSED_ERROR_MSG = "Connection is closed.";
  114. export declare function zipMap<K, V>(keys: K[], values: V[]): Map<K, V>;
  115. export { Debug, defaults, noop };