-
- /**
- * Convert websocket error code to human readable string message
- *
- * @param code - websocket error code
- * @returns human readable string message
- */
- public static getWebSocketCloseEventStatusString(code: number): string {
- if (code >= 0 && code <= 999) {
- return '(Unused)';
- } else if (code >= 1016) {
- if (code <= 1999) {
- return '(For WebSocket standard)';
- } else if (code <= 2999) {
- return '(For WebSocket extensions)';
- } else if (code <= 3999) {
- return '(For libraries and frameworks)';
- } else if (code <= 4999) {
- return '(For applications)';
+ if (fluctuationPercent === 0) {
+ return roundTo(staticValue, scale);
+ }
+ const fluctuationRatio = fluctuationPercent / 100;
+ return getRandomFloatRounded(
+ staticValue * (1 + fluctuationRatio),
+ staticValue * (1 - fluctuationRatio),
+ scale,
+ );
+};
+
+export const extractTimeSeriesValues = (timeSeries: Array<TimestampedData>): number[] => {
+ return timeSeries.map((timeSeriesItem) => timeSeriesItem.value);
+};
+
+export const isObject = (item: unknown): boolean => {
+ return (
+ isNullOrUndefined(item) === false && typeof item === 'object' && Array.isArray(item) === false
+ );
+};
+
+export const cloneObject = <T extends object>(object: T): T => {
+ return clone<T>(object);
+};
+
+export const hasOwnProp = (object: unknown, property: PropertyKey): boolean => {
+ return isObject(object) && Object.hasOwn(object as object, property);
+};
+
+export const isCFEnvironment = (): boolean => {
+ return !isNullOrUndefined(process.env.VCAP_APPLICATION);
+};
+
+export const isIterable = <T>(obj: T): boolean => {
+ return !isNullOrUndefined(obj) ? typeof obj[Symbol.iterator as keyof T] === 'function' : false;
+};
+
+const isString = (value: unknown): boolean => {
+ return typeof value === 'string';
+};
+
+export const isEmptyString = (value: unknown): boolean => {
+ return isNullOrUndefined(value) || (isString(value) && (value as string).trim().length === 0);
+};
+
+export const isNotEmptyString = (value: unknown): boolean => {
+ return isString(value) && (value as string).trim().length > 0;
+};
+
+export const isUndefined = (value: unknown): boolean => {
+ return value === undefined;
+};
+
+export const isNullOrUndefined = (value: unknown): boolean => {
+ // eslint-disable-next-line eqeqeq, no-eq-null
+ return value == null;
+};
+
+export const isEmptyArray = (object: unknown): boolean => {
+ return Array.isArray(object) && object.length === 0;
+};
+
+export const isNotEmptyArray = (object: unknown): boolean => {
+ return Array.isArray(object) && object.length > 0;
+};
+
+export const isEmptyObject = (obj: object): boolean => {
+ if (obj?.constructor !== Object) {
+ return false;
+ }
+ // Iterates over the keys of an object, if
+ // any exist, return false.
+ for (const _ in obj) {
+ return false;
+ }
+ return true;
+};
+
+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
+ * @returns delay in milliseconds
+ */
+export const exponentialDelay = (retryNumber = 0, maxDelayRatio = 0.2): number => {
+ const delay = Math.pow(2, retryNumber) * 100;
+ const randomSum = delay * maxDelayRatio * secureRandom(); // 0-20% of the delay
+ return delay + randomSum;
+};
+
+const isPromisePending = (promise: Promise<unknown>): boolean => {
+ return inspect(promise).includes('pending');
+};
+
+export const promiseWithTimeout = async <T>(
+ promise: Promise<T>,
+ timeoutMs: number,
+ timeoutError: Error,
+ timeoutCallback: () => void = () => {
+ /* This is intentional */
+ },
+): Promise<T> => {
+ // Create a timeout promise that rejects in timeout milliseconds
+ const timeoutPromise = new Promise<never>((_, reject) => {
+ setTimeout(() => {
+ if (isPromisePending(promise)) {
+ timeoutCallback();
+ // FIXME: The original promise shall be canceled