- /**
- * @param {number} [retryNumber=0]
- * @returns {number} - delay in milliseconds
- */
- static exponentialDelay(retryNumber = 0): number {
- const delay = Math.pow(2, retryNumber) * 100;
- const randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay
- return delay + randomSum;
- }
+export const clone = <T>(object: T): T => {
+ return structuredClone<T>(object)
+}
+
+/**
+ * Detects whether the given value is an asynchronous function or not.
+ *
+ * @param fn - Unknown value.
+ * @returns `true` if `fn` was an asynchronous function, otherwise `false`.
+ * @internal
+ */
+export const isAsyncFunction = (fn: unknown): fn is (...args: unknown[]) => Promise<unknown> => {
+ return is(Function, fn) && fn.constructor.name === 'AsyncFunction'
+}
+
+export const isObject = (value: unknown): value is object => {
+ return value != null && !Array.isArray(value) && is(Object, value)
+}
+
+export const hasOwnProp = (value: unknown, property: PropertyKey): boolean => {
+ return isObject(value) && Object.hasOwn(value, property)
+}
+
+export const isCFEnvironment = (): boolean => {
+ return env.VCAP_APPLICATION != null
+}
+
+export const isNotEmptyString = (value: unknown): value is string => {
+ return typeof value === 'string' && value.trim().length > 0
+}
+
+export const isNotEmptyArray = (value: unknown): value is unknown[] => {
+ return Array.isArray(value) && value.length > 0
+}
+
+export const insertAt = (str: string, subStr: string, pos: number): string =>
+ `${str.slice(0, pos)}${subStr}${str.slice(pos)}`
+
+/**
+ * Computes the retry delay in milliseconds using an exponential backoff algorithm.
+ *
+ * @param retryNumber - the number of retries that have already been attempted
+ * @param delayFactor - the base delay factor in milliseconds
+ * @returns delay in milliseconds
+ */
+export const exponentialDelay = (retryNumber = 0, delayFactor = 100): number => {
+ const delay = Math.pow(2, retryNumber) * delayFactor
+ const randomSum = delay * 0.2 * secureRandom() // 0-20% of the delay
+ return delay + randomSum
+}