Skip to content

Commit eb6fa71

Browse files
committed
add --count flag for optional counting
1 parent 9a06985 commit eb6fa71

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-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 = []
1515
const literalPaths = []
1616
const recordPaths = []
17+
let count = false
1718

1819
const args = process.argv.slice(2)
1920
while (args.length) {
2021
const a = args.shift()
2122
if (a === '--discriminant') discriminantPaths.push(args.shift() ?? '')
2223
if (a === '--literal') literalPaths.push(args.shift() ?? '')
2324
if (a === '--record') recordPaths.push(args.shift() ?? '')
25+
if (a === '--count') count = true
2426
}
2527

2628
// any discriminants are literals too
@@ -34,7 +36,7 @@ async function main () {
3436
}
3537

3638
if (ltype.never) return process.exit(1)
37-
console.log(print(ltype))
39+
console.log(print(ltype, '', count))
3840
}
3941

4042
main()

lib.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,20 @@ export function print (type: Type, indent = '', count = false): string {
167167
if (type.literal) return `${JSON.stringify(type.literal.value)}${comment(type, count)}`
168168
if (type.scalar) return `${type.scalar}${comment(type, count)}`
169169
if (type.array) {
170-
if (type.array.sum) return `(${print(type.array, indent)})[]`
171-
return `${print(type.array, indent)}[]`
170+
if (type.array.sum) return `(${print(type.array, indent, count)})[]`
171+
return `${print(type.array, indent, count)}[]`
172172
}
173173

174174
if (type.sum) {
175175
return type.sum
176-
.map(x => print(x, indent))
176+
.map(x => print(x, indent, count))
177177
.sort()
178178
.join(' | ')
179179
}
180180

181181
if (type.record) {
182182
let output = `{`
183-
output += `\n${indent + ' '}[key: ${print(type.record.key)}]: ${print(type.record.value, indent + ' ')}`
183+
output += `\n${indent + ' '}[key: ${print(type.record.key, '', count)}]: ${print(type.record.value, indent + ' ', count)}`
184184
return output + `\n${indent}}`
185185
}
186186

@@ -189,7 +189,7 @@ export function print (type: Type, indent = '', count = false): string {
189189
for (const key of Object.keys(type.object).sort()) {
190190
const fkey = (/[^A-Za-z0-9_]/.test(key) || (/^[0-9]/.test(key) && /[A-Za-z_]/.test(key)) || key === "") ? `"${key}"` : key
191191
const ftype = type.object[key]
192-
output += `\n${indent + ' '}${fkey}${(key in (type.maybe ?? {})) ? '?' : ''}: ${print(ftype, indent + ' ')}`
192+
output += `\n${indent + ' '}${fkey}${(key in (type.maybe ?? {})) ? '?' : ''}: ${print(ftype, indent + ' ', count)}`
193193
}
194194
return output + `\n${indent}}`
195195
}

0 commit comments

Comments
 (0)