diff --git a/src/index.ts b/src/index.ts index f79127a..18e85e2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { JSONSchema, JSONSchemaObject, PatternProperties } from "@json-schema-tools/meta-schema"; +import { jsonPathStringify, isCycle, last } from "./utils"; /** * Signature of the mutation method passed to traverse. @@ -49,28 +50,6 @@ export const defaultOptions: TraverseOptions = { bfs: false, }; -const jsonPathStringify = (s: string[]) => { - return s.map((i) => { - if (i === "") { - return '$'; - } else { - return `.${i}`; - } - }).join(""); -}; - -const isCycle = (s: JSONSchema, recursiveStack: JSONSchema[]): JSONSchema | false => { - const foundInRecursiveStack = recursiveStack.find((recSchema) => recSchema === s); - if (foundInRecursiveStack) { - return foundInRecursiveStack; - } - return false; -}; - -const last = (i: JSONSchema[], skip = 1): JSONSchema => { - return i[i.length - skip]; -}; - /** * Traverse all subschema of a schema, calling the mutator function with each. * The mutator is called on leaf nodes first. @@ -273,11 +252,11 @@ export default function traverse( mutableStack.pop(); return mutableSchema; } else { - const isCycle = cycleSet.indexOf(schema) !== -1 + const isCycleNode = cycleSet.indexOf(schema) !== -1 mutableStack.pop(); return mutation( mutableSchema, - isCycle, + isCycleNode, jsonPathStringify(pathStack), last(mutableStack) ); diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..eb6e29e --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,28 @@ +import { JSONSchema } from "@json-schema-tools/meta-schema"; + +export const jsonPathStringify = (s: string[]): string => { + return s + .map((i) => { + if (i === "") { + return "$"; + } else { + return `.${i}`; + } + }) + .join(""); +}; + +export const isCycle = ( + s: JSONSchema, + recursiveStack: JSONSchema[], +): JSONSchema | false => { + const foundInRecursiveStack = recursiveStack.find((recSchema) => recSchema === s); + if (foundInRecursiveStack) { + return foundInRecursiveStack; + } + return false; +}; + +export const last = (i: JSONSchema[], skip = 1): JSONSchema => { + return i[i.length - skip]; +};