@json-schema-tools/traverse
Advanced tools
Comparing version 1.10.2 to 1.10.3
@@ -8,3 +8,3 @@ import { JSONSchema } from "@json-schema-tools/meta-schema"; | ||
* @param path json-path string in dot-notation as per [draft-goessner-dispatch-jsonpath-00](https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html#name-overview-of-jsonpath-expres) | ||
* @param parent if the schema is the root, this will be the same as `schema`. Otherwise, it will be a reference to JSONSchema that is the parent. | ||
* @param parent A reference to JSONSchema that is the parent of the `schema` param. If the `schema` is the root schema, `parent` will be `undefined`. when schema is a cycle, parent is the parent of the referenced cycle (once again, if the cycled schema is the root, the parent will be undefined). | ||
*/ | ||
@@ -48,2 +48,2 @@ export declare type MutationFunction = (schema: JSONSchema, isCycle: boolean, path: string, parent: JSONSchema) => JSONSchema; | ||
*/ | ||
export default function traverse(schema: JSONSchema, mutation: MutationFunction, traverseOptions?: TraverseOptions, depth?: number, recursiveStack?: JSONSchema[], pathStack?: string[], prePostMap?: Array<[JSONSchema, JSONSchema]>, cycleSet?: JSONSchema[]): JSONSchema; | ||
export default function traverse(schema: JSONSchema, mutation: MutationFunction, traverseOptions?: TraverseOptions, depth?: number, recursiveStack?: JSONSchema[], mutableStack?: JSONSchema[], pathStack?: string[], prePostMap?: Array<[JSONSchema, JSONSchema]>, cycleSet?: JSONSchema[]): JSONSchema; |
@@ -46,5 +46,4 @@ "use strict"; | ||
}; | ||
var last = function (i, skipTwo) { | ||
if (skipTwo === void 0) { skipTwo = false; } | ||
var skip = skipTwo ? -2 : -1; | ||
var last = function (i, skip) { | ||
if (skip === void 0) { skip = 1; } | ||
return i[i.length - skip]; | ||
@@ -63,6 +62,7 @@ }; | ||
*/ | ||
function traverse(schema, mutation, traverseOptions, depth, recursiveStack, pathStack, prePostMap, cycleSet) { | ||
function traverse(schema, mutation, traverseOptions, depth, recursiveStack, mutableStack, pathStack, prePostMap, cycleSet) { | ||
if (traverseOptions === void 0) { traverseOptions = exports.defaultOptions; } | ||
if (depth === void 0) { depth = 0; } | ||
if (recursiveStack === void 0) { recursiveStack = []; } | ||
if (mutableStack === void 0) { mutableStack = []; } | ||
if (pathStack === void 0) { pathStack = []; } | ||
@@ -84,3 +84,3 @@ if (prePostMap === void 0) { prePostMap = []; } | ||
else { | ||
return mutation(schema, false, jsonPathStringify(pathStack), last(recursiveStack) || schema); | ||
return mutation(schema, false, jsonPathStringify(pathStack), last(mutableStack)); | ||
} | ||
@@ -92,5 +92,6 @@ } | ||
} | ||
mutableStack.push(mutableSchema); | ||
if (opts.bfs === true) { | ||
if (opts.skipFirstMutation === false || depth !== 0) { | ||
mutableSchema = mutation(mutableSchema, false, jsonPathStringify(pathStack), last(recursiveStack) || schema); | ||
mutableSchema = mutation(mutableSchema, false, jsonPathStringify(pathStack), last(mutableStack, 2)); | ||
} | ||
@@ -107,3 +108,3 @@ } | ||
if (opts.skipFirstMutation === true && foundCycle === recursiveStack[0]) { | ||
return mutation(s, true, jsonPathStringify(path), last(recursiveStack, true) || schema); | ||
return mutation(s, true, jsonPathStringify(path), last(mutableStack)); | ||
} | ||
@@ -117,3 +118,3 @@ var _a = prePostMap.find(function (_a) { | ||
// else | ||
return traverse(s, mutation, traverseOptions, depth + 1, recursiveStack, path, prePostMap, cycleSet); | ||
return traverse(s, mutation, traverseOptions, depth + 1, recursiveStack, mutableStack, path, prePostMap, cycleSet); | ||
}; | ||
@@ -151,3 +152,3 @@ if (schema.anyOf) { | ||
if (opts.skipFirstMutation === true && foundCycle_1 === recursiveStack[0]) { | ||
mutableSchema.items = mutation(schema.items, true, jsonPathStringify(pathStack), last(recursiveStack, true) || schema); | ||
mutableSchema.items = mutation(schema.items, true, jsonPathStringify(pathStack), last(mutableStack)); | ||
} | ||
@@ -163,3 +164,3 @@ else { | ||
else { | ||
mutableSchema.items = traverse(schema.items, mutation, traverseOptions, depth + 1, recursiveStack, __spreadArray(__spreadArray([], pathStack, true), ["items"], false), prePostMap, cycleSet); | ||
mutableSchema.items = traverse(schema.items, mutation, traverseOptions, depth + 1, recursiveStack, mutableStack, __spreadArray(__spreadArray([], pathStack, true), ["items"], false), prePostMap, cycleSet); | ||
} | ||
@@ -195,2 +196,3 @@ } | ||
if (opts.bfs === true) { | ||
mutableStack.pop(); | ||
return mutableSchema; | ||
@@ -200,5 +202,6 @@ } | ||
var isCycle_1 = cycleSet.indexOf(schema) !== -1; | ||
return mutation(mutableSchema, isCycle_1, jsonPathStringify(pathStack), last(recursiveStack, true) || schema); | ||
mutableStack.pop(); | ||
return mutation(mutableSchema, isCycle_1, jsonPathStringify(pathStack), last(mutableStack)); | ||
} | ||
} | ||
exports.default = traverse; |
{ | ||
"name": "@json-schema-tools/traverse", | ||
"version": "1.10.2", | ||
"version": "1.10.3", | ||
"description": "This package exports a method that will traverse a JSON-Schema, calling a mutation function for each sub schema found. It is useful for building tools to work with JSON Schemas.", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
26408
245