Skip to content

Commit

Permalink
Migrate to json-op library
Browse files Browse the repository at this point in the history
  • Loading branch information
redneckz committed Jan 9, 2024
1 parent 261497f commit 20a471d
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 120 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@redneckz/json-ref",
"version": "0.0.7",
"version": "0.0.8",
"license": "MIT",
"author": {
"name": "redneckz",
Expand Down Expand Up @@ -45,6 +45,9 @@
"test": "jest",
"size": "echo size-limit"
},
"dependencies": {
"@redneckz/json-op": "^0.0.5"
},
"devDependencies": {
"@size-limit/preset-small-lib": "^8.2.4",
"@types/jest": "^29.5.0",
Expand All @@ -64,4 +67,4 @@
"path": "lib/*.js"
}
]
}
}
13 changes: 0 additions & 13 deletions src/JSONNode.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/JSONRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { JSONPath, fp, isJSONRecord, type JSONNode, type JSONRecord } from '@redneckz/json-op';

export type JSONRef = { $ref: string } & JSONRecord;

export const isJSONRef = (_: JSONNode | undefined): _ is JSONRef =>
Boolean(isJSONRecord(_) && typeof _.$ref === 'string');

export const isJSONRefPath = JSONPath.endsWith(['$ref']);
2 changes: 1 addition & 1 deletion src/URIResolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { JSONNode } from './JSONNode';
import { type JSONNode } from '@redneckz/json-op';

export type URIResolver<R extends Promise<JSONNode> | JSONNode = Promise<JSONNode>> = (uri: string) => R;
10 changes: 3 additions & 7 deletions src/collectRef.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { type JSONNode } from './JSONNode';
import { visitRef } from './visitRef';
import { type JSONNode } from '@redneckz/json-op';
import { refEntries } from './refEntries';

export const collectRef = (json: JSONNode): string[] => {
const refs: string[] = [];
visitRef(json, _ => refs.push(_));
return refs;
};
export const collectRef = (json: JSONNode): string[] => refEntries(json).map(([, _]) => _);
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
export { resolveRef } from './resolveRef';
export { resolveRef as resolveRefSync } from './resolveRef.sync';
export { visitRef, type RefVisitor } from './visitRef';
export { collectRef } from './collectRef';

export { resolveJPointer } from './resolveJPointer';

export type { URIResolver } from './URIResolver';

export * from './JSONNode';
export * from './JSONRef';
29 changes: 0 additions & 29 deletions src/mapJSONNode.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/mergeRecords.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/refEntries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { type JSONPath, fp, leafs, type JSONNode } from '@redneckz/json-op';
import { isJSONRefPath } from './JSONRef';

export type JSONRefEntry = [path: JSONPath.JSONPath, ref: string];

export const refEntries = (json: JSONNode): JSONRefEntry[] =>
leafs(json).filter(fp.t0(isJSONRefPath)) as JSONRefEntry[];
2 changes: 1 addition & 1 deletion src/resolveJPointer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONNode, isJSONArray, isJSONRecord } from './JSONNode';
import { isJSONArray, isJSONRecord, type JSONNode } from '@redneckz/json-op';
import { parseJPointer } from './parseJPointer';

export const resolveJPointer = (json: JSONNode, uri: string): JSONNode =>
Expand Down
21 changes: 10 additions & 11 deletions src/resolveRef.sync.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { type JSONNode, type JSONRecord } from './JSONNode';
import { JSONPath, fp, map, type JSONNode } from '@redneckz/json-op';
import { isJSONRefPath } from './JSONRef';
import type { URIResolver } from './URIResolver';
import { mapJSONNode } from './mapJSONNode';
import { mergeRecords } from './mergeRecords';
import { resolveJPointer } from './resolveJPointer';

export const resolveRef = (json: JSONNode, resolver: URIResolver<JSONNode>): JSONNode =>
mapJSONNode(json, {
ref: ({ $ref, ...rest }) => resolveRef($ref ? resolveJPointer(resolver($ref), $ref) : rest, resolver),
record: record =>
mergeRecords(
Object.entries(record).map(([key, value]) => ({ [key]: resolveRef(value, resolver) } as JSONRecord))
),
array: list => list.map(_ => resolveRef(_, resolver))
});
map(([path, node]) =>
isJSONRefPath(path)
? [
fp.init(path) as JSONPath.JSONPath,
resolveRef(resolveJPointer(resolver(node as string), node as string), resolver)
]
: [path, node]
)(json);
25 changes: 10 additions & 15 deletions src/resolveRef.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { type JSONNode, type JSONRecord } from './JSONNode';
import { JSONPath, async, fp, type JSONNode } from '@redneckz/json-op';
import { isJSONRefPath } from './JSONRef';
import type { URIResolver } from './URIResolver';
import { mapJSONNode } from './mapJSONNode';
import { mergeRecords } from './mergeRecords';
import { resolveJPointer } from './resolveJPointer';

export const resolveRef = async (json: JSONNode, resolver: URIResolver): Promise<JSONNode> =>
mapJSONNode(json, {
ref: async ({ $ref, ...rest }) => resolveRef($ref ? resolveJPointer(await resolver($ref), $ref) : rest, resolver),
record: async record =>
mergeRecords(
await Promise.all(
Object.entries(record).map(
async ([key, value]) => ({ [key]: await resolveRef(value, resolver) } as JSONRecord)
)
)
),
array: async list => Promise.all(list.map(_ => resolveRef(_, resolver)))
});
async.map(async ([path, node]) =>
isJSONRefPath(path)
? [
fp.init(path) as JSONPath.JSONPath,
await resolveRef(resolveJPointer(await resolver(node as string), node as string), resolver)
]
: [path, node]
)(json);
20 changes: 0 additions & 20 deletions src/visitRef.spec.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/visitRef.ts

This file was deleted.

0 comments on commit 20a471d

Please sign in to comment.