nope-js-browser
Version:
NoPE Runtime for the Browser. For nodejs please use nope-js-node
280 lines (279 loc) • 8.25 kB
TypeScript
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
export declare const SPLITCHAR = "/";
/**
* Function to recurvely get an Attribute of the Object.
*
* @export
* @example data = [{a:1},{a:2}]; rgetattr(data, "0/a") -> 0; rgetattr(data,"hallo", "default") -> "default"
* @param {*} _data Data, where the item should be received
* @param {string} _path The path to extract
* @param {*} [_default=_sentinel] Default Object, if nothing else is provided
* @returns {*} The extracted data.
*/
export declare function rgetattr<T = any>(_data: any, _path: string, _default?: any, _SPLITCHAR?: string): T | null;
/**
* Helper to query data from an object.
* @example data = [{a:1},{a:2}]; rqueryAttr(data, "+/a") -> [{path: "0/a", data: 0},{path: "1/a", data: 1}]
* @param data The data
* @param query The query to use.
* @returns Returns an array
*/
export declare function rqueryAttr<T>(data: any, query: string): {
path: string;
data: T;
}[];
/**
* Helper to query data from an object.
*
* props is defined as followed:
* ```typescript
* props: {
* key: string;
* query: string;
* }[]
* ```
*
* @example Example 1:
*
* ```javascript
*
* const data = { "deep": { "nested": "test" } };
* const result = convert_data(data, [
* {
* "key": "result",
* "query": "deep/nested",
* },
* ]);
*
* // ==> result = [{"result": "test"}]
* ```
*
* @example Example 2:
*
* ```javascript
* data = {
* "array": [
* {
* "data1": 0,
* "data2": "a",
* },
* {
* "data1": 1,
* "data2": "a",
* },
* ],
* "not": { "nested": "hello" }
* }
*
* let result = convert_data(data, [
* {
* "key": "a",
* "query": "array/+/data1",
* },
* {
* "key": "b",
* "query": "array/+/data2",
* },
* ])
*
* // ==> result = [{"a": 0, "b": "a"}, {"a": 1, "b": "a"}]
* ```
*
* @param data The data
* @param query The query to use.
* @returns Returns an array
*/
export declare function convertData<T>(data: any, props: {
key: string;
query: string;
}[]): T[];
/**
* Function to Set recursely a Attribute of an Object
*
* @author M.Karkowski
* @export
* @param {*} _data The Object, where the data should be stored
* @param {string} _path The Path of the Attribute. All are seprated by a the splitchar. (Defaults to'.' => For Instance 'a/b/0/a/c')
* @param {*} _value The Value which should be Stored in the Attribute.
* @param {string} [_SPLITCHAR=SPLITCHAR] The Splitchar to use. Defaults to "/"
*/
export declare function rsetattr(_data: any, _path: string, _value: any, _SPLITCHAR?: string): void;
/**
* Checks whether the Value is an Integer
*
* @export
* @param {*} value Value to be checked
* @returns {boolean} Result
*/
export declare function isInt(value: any): boolean;
/**
* Checks whether the Value is a Float
*
* @export
* @param {*} value Value to be checked
* @returns {boolean} Result
*/
export declare function isFloat(value: any): boolean;
/**
* Copys the Object. Creates a Deep-Copy
* of the Function
*
* @export
* @param {*} value The value which should be copied
* @returns {*} A Copy of the Value
*/
export declare function copy<T>(value: T): T;
/**
* Function Converts a Object to a Map.
*
* @export
* @param {*} _obj The Object which should be converted.
* @returns {Map<string,any>}
*/
export declare function objectToMap(_obj: any): Map<string, any>;
/**
* Checks whether the Value is an Object
*
* @export
* @param {*} value Data to Test
* @returns {boolean} Flag showing whether the Presented Data is an Object
*/
export declare function isObject(value: any): boolean;
/**
* Checks whether the Value is an Object
*
* @export
* @param {*} value Data to Test
* @returns {boolean} Flag showing whether the Presented Data is an Object
*/
export declare function isObjectOrArray(value: any): boolean;
/**
* Flattens an Object to a Map.
*
* For Instance:
*
* data = {a : { b : { c : 1, d: "hallo"}}}
*
* // Normal Call
* res = flatteObject(data)
* => res = {"a.b.c":1,"a.b.d":"hallo"}
*
* // With a Selected prefix 'additional.name'
* res = flatteObject(data,{prefix:'additional.name'})
* => res = {"additional.name.a.b.c":1,"additional.name.a.b.d":"hallo"}
*
* @export
* @param {*} data The Data that should be converted
* @param {string} [prefix=''] An additional prefix.
* @returns {Map<string, any>} The flatten Object
*/
export declare function flattenObject(data: any, options?: {
prefix?: string;
splitchar?: string;
maxDepth?: number;
onlyPathToSimpleValue?: boolean;
}): Map<string, any>;
/**
* Function, that will iterate over an object.
* It will call the callback on every element.
*
*
* @author M.Karkowski
* @export
* @param {*} obj The Object to iterate
* @param {string} [prefix=""] A prefix for the Path.
* @param {(
* path: string,
* data: any,
* parent?: string,
* level?: number
* ) => void} dataCallback Callback, that will be called.
* @param {string} [_SPLITCHAR=SPLITCHAR] The Splitchar to use, to generate the path
* @param {boolean} [_callOnlyOnValues=true] A Flag, to start the
* @param {*} [_maxDepth=Infinity] Determine the max Depth, after which the Iteration will be stopped.
* @param {string} [_parent=""] For Recursive call only
* @param {number} [_level=0] For Recursive call only
* @return {*} {*}
*/
export declare function recursiveForEach(obj: any, prefix: string, dataCallback: (path: string, data: any, parent?: string, level?: number) => void, _SPLITCHAR?: string, _callOnlyOnValues?: boolean, _maxDepth?: number, _parent?: string, _level?: number): any;
/**
* Exports the used Types of an Object. The result is the
* a Map, where the key represents the path and the value
* represents the type of the element (stored in the path)
*
* @author M.Karkowski
* @export
* @param {*} data The Data to check
* @param {{
* prefix?: string,
* splitchar?: string,
* maxDepth?: number,
* }} [options={}]
* @return {Map<string, string>} `key` = `path`; `value` = `type of element` as string;
*/
export declare function flattenObjectType(data: any, options?: {
prefix?: string;
splitchar?: string;
onlyPathToSimpleValue?: boolean;
maxDepth?: number;
}): Map<string, string>;
/**
* Deflattens an Dict Based Object. The Object it self is represented
* as Map, whereas the Key represents the path.
*
*
* @author M.Karkowski
* @export
* @param {Map<string, any>} _flattenObject
* @return {*} {*}
*/
export declare function deflattenObject(_flattenObject: Map<string, any>, options: {
prefix?: string;
splitchar?: string;
}): any;
/**
* Function for deeply assigning
*
* @export
* @param {*} target
* @param {*} source
* @returns
*/
export declare function deepAssign(target: any, source: any): any;
/**
* Function to deeply clone the given object.
*
* @author M.Karkowski
* @export
* @template T
* @param {T} obj
* @return {*} {T}
*/
export declare function deepClone<T>(obj: T): T;
/**
* Helper to get the Type of an Object.
* @param obj The Object
* @returns
*/
export declare function getType(obj: any): string;
/**
* Compares deep a and b.
* @param source The source item
* @param target The target item
* @param {number} maxDepth Max Depth, after which the test is skipped and the `onMaxDepth` value is returned
* @param {boolean} [onMaxDepth=false] Value to return if the maxDepth is reached.
* @returns
*/
export declare function deepEqual(a: any, b: any): boolean;
/**
* Function to adapt the Object and only return a specific amount of elements.
* @param obj The Object itself
* @param properties a list of properties/pathes to keep
*/
export declare function keepPropertiesOfObject(obj: any, properties: {
[index: string]: () => any;
}): any;