Skip to content

Commit a6f9c74

Browse files
committed
add --omit feature for given type paths
1 parent 68541af commit a6f9c74

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

cli.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ async function main () {
1414
const discriminantPaths: string[] = []
1515
const literalPaths: string[] = []
1616
const recordPaths: string[] = []
17+
const omitPaths: string[] = []
1718
let count = false
1819

1920
const args = process.argv.slice(2)
2021
while (args.length) {
2122
const a = args.shift()
2223
if (a === '--discriminant') discriminantPaths.push(args.shift() ?? '')
2324
if (a === '--literal') literalPaths.push(args.shift() ?? '')
25+
if (a === '--omit') omitPaths.push(args.shift() ?? '')
2426
if (a === '--record') recordPaths.push(args.shift() ?? '')
2527
if (a === '--count') count = true
2628
}
@@ -31,7 +33,7 @@ async function main () {
3133
for await (const line of rl) {
3234
if (!line) continue // ignore empty lines
3335
const json = JSON.parse(line)
34-
const rtype = gettype(json, '', literalPaths, discriminantPaths, recordPaths)
36+
const rtype = gettype(json, '', literalPaths, discriminantPaths, recordPaths, omitPaths)
3537
ltype = fold(ltype, rtype)
3638
}
3739

lib.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,16 @@ export function gettype (
120120
path = '',
121121
literalPaths: string[] = [],
122122
discriminantPaths: string[] = [],
123-
recordPaths: string[] = []
123+
recordPaths: string[] = [],
124+
omitPaths: string[] = [],
124125
) {
125126
if (typeof json === 'object') {
126127
if (json === null) return { literal: { value: null }, count: 1 }
127128
if (Array.isArray(json)) {
128129
if (json.length === 0) return { array: { ...NEVER }, count: 1 }
129130
return {
130131
array: foldleft(
131-
json.map(x => gettype(x, `${path}[]`, literalPaths, discriminantPaths, recordPaths))
132+
json.map(x => gettype(x, `${path}[]`, literalPaths, discriminantPaths, recordPaths, omitPaths))
132133
),
133134
count: 1
134135
}
@@ -138,8 +139,10 @@ export function gettype (
138139
let keyType: Type = { ...NEVER }
139140
let valueType: Type = { ...NEVER }
140141
for (const key in json) {
141-
keyType = fold(keyType, gettype(key, `${path}|keys`, literalPaths, discriminantPaths, recordPaths))
142-
valueType = fold(valueType, gettype(json[key], `${path}[]`, literalPaths, discriminantPaths, recordPaths))
142+
const innerPath = `${path}.${key}`
143+
if (omitPaths.includes(innerPath)) continue
144+
keyType = fold(keyType, gettype(key, `${path}|keys`, literalPaths, discriminantPaths, recordPaths, omitPaths))
145+
valueType = fold(valueType, gettype(json[key], `${path}[]`, literalPaths, discriminantPaths, recordPaths, omitPaths))
143146
}
144147
return {
145148
record: {
@@ -153,8 +156,10 @@ export function gettype (
153156
const object = {} as Record<string, Type>
154157
let discriminant: Type['literal'] | undefined
155158
for (const key in json) {
156-
const ft = gettype(json[key], `${path}.${key}`, literalPaths, discriminantPaths, recordPaths)
159+
const innerPath = `${path}.${key}`
160+
if (omitPaths.includes(innerPath)) continue
157161

162+
const ft = gettype(json[key], innerPath, literalPaths, discriminantPaths, recordPaths, omitPaths)
158163
object[key] = ft
159164
if (ft.literal && discriminantPaths.includes(`${path}.${key}`)) {
160165
discriminant = ft.literal

0 commit comments

Comments
 (0)