@rudderstack/integrations-lib
Version:
A comprehensive TypeScript library providing shared utilities, SDKs, and tools for RudderStack integrations and destinations.
169 lines • 9.02 kB
TypeScript
import get from 'get-value';
import set from 'set-value';
interface DestinationMap {
[key: string]: string;
}
export { get, set };
export declare const isDefined: (x: unknown) => x is {} | null;
export declare const isNotEmpty: (x: unknown) => boolean;
export declare const isNotNull: (x: unknown) => x is {};
export declare const isDefinedAndNotNull: (x: unknown) => x is {};
export declare const isDefinedAndNotNullAndNotEmpty: (x: unknown) => boolean;
export declare const isBlank: (value: unknown) => boolean;
export declare const removeUndefinedValues: (obj: unknown) => unknown;
export declare const removeNullValues: (obj: unknown) => unknown;
export declare const removeUndefinedAndNullValues: (obj: unknown) => unknown;
export declare const removeUndefinedAndNullAndEmptyValues: (obj: unknown) => unknown;
/**
* The flattenMap function takes a collection as input and returns a flattened version of the collection. If the input is not an array or an object, it is returned as is. Otherwise, the function uses the flatMap function from the lodash library to flatten the collection.
* @param collection
* @returns
*/
export declare const flattenMap: (collection: unknown) => unknown;
export declare const base64Convertor: (string: string) => string;
export declare const isValidUrl: (url: string | URL) => URL | null;
export declare const stripTrailingSlash: (str: string) => string;
export declare const isPrimitive: (arg: unknown) => boolean;
/**
*
* @param {*} arg
* @returns {type}
*
* Returns type of passed arg
* for null argss returns "NULL" insted of "object"
*
*/
export declare const getType: (arg: unknown) => "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" | "NULL";
export declare const isObject: (value: unknown) => boolean;
export declare const isEmpty: (input: unknown) => boolean;
/**
* Returns true for empty object {}
* @param {*} obj
* @returns
*/
export declare const isEmptyObject: (obj: any) => boolean;
/**
* Function to check if value is Defined, Not null and Not Empty.
* Create this function, Because existing isDefinedAndNotNullAndNotEmpty(123) is returning false due to lodash _.isEmpty function.
* _.isEmpty is used to detect empty collections/objects and it will return true for Integer, Boolean values.
* ref: https://github.com/lodash/lodash/issues/496
* @param {*} value 123
* @returns yes
*/
export declare const isDefinedNotNullNotEmpty: (value: unknown) => boolean;
export declare const removeUndefinedNullEmptyExclBoolInt: (obj: unknown) => unknown;
export declare const getHashFromArray: (arrays: Array<DestinationMap>, fromKey?: string, toKey?: string, isLowerCase?: boolean) => Record<string, any>;
/**
* Format the destination.Config.dynamicMap arrays to hashMap
* where value is an array
* @param {} arrays [{"from":"prop1","to":"val1"},{"from":"prop1","to":"val2"},{"from":"prop2","to":"val2"}]
* @param {} fromKey="from"
* @param {} toKey="to"
* @param {} isLowerCase=true
* @param {} return hashmap {"prop1":["val1","val2"],"prop2":["val2"]}
*/
export declare const getHashFromArrayWithDuplicate: (arrays: Array<DestinationMap>, fromKey?: string, toKey?: string, isLowerCase?: boolean) => Record<string, any>;
/**
* Format the arrays to hashMap with key as `fromKey` and value as Object
* @param {*} arrays [{"id":"a0b8efe1-c828-4c63-8850-0d0742888f9d","name":"Email","type":"email","type_config":{},"date_created":"1662225840284","hide_from_guests":false,"required":false}]
* @param {*} fromKey name
* @param {*} isLowerCase false
* @returns // {"Email":{"id":"a0b8efe1-c828-4c63-8850-0d0742888f9d","name":"Email","type":"email","type_config":{},"date_created":"1662225840284","hide_from_guests":false,"required":false}}
*/
export declare const getHashFromArrayWithValueAsObject: (arrays: Array<DestinationMap>, fromKey?: string, isLowerCase?: boolean) => Record<string, any>;
/**
* Retrieves a value from a message object based on a given key. It searches for the key in the properties, traits, and context.traits of the message object in that order.
*
* @param {Object} options - The options object.
* @param {Object} options.message - The message object containing properties, traits, and context.traits.
* @param {string} options.key - The key to search for in the message object.
* @returns {*} The value found in the message object for the given key, or null if the key is not found or the value is null.
*/
export declare const getValueFromPropertiesOrTraits: ({ message, key }: {
message: any;
key: any;
}) => any;
/**
* Flattens a JSON object into a single-level object.
*
* @param data - The JSON object to be flattened.
* @param separator - The character used to separate the keys in the flattened object. Default is '.'.
* @param mode - The mode used to flatten arrays. If set to 'strict', the array indices will be included in the flattened keys. Default is 'normal'.
* @returns The flattened JSON object.
*/
export declare const flattenJson: (data: unknown, separator?: string, mode?: string) => Record<any, any>;
/**
* Get the offset value in seconds for a given timezone.
*
* @param value - The timezone value for which the offset needs to be calculated.
* @returns The offset value in seconds for the given timezone. If the timezone is invalid or not found, it returns `undefined`.
*
* @example
* const offset = getOffsetInSec('Asia/Calcutta');
* console.log(offset); // Output: 19800
*/
export declare const getOffsetInSec: (value: string) => number | undefined;
/**
* Converts a date string into a formatted timestamp.
* If a format string is provided, it uses the moment library to format the date.
* If no format string is provided, it returns the timestamp of the date.
* @param dateStr - The date string to be formatted.
* @param format - (Optional) The format string to be used for formatting the date.
* @returns The formatted date as a string if a format string is provided,
* otherwise the timestamp of the date as a number.
*/
export declare const formatTimeStamp: (dateStr: string, format?: string) => string | number;
export declare const hashToSha256: (value: string | Buffer | number[]) => string;
/**
* Generates a Universally Unique Identifier (UUID) using the `crypto.randomUUID()` method.
* The `disableEntropyCache` option is set to `true` to prevent caching of generated UUIDs.
*
* @returns {string} A randomly generated UUID.
*
* @see {@link https://nodejs.org/api/crypto.html#cryptorandomuuidoptions:~:text=options%20%3CObject%3E-,disableEntropyCache,-%3Cboolean%3E%20By}
*/
export declare const generateUUID: () => string;
export declare const convertToString: (value: any) => string;
export declare const uuidv5: (val: string) => string;
export declare const generateRandomString: (length?: number) => string;
/**
*
* @param {Record<string, any | any[]} qParams - Object containing parameters where values are arrays
* @returns {Record<string, any>} Flattened object where array values of length one are replaced with their first element
* @description
* Flattens an object by replacing array values of length one with their first element,
* while keeping non-array values or arrays of length more than one unchanged.
* In case of empty array, the key is removed from the output
*/
export declare const flattenQueryParams: (qParams: Record<string, any | any[]>) => Record<string, any>;
/**
* Locks a property of an object, making it immutable.
* @param obj - The object to lock the property of.
* @param key - The key of the property to lock.
* @returns The object with the property locked.
*/
export declare const lockProperty: <T extends object, K extends keyof T>(obj: T, key: K) => T;
/**
* Deep freezes an object, making it immutable.
* @param obj - The object to deep freeze.
* @returns {T | Readonly<T>} - If `propKey` is provided, returns the original object `T` with `obj[propKey]` deep frozen and `propKey` locked. Otherwise, returns a `Readonly<T>` version of the entire object.
*
* @example
* const config = deepFreeze({ api: { key: 'secret', timeout: 1000 } });
* // Attempting to modify config.api.key will throw an error in strict mode
*/
export declare const deepFreeze: <T extends Record<string, unknown>>(obj: T) => Readonly<T>;
/**
* Deep freezes a specific property of an object and locks that property from reassignment.
* @template T - The type of the object.
* @param {T} obj - The object containing the property to freeze.
* @param {keyof T} propKey - The key of the property to deep freeze and lock.
* @returns {T} - The original object `T` with `obj[propKey]` deep frozen and `propKey` locked.
*
* @example
* const myObject = { data: { value: 123 } };
* deepFreezeProperty(myObject, 'data');
* // myObject.data is now immutable, and myObject.data cannot be reassigned.
*/
export declare const deepFreezeProperty: <T extends Record<string, unknown>>(obj: T, propKey: keyof T) => T;
//# sourceMappingURL=misc.d.ts.map