Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
);
Expand Down
28 changes: 28 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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];
};
Loading