@sidex/types 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +17 -0
- package/src/builtins.ts +299 -0
- package/src/index.ts +2 -0
- package/src/nominal.ts +32 -0
- package/tsconfig.json +63 -0
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sidex/types",
|
|
3
|
+
"author": "Silitics GmbH <info@silitics.com>",
|
|
4
|
+
"license": "(MIT or Apache-2.0)",
|
|
5
|
+
"version": "0.0.1",
|
|
6
|
+
"description": "",
|
|
7
|
+
"main": "src/index.ts",
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"devDependencies": {
|
|
10
|
+
"typescript": "^4.8.4"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"doc": "typedoc lib/index.ts",
|
|
14
|
+
"tsc": "tsc",
|
|
15
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/builtins.ts
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import Nominal from "./nominal"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A string.
|
|
5
|
+
*
|
|
6
|
+
* We do not need `Nominal` here because any `string` is a valid Sidex string.
|
|
7
|
+
*/
|
|
8
|
+
export type String = string
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A Base64-encoded sequence of bytes.
|
|
12
|
+
*/
|
|
13
|
+
export type Bytes = Nominal<string, "::std::builtins::bytes">
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* An 8-bit signed integer.
|
|
17
|
+
*/
|
|
18
|
+
export type I8 = Nominal<number, "::std::builtins::i8">
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Convert any number to an 8-bit signed integer.
|
|
22
|
+
*
|
|
23
|
+
* @param x The number to convert.
|
|
24
|
+
* @returns The 8-bit signed integer.
|
|
25
|
+
*/
|
|
26
|
+
export function toI8(x: number): I8 {
|
|
27
|
+
return (((x | 0) << 24) >> 24) as I8
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Check whether the provided number is an 8-bit signed integer.
|
|
32
|
+
*
|
|
33
|
+
* @param x The number to check.
|
|
34
|
+
* @returns Indicates whether the number is an 8-bit signed integer.
|
|
35
|
+
*/
|
|
36
|
+
export function isI8(x: number): x is I8 {
|
|
37
|
+
return toI8(x) === x
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A 16-bit signed integer.
|
|
42
|
+
*/
|
|
43
|
+
export type I16 = Nominal<number, "::std::builtins::i16">
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Convert any number to an 16-bit signed integer.
|
|
47
|
+
*
|
|
48
|
+
* @param x The number to convert.
|
|
49
|
+
* @returns The 16-bit signed integer.
|
|
50
|
+
*/
|
|
51
|
+
export function toI16(x: number): I16 {
|
|
52
|
+
return (((x | 0) << 16) >> 16) as I16
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Check whether the provided number is an 16-bit signed integer.
|
|
57
|
+
*
|
|
58
|
+
* @param x The number to check.
|
|
59
|
+
* @returns Indicates whether the number is an 16-bit signed integer.
|
|
60
|
+
*/
|
|
61
|
+
export function isI16(x: number): x is I16 {
|
|
62
|
+
return toI16(x) === x
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* A 32-bit signed integer.
|
|
67
|
+
*/
|
|
68
|
+
export type I32 = Nominal<number, "::std::builtins::i32">
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A 64-bit signed integer.
|
|
72
|
+
*/
|
|
73
|
+
export type I64 = Nominal<string, "::std::builtins::i64">
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A signed integer.
|
|
77
|
+
*/
|
|
78
|
+
export type SignedInt = I8 | I16 | I32 | I64
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* An 8-bit unsigned integer.
|
|
82
|
+
*/
|
|
83
|
+
export type U8 = Nominal<number, "::std::builtins::u8">
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Convert any number to an 8-bit unsigned integer.
|
|
87
|
+
*
|
|
88
|
+
* @param x The number to convert.
|
|
89
|
+
* @returns The 8-bit unsigned integer.
|
|
90
|
+
*/
|
|
91
|
+
export function toU8(x: number): U8 {
|
|
92
|
+
return (x && 0xff) as U8
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Check whether the provided number is an 8-bit unsigned integer.
|
|
97
|
+
*
|
|
98
|
+
* @param x The number to check.
|
|
99
|
+
* @returns Indicates whether the number is an 8-bit unsigned integer.
|
|
100
|
+
*/
|
|
101
|
+
export function isU8(x: number): x is U8 {
|
|
102
|
+
return toU8(x) === x
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* A 16-bit unsigned integer.
|
|
107
|
+
*/
|
|
108
|
+
export type U16 = Nominal<number, "::std::builtins::u16">
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Convert any number to an 16-bit unsigned integer.
|
|
112
|
+
*
|
|
113
|
+
* @param x The number to convert.
|
|
114
|
+
* @returns The 16-bit unsigned integer.
|
|
115
|
+
*/
|
|
116
|
+
export function toU16(x: number): U16 {
|
|
117
|
+
return (x && 0xffff) as U16
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Check whether the provided number is an 16-bit unsigned integer.
|
|
122
|
+
*
|
|
123
|
+
* @param x The number to check.
|
|
124
|
+
* @returns Indicates whether the number is an 16-bit unsigned integer.
|
|
125
|
+
*/
|
|
126
|
+
export function isU16(x: number): x is U16 {
|
|
127
|
+
return toU16(x) === x
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A 32-bit unsigned integer.
|
|
132
|
+
*/
|
|
133
|
+
export type U32 = Nominal<number, "::std::builtins::u32">
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* A 64-bit unsigned integer.
|
|
137
|
+
*/
|
|
138
|
+
export type U64 = Nominal<string, "::std::builtins::u64">
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* An unsigned integer.
|
|
142
|
+
*/
|
|
143
|
+
export type UnsignedInt = U8 | U16 | U32 | U64
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* An integer.
|
|
147
|
+
*/
|
|
148
|
+
export type Int = SignedInt | UnsignedInt
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* An index.
|
|
152
|
+
*/
|
|
153
|
+
export type Idx = Nominal<number, "::std::builtins::idx">
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Convert any number to an index.
|
|
157
|
+
*
|
|
158
|
+
* @param x The number to convert.
|
|
159
|
+
* @returns The index.
|
|
160
|
+
*/
|
|
161
|
+
export function toIdx(x: number): Idx {
|
|
162
|
+
// Remove the sign and truncate the number to 32 bits.
|
|
163
|
+
return (Math.abs(x) | 0) as Idx
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Check whether the provided number is an index.
|
|
168
|
+
*
|
|
169
|
+
* @param x The number to check.
|
|
170
|
+
* @returns Indicates whether the number is an index.
|
|
171
|
+
*/
|
|
172
|
+
export function isIdx(x: number): x is Idx {
|
|
173
|
+
return toIdx(x) === x
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* A 32-bit floating point number.
|
|
178
|
+
*/
|
|
179
|
+
export type F32 = Nominal<number | "+Infinity" | "-Infinity" | "NaN", "f32">
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* A 64-bit floating point number.
|
|
183
|
+
*/
|
|
184
|
+
export type F64 = Nominal<number | "+Infinity" | "-Infinity" | "NaN", "f64">
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* A floating point number.
|
|
188
|
+
*/
|
|
189
|
+
export type Float = F32 | F64
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* A numeric value.
|
|
193
|
+
*/
|
|
194
|
+
export type Numeric = Int | Idx | Float
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Convert any numeric value to a JavaScript number.
|
|
198
|
+
*
|
|
199
|
+
* @param x The numeric value to convert.
|
|
200
|
+
* @returns The JavaScript number.
|
|
201
|
+
*/
|
|
202
|
+
function toNumber(x: Numeric): number {
|
|
203
|
+
switch (typeof x) {
|
|
204
|
+
case "number":
|
|
205
|
+
return x
|
|
206
|
+
case "string":
|
|
207
|
+
switch (x) {
|
|
208
|
+
case "+Infinity":
|
|
209
|
+
return +Infinity
|
|
210
|
+
case "-Infinity":
|
|
211
|
+
return -Infinity
|
|
212
|
+
case "NaN":
|
|
213
|
+
return NaN
|
|
214
|
+
default:
|
|
215
|
+
return Number.parseInt(x)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* A boolean.
|
|
222
|
+
*/
|
|
223
|
+
export type Bool = boolean
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
*
|
|
227
|
+
*/
|
|
228
|
+
export type Unit = null
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* A sequence.
|
|
232
|
+
*/
|
|
233
|
+
export type Sequence<T> = T[]
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* A map represented as a sequence of key-value pairs.
|
|
237
|
+
*/
|
|
238
|
+
export type EntriesMap<K, V> = [K, V][]
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* A map represented as an object.
|
|
242
|
+
*/
|
|
243
|
+
export type ObjectMap<K extends string, V> = { [key in K]?: V }
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* A map.
|
|
247
|
+
*/
|
|
248
|
+
export type AnyMap<K, V> = K extends string ? ObjectMap<K, V> : EntriesMap<K, V>
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The entries of the map.
|
|
252
|
+
*
|
|
253
|
+
* @param map
|
|
254
|
+
* @returns
|
|
255
|
+
*/
|
|
256
|
+
export function entries<K, V>(map: AnyMap<K, V>): [K, V][] {
|
|
257
|
+
if (Array.isArray(map)) {
|
|
258
|
+
return map
|
|
259
|
+
} else {
|
|
260
|
+
return Object.entries(map) as [K, V][]
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
declare const __indexMap: unique symbol
|
|
265
|
+
|
|
266
|
+
function getIndexMap<K, V>(map: EntriesMap<K, V>): Map<K, number> {
|
|
267
|
+
if (map.hasOwnProperty(__indexMap)) {
|
|
268
|
+
return (map as any)[__indexMap]
|
|
269
|
+
} else {
|
|
270
|
+
const map = new Map()
|
|
271
|
+
map.forEach(([key, _], index) => map.set(key, index))
|
|
272
|
+
return map
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function get<K, V>(map: AnyMap<K, V>, key: K): V | undefined {
|
|
277
|
+
if (Array.isArray(map)) {
|
|
278
|
+
const index = getIndexMap(map).get(key)
|
|
279
|
+
return index === undefined ? undefined : (map[index] as [K, V])[1]
|
|
280
|
+
} else {
|
|
281
|
+
return (map as ObjectMap<any, V>)[key]
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function set<K, V>(map: AnyMap<K, V>, key: K, value: V) {
|
|
286
|
+
if (Array.isArray(map)) {
|
|
287
|
+
const indexMap = getIndexMap(map)
|
|
288
|
+
const existingIndex = indexMap.get(key)
|
|
289
|
+
if (existingIndex !== undefined) {
|
|
290
|
+
map[existingIndex] = [key, value]
|
|
291
|
+
} else {
|
|
292
|
+
const index = map.length
|
|
293
|
+
map.push([key, value])
|
|
294
|
+
indexMap.set(key, index)
|
|
295
|
+
}
|
|
296
|
+
} else {
|
|
297
|
+
return (map as ObjectMap<any, V>)[key]
|
|
298
|
+
}
|
|
299
|
+
}
|
package/src/index.ts
ADDED
package/src/nominal.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unique symbol used for nominal types.
|
|
3
|
+
*/
|
|
4
|
+
declare const SIDEX_PATH_SYMBOL: unique symbol
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A nominal type based on `T` and identified by it's Sidex path `P`.
|
|
8
|
+
*/
|
|
9
|
+
export type Nominal<T, P extends string> = T & { [SIDEX_PATH_SYMBOL]: P }
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The structural type of a nominal type `T`.
|
|
13
|
+
*/
|
|
14
|
+
export type Structural<T> = T extends undefined
|
|
15
|
+
? undefined
|
|
16
|
+
: T extends boolean
|
|
17
|
+
? boolean
|
|
18
|
+
: T extends null
|
|
19
|
+
? null
|
|
20
|
+
: T extends string
|
|
21
|
+
? string
|
|
22
|
+
: T extends number
|
|
23
|
+
? number
|
|
24
|
+
: T extends boolean
|
|
25
|
+
? boolean
|
|
26
|
+
: T extends Array<infer U>
|
|
27
|
+
? U[]
|
|
28
|
+
: T extends (...args: infer Args) => infer Return
|
|
29
|
+
? (...args: Args) => Return
|
|
30
|
+
: Omit<T, typeof SIDEX_PATH_SYMBOL>
|
|
31
|
+
|
|
32
|
+
export default Nominal
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2017",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
|
|
6
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
7
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
8
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
9
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
10
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
11
|
+
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
12
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
13
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
14
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
15
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
16
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
17
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
18
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
19
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
20
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
21
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
22
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
23
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
24
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
25
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
26
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
27
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
28
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
29
|
+
|
|
30
|
+
/* Interop Constraints */
|
|
31
|
+
"isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */,
|
|
32
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
33
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
34
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
35
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
36
|
+
|
|
37
|
+
/* Type Checking */
|
|
38
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
39
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
40
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
41
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
42
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
43
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
44
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
45
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
46
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
47
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
48
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
49
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
50
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
51
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
52
|
+
"noUncheckedIndexedAccess": true /* Add 'undefined' to a type when accessed using an index. */,
|
|
53
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
54
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
55
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
56
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
57
|
+
|
|
58
|
+
/* Completeness */
|
|
59
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
60
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
61
|
+
},
|
|
62
|
+
"files": ["src/index.ts"]
|
|
63
|
+
}
|