-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
executable file
·42 lines (36 loc) · 1.28 KB
/
generate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#! /usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const iter = require('iter-tools')
const mkstr = x => JSON.stringify(String(x))
const codestr = x => mkstr(String.fromCharCode(x))
const union = iterable => Array.from(iterable).join(' | ')
const declare = (name, definition) => `export type ${name} = ${definition};`
const ascii = Array.from(iter.range(1 << 8))
const lowerCaseAlphabetCodes = iter.range({ start: 'a'.charCodeAt(), end: 'z'.charCodeAt() + 1 })
const upperCaseAlphabetCodes = iter.range({ start: 'A'.charCodeAt(), end: 'Z'.charCodeAt() + 1 })
const Digit = union(iter.range({ start: 0, end: 10 }))
const Byte = union(iter.range({ start: -128, end: 128 }))
const SignedByte = 'Byte'
const UnsignedByte = union(ascii)
const AsciiNumber = 'UnsignedByte'
const AsciiCharacter = union(iter.map(codestr, ascii))
const LowerCaseAlphabet = union(iter.map(codestr, lowerCaseAlphabetCodes))
const UpperCaseAlphabet = union(iter.map(codestr, upperCaseAlphabetCodes))
const code = Object.entries({
Digit,
Byte,
SignedByte,
UnsignedByte,
AsciiNumber,
AsciiCharacter,
LowerCaseAlphabet,
UpperCaseAlphabet
})
.map(([name, definition]) => declare(name, definition))
.join('\n')
fs.writeFileSync(
path.join(__dirname, 'index.d.ts'),
code + '\n'
)