@ts-common/source-map
Advanced tools
Comparing version 0.2.8 to 0.2.9
@@ -71,3 +71,3 @@ import { StringMap } from "@ts-common/string-map"; | ||
*/ | ||
export declare const getChildFilePosition: (data: object, index: string | number) => FilePosition | undefined; | ||
export declare const getChildFilePosition: (data: object | undefined, index: string | number | undefined) => FilePosition | undefined; | ||
/** | ||
@@ -80,1 +80,3 @@ * Get a file position of a descendant by path. | ||
export declare const getDescendantFilePosition: (object: object, path: Iterable<string | number> | undefined) => FilePosition | undefined; | ||
export declare const getAllDirectives: (object: object, path: Iterable<string | number> | undefined) => StringMap<unknown>; | ||
//# sourceMappingURL=index.d.ts.map |
74
index.js
@@ -123,13 +123,10 @@ "use strict"; | ||
exports.getRootObjectInfo = (info) => !info.isChild ? info : exports.getRootObjectInfo(info.parent[exports.objectInfoSymbol]()); | ||
const getReversedPath = (info) => { | ||
function* iterator() { | ||
let i = info; | ||
while (i.isChild) { | ||
yield i.property; | ||
i = i.parent[exports.objectInfoSymbol](); | ||
} | ||
const getReversedInfoIterator = function* (info) { | ||
while (info.isChild) { | ||
yield info; | ||
info = info.parent[exports.objectInfoSymbol](); | ||
} | ||
return _.iterable(iterator); | ||
yield info; | ||
}; | ||
exports.getPath = (info) => _.reverse(getReversedPath(info)); | ||
exports.getPath = (info) => _.reverse(_.filterMap(getReversedInfoIterator(info), i => i.isChild ? i.property : undefined)); | ||
exports.cloneDeep = (source) => { | ||
@@ -165,2 +162,8 @@ const data = source; | ||
exports.getChildFilePosition = (data, index) => { | ||
if (data === undefined) { | ||
return undefined; | ||
} | ||
if (index === undefined) { | ||
return exports.getFilePosition(data); | ||
} | ||
const child = data[index]; | ||
@@ -180,3 +183,3 @@ if (child === undefined) { | ||
/** | ||
* Get a file position of a descendant by path. | ||
* Returns a data reference corresponding to the given path. | ||
* | ||
@@ -186,21 +189,54 @@ * @param object | ||
*/ | ||
exports.getDescendantFilePosition = (object, path) => { | ||
const getDataRef = (object, path) => { | ||
if (path === undefined) { | ||
return exports.getFilePosition(object); | ||
return { parent: object, index: undefined }; | ||
} | ||
let index = undefined; | ||
// preserve the last index. | ||
for (const i of path) { | ||
if (index !== undefined) { | ||
object = object[index]; | ||
const newObject = object[index]; | ||
if (newObject === null || typeof newObject !== "object") { | ||
return { parent: undefined, index: undefined }; | ||
} | ||
object = newObject; | ||
} | ||
if (object === null || typeof object !== "object") { | ||
return undefined; | ||
} | ||
index = i; | ||
} | ||
if (index === undefined) { | ||
return exports.getFilePosition(object); | ||
return { parent: object, index }; | ||
}; | ||
/** | ||
* Get a file position of a descendant by path. | ||
* | ||
* @param object | ||
* @param path | ||
*/ | ||
exports.getDescendantFilePosition = (object, path) => { | ||
const dataRef = getDataRef(object, path); | ||
return exports.getChildFilePosition(dataRef.parent, dataRef.index); | ||
}; | ||
const getReversedFilePositions = function* (dataRef) { | ||
const parent = dataRef.parent; | ||
if (parent === undefined) { | ||
return; | ||
} | ||
return exports.getChildFilePosition(object, index); | ||
if (dataRef.index !== undefined) { | ||
const filePosition = exports.getChildFilePosition(parent, dataRef.index); | ||
if (filePosition !== undefined) { | ||
yield filePosition; | ||
} | ||
} | ||
let i = exports.getInfo(parent); | ||
if (i === undefined) { | ||
return; | ||
} | ||
yield* _.map(getReversedInfoIterator(i), v => v.position); | ||
}; | ||
exports.getAllDirectives = (object, path) => { | ||
const dataRef = getDataRef(object, path); | ||
const reversedFilePositions = getReversedFilePositions(dataRef); | ||
const reversedDirectives = _.filterMap(reversedFilePositions, v => v.directives); | ||
const directives = _.reverse(reversedDirectives); | ||
return sm.merge(...directives); | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@ts-common/source-map", | ||
"version": "0.2.8", | ||
"version": "0.2.9", | ||
"description": "Source Map", | ||
@@ -18,3 +18,7 @@ "main": "index.js", | ||
"files": [ | ||
"index.d.ts" | ||
"index.js", | ||
"index.js.map", | ||
"index.d.ts", | ||
"index.d.ts.map", | ||
"index.ts" | ||
], | ||
@@ -36,3 +40,3 @@ "repository": { | ||
"devDependencies": { | ||
"@types/chai": "^4.1.5", | ||
"@types/chai": "^4.1.6", | ||
"@types/mocha": "^5.2.5", | ||
@@ -49,4 +53,4 @@ "chai": "^4.2.0", | ||
"@ts-common/property-set": "0.0.8", | ||
"@ts-common/string-map": "0.0.26" | ||
"@ts-common/string-map": "0.1.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41987
8
620
+ Added@ts-common/string-map@0.1.0(transitive)
Updated@ts-common/string-map@0.1.0