@ts-common/source-map
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -59,4 +59,19 @@ import { StringMap } from "@ts-common/string-map"; | ||
export declare const getPath: (info: ObjectInfo) => ReadonlyArray<string | number>; | ||
export declare const cloneDeep: <T extends string | number | boolean | object | null>(source: T) => T; | ||
/** | ||
* Returns a deep clone of `source` and set a source-map for each member. | ||
* | ||
* @param source an original object | ||
* @param getInfoFunctOptional the function should return an object info of a provided member. | ||
* If the function is not provided the algorithm extract information from the provided member. | ||
*/ | ||
export declare const cloneDeep: <T extends string | number | boolean | object | null>(source: T, getInfoFuncOptional?: ((member: string | number | boolean | object | null | undefined) => InfoFunc | undefined) | undefined) => T; | ||
/** | ||
* Returns a deep clone of `source`. Each member of the returned object will contain the provided | ||
* source-map information. | ||
* | ||
* @param source | ||
* @param infoFunc | ||
*/ | ||
export declare const cloneDeepWithInfo: <T extends string | number | boolean | object | null>(source: T, infoFunc: InfoFunc | undefined) => T; | ||
/** | ||
* Get a file position | ||
@@ -63,0 +78,0 @@ * |
47
index.js
@@ -134,17 +134,38 @@ "use strict"; | ||
exports.getPath = (info) => _.reverse(_.filterMap(getReversedInfoIterator(info), i => i.isChild ? i.property : undefined)); | ||
exports.cloneDeep = (source) => { | ||
const data = source; | ||
if (data === null || | ||
typeof data === "boolean" || | ||
typeof data === "number" || | ||
typeof data === "string") { | ||
return source; | ||
} | ||
const result = Array.isArray(data) ? | ||
data.map(exports.cloneDeep) : | ||
sm.map(data, exports.cloneDeep); | ||
exports.copyInfo(data, result); | ||
return result; | ||
/** | ||
* Returns a deep clone of `source` and set a source-map for each member. | ||
* | ||
* @param source an original object | ||
* @param getInfoFunctOptional the function should return an object info of a provided member. | ||
* If the function is not provided the algorithm extract information from the provided member. | ||
*/ | ||
exports.cloneDeep = (source, getInfoFuncOptional) => { | ||
const get = getInfoFuncOptional === undefined ? exports.getInfoFunc : getInfoFuncOptional; | ||
const clone = (data) => { | ||
if (data === null || | ||
typeof data === "boolean" || | ||
typeof data === "number" || | ||
typeof data === "string") { | ||
return data; | ||
} | ||
const result = Array.isArray(data) ? | ||
data.map(clone) : | ||
sm.map(data, clone); | ||
const infoFunc = get(data); | ||
if (infoFunc !== undefined) { | ||
exports.setInfoFunc(result, infoFunc); | ||
} | ||
return result; | ||
}; | ||
return clone(source); | ||
}; | ||
/** | ||
* Returns a deep clone of `source`. Each member of the returned object will contain the provided | ||
* source-map information. | ||
* | ||
* @param source | ||
* @param infoFunc | ||
*/ | ||
exports.cloneDeepWithInfo = (source, infoFunc) => exports.cloneDeep(source, () => infoFunc); | ||
/** | ||
* Get a file position | ||
@@ -151,0 +172,0 @@ * |
52
index.ts
@@ -214,19 +214,45 @@ import { StringMap } from "@ts-common/string-map" | ||
export const cloneDeep = <T extends Data>(source: T): T => { | ||
const data: Data = source | ||
if (data === null || | ||
typeof data === "boolean" || | ||
typeof data === "number" || | ||
typeof data === "string" | ||
) { | ||
return source | ||
/** | ||
* Returns a deep clone of `source` and set a source-map for each member. | ||
* | ||
* @param source an original object | ||
* @param getInfoFunctOptional the function should return an object info of a provided member. | ||
* If the function is not provided the algorithm extract information from the provided member. | ||
*/ | ||
export const cloneDeep = <T extends Data>( | ||
source: T, | ||
getInfoFuncOptional?: (member: Data|undefined) => InfoFunc|undefined | ||
): T => { | ||
const get = getInfoFuncOptional === undefined ? getInfoFunc : getInfoFuncOptional | ||
const clone = (data: Data): Data => { | ||
if (data === null || | ||
typeof data === "boolean" || | ||
typeof data === "number" || | ||
typeof data === "string" | ||
) { | ||
return data | ||
} | ||
const result = Array.isArray(data) ? | ||
data.map(clone) : | ||
sm.map(data as sm.StringMap<Data>, clone) | ||
const infoFunc = get(data) | ||
if (infoFunc !== undefined) { | ||
setInfoFunc(result, infoFunc) | ||
} | ||
return result | ||
} | ||
const result = Array.isArray(data) ? | ||
data.map(cloneDeep) : | ||
sm.map(data as sm.StringMap<Data>, cloneDeep) | ||
copyInfo(data, result) | ||
return result as T | ||
return clone(source) as T | ||
} | ||
/** | ||
* Returns a deep clone of `source`. Each member of the returned object will contain the provided | ||
* source-map information. | ||
* | ||
* @param source | ||
* @param infoFunc | ||
*/ | ||
export const cloneDeepWithInfo = <T extends Data>(source: T, infoFunc: InfoFunc | undefined): T => | ||
cloneDeep(source, () => infoFunc) | ||
/** | ||
* Get a file position | ||
@@ -233,0 +259,0 @@ * |
{ | ||
"name": "@ts-common/source-map", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "Source Map", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,3 +0,6 @@ | ||
# source-map | ||
# Source Map | ||
[![Build Status](https://dev.azure.com/ts-common/ts-common/_apis/build/status/ts-common.source-map)](https://dev.azure.com/ts-common/ts-common/_build/latest?definitionId=14) | ||
Source Map | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
44790
689
7