1
- import { deburr , isPlainObject , mapValues , trim , upperFirst } from 'lodash'
1
+ import { deburr , isPlainObject , trim , upperFirst } from 'lodash'
2
2
import { basename , dirname , extname , join , normalize , sep } from 'path'
3
3
import { JSONSchema , LinkedJSONSchema } from './types/JSONSchema'
4
4
5
5
// TODO: pull out into a separate package
6
- // eslint-disable-next-line
7
6
export function Try < T > ( fn : ( ) => T , err : ( e : Error ) => any ) : T {
8
7
try {
9
8
return fn ( )
@@ -12,25 +11,6 @@ export function Try<T>(fn: () => T, err: (e: Error) => any): T {
12
11
}
13
12
}
14
13
15
- export function mapDeep ( object : object , fn : ( value : object , key ?: string ) => object , key ?: string ) : object {
16
- return fn (
17
- mapValues ( object , ( _ : unknown , key ) => {
18
- if ( isPlainObject ( _ ) ) {
19
- return mapDeep ( _ as object , fn , key )
20
- } else if ( Array . isArray ( _ ) ) {
21
- return _ . map ( item => {
22
- if ( isPlainObject ( item ) ) {
23
- return mapDeep ( item as object , fn , key )
24
- }
25
- return item
26
- } )
27
- }
28
- return _
29
- } ) ,
30
- key
31
- )
32
- }
33
-
34
14
// keys that shouldn't be traversed by the catchall step
35
15
const BLACKLISTED_KEYS = new Set ( [
36
16
'id' ,
@@ -70,34 +50,39 @@ const BLACKLISTED_KEYS = new Set([
70
50
71
51
function traverseObjectKeys (
72
52
obj : Record < string , LinkedJSONSchema > ,
73
- callback : ( schema : LinkedJSONSchema ) => void ,
53
+ callback : ( schema : LinkedJSONSchema , key : string | null ) => void ,
74
54
processed : Set < LinkedJSONSchema >
75
55
) {
76
56
Object . keys ( obj ) . forEach ( k => {
77
57
if ( obj [ k ] && typeof obj [ k ] === 'object' && ! Array . isArray ( obj [ k ] ) ) {
78
- traverse ( obj [ k ] , callback , processed )
58
+ traverse ( obj [ k ] , callback , processed , k )
79
59
}
80
60
} )
81
61
}
62
+
82
63
function traverseArray (
83
64
arr : LinkedJSONSchema [ ] ,
84
- callback : ( schema : LinkedJSONSchema ) => void ,
65
+ callback : ( schema : LinkedJSONSchema , key : string | null ) => void ,
85
66
processed : Set < LinkedJSONSchema >
86
67
) {
87
- arr . forEach ( i => traverse ( i , callback , processed ) )
68
+ arr . forEach ( ( s , k ) => traverse ( s , callback , processed , k . toString ( ) ) )
88
69
}
70
+
89
71
export function traverse (
90
72
schema : LinkedJSONSchema ,
91
- callback : ( schema : LinkedJSONSchema ) => void ,
92
- processed = new Set < LinkedJSONSchema > ( )
73
+ callback : ( schema : LinkedJSONSchema , key : string | null ) => void ,
74
+ processed = new Set < LinkedJSONSchema > ( ) ,
75
+ key ?: string
93
76
) : void {
94
77
// Handle recursive schemas
95
78
if ( processed . has ( schema ) ) {
96
79
return
97
80
}
98
81
82
+ // console.log('key', key + '\n')
83
+
99
84
processed . add ( schema )
100
- callback ( schema )
85
+ callback ( schema , key ?? null )
101
86
102
87
if ( schema . anyOf ) {
103
88
traverseArray ( schema . anyOf , callback , processed )
0 commit comments