-
Notifications
You must be signed in to change notification settings - Fork 5
/
HUnit.ts
471 lines (408 loc) · 10.1 KB
/
HUnit.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* Copyright (c) 2020, J2 Innovations. All Rights Reserved
*/
import { UnitDatabase } from './UnitDatabase'
import { UnitDimensions, UnitDimensionsData } from './UnitDimensions'
/**
* The module cached unit database.
*/
let db: UnitDatabase | undefined
/**
* @returns The unit database.
*/
function getDb(): UnitDatabase {
// With two-way dependencies, it's always far safer to lazily create resources in TS/JS.
return db ?? (db = new UnitDatabase())
}
/**
* A unit's raw data.
*/
export interface UnitData {
quantity?: string
ids: string[]
dimensions?: Partial<UnitDimensionsData>
scale: number
offset: number
}
/**
* Returns true if the unit is for bytes.
*
* @param unit The unit test.
* @returns True if the unit is for bytes.
*/
function isByteUnit(unit: HUnit): boolean {
return (
unit.name === 'byte' ||
unit.name === 'kilobyte' ||
unit.name === 'megabyte' ||
unit.name === 'gigabyte' ||
unit.name === 'terabyte' ||
unit.name === 'petabyte'
)
}
/**
* A type guard for testing whether a value is an `HUnit`.
*
* @param value The value to test.
* @returns True if the value is an `HUnit` instance.
*/
export function valueIsHUnit(value: unknown): value is HUnit {
return !!(value as HUnit)?._isHUnit
}
/**
* A haystack unit.
*
* https://project-haystack.org/doc/Units
*/
export class HUnit {
readonly #quantity?: string
readonly #ids: string[]
readonly #dimensions?: UnitDimensions
readonly #scale: number
readonly #offset: number
/**
* Used for a type guard check.
*/
public readonly _isHUnit = true
public constructor({ quantity, ids, dimensions, scale, offset }: UnitData) {
this.#quantity = quantity
this.#ids = ids
this.#dimensions = dimensions && new UnitDimensions(dimensions)
this.#scale = scale
this.#offset = offset
}
/**
* Defines a unit in the database.
*
* @param data The unit instance or data.
* @return The unit instance.
* @throws An error if the unit is already registered.
*/
public static define(data: UnitData | HUnit): HUnit {
const unit = valueIsHUnit(data) ? data : new HUnit(data)
getDb().define(unit)
return unit
}
/**
* Return a unit from the database via its id or undefined if it can't be found.
*
* @param id The id of the unit to look up.
* @returns The unit or undefined.
*/
public static get(id: string): HUnit | undefined {
return getDb().get(id)
}
/**
* Clear the underlying unit database.
*/
public static clearDatabase(): void {
// Completely overwrite the database so we also
// wipe out any memoization.
db = undefined
}
/**
* Parse the database and return all of the units.
*
* @param text The text from `units.txt` to parse.
* @returns All parsed units.
*/
public static parseDatabase(text: string): UnitData[] {
const units: UnitData[] = []
let lastQuantity = ''
for (const line of text.split('\n')) {
const quantity = this.parseQuantity(line)
if (quantity) {
lastQuantity = quantity
} else {
const unit = this.parseUnit(line)
if (unit) {
// Record the last quantity if we have one.
if (lastQuantity) {
unit.quantity = lastQuantity
}
units.push(unit)
}
}
}
return units
}
/**
* Parse some text to see if contains some quantity information.
* Return the quantity or undefined if it can't be found.
*
* @param text The text to parse.
* @returns The quantity or undefined if it can't be found.
*/
public static parseQuantity(text: string): string {
const res = /^ *-- *([^(]+) */.exec(text)
return ((res && res[1]) ?? '').trim()
}
/**
* Parse a line from `units.txt` as follows...
*
* unit := <ids> [";" <dim> [";" <scale> [";" <offset>]]]
* names := <ids> ("," <id>)*
* id := <idChar>*
* idChar := 'a'-'z' | 'A'-'Z' | '_' | '%' | '/' | any char > 128
* dim := <ratio> ["*" <ratio>]* // no whitespace allowed
* ratio := <base> <exp>
* base := "kg" | "m" | "sec" | "K" | "A" | "mol" | "cd"
* exp := <int>
* scale := <float>
* offset := <float>
*
* @param text The text to parse.
* @return The unit data or undefined if no unit data is found.
* @throws An error if unit data is found but the format is invalid.
*/
public static parseUnit(text: string): UnitData | undefined {
const [, ids, dimensions, scale, offset] =
/^ *([a-zA-Z][^;]*) *;? *([^;]+)? *;? *([^;]+)? *;? *([^;]+)?/.exec(
text
) ?? []
if (!ids) {
return undefined
}
const data: UnitData = {
ids: this.parseIds(ids),
scale: 1,
offset: 0,
}
if (dimensions) {
data.dimensions = this.parseDimensions(dimensions)
}
if (scale) {
data.scale = this.parseNumber(scale)
}
if (offset) {
data.offset = this.parseNumber(offset)
}
return data
}
private static parseIds(ids: string): string[] {
return ids.split(',').map((id) => id.trim())
}
private static parseDimensions(
dimensions: string
): UnitDimensions | undefined {
const dimension: Partial<UnitDimensionsData> = {}
dimensions.split('*').forEach((dim): void => {
const [, ratio, val] =
/^ *(kg|sec|mol|m|K|A|cd) *([-+]?\d+)/.exec(dim) ?? []
switch (ratio) {
case 'kg':
dimension.kg = Number(val)
break
case 'sec':
dimension.sec = Number(val)
break
case 'mol':
dimension.mol = Number(val)
break
case 'm':
dimension.m = Number(val)
break
case 'K':
dimension.K = Number(val)
break
case 'A':
dimension.A = Number(val)
break
case 'cd':
dimension.cd = Number(val)
break
}
})
return Object.keys(dimension).length
? new UnitDimensions(dimension)
: undefined
}
private static parseNumber(str: string): number {
const num = Number(str.trim())
if (isNaN(num) || typeof num !== 'number') {
throw new Error(`Unable to convert '${str}' to a number`)
}
return num
}
/**
* @returns The ids for the units.
*/
public get ids(): string[] {
return this.#ids
}
/**
* @returns The unit's name.
*/
public get name(): string {
return this.#ids[0] ?? ''
}
/**
* @returns The unit's symbol.
*/
public get symbol(): string {
return this.#ids[this.#ids.length - 1] ?? ''
}
/**
* @returns The unit's scale.
*/
public get scale(): number {
return this.#scale
}
/**
* @returns The unit's offset.
*/
public get offset(): number {
return this.#offset
}
/**
* @returns The unit's quantity or undefined if none available.
*/
public get quantity(): string | undefined {
return this.#quantity
}
/**
* @returns A list of all the quantities available.
*/
public static get quantities(): string[] {
return getDb().quantities
}
/**
* Return the units for the quantity.
*
* @param quanity The quantity to search for.
* @returns An array of quantities. An empty array is returned
* if the quantity can't be found.
*/
public static getUnitsForQuantity(quanity: string): readonly HUnit[] {
return getDb().getUnitsForQuantity(quanity)
}
/**
* @returns All the registered units.
*/
public static get units(): readonly HUnit[] {
return getDb().units
}
/**
* @returns The unit's dimensions or undefined if dimensionless.
*/
public get dimensions(): UnitDimensions | undefined {
return this.#dimensions
}
/**
* Return a unit that can be used when multiplying two numbers together.
*
* @param unit The unit to multiply.
* @return The unit to multiply by.
* @throws An error if the units can't be multiplied.
*/
public multiply(unit: HUnit): HUnit {
return getDb().multiply(this, unit)
}
/**
* Return a unit that can be used when dividing two numbers together.
*
* @param unit The unit to divide.
* @return The unit to divide by.
* @throws An error if the units can't be divided.
*/
public divide(unit: HUnit): HUnit {
return getDb().divide(this, unit)
}
/**
* @returns The unit as a JSON object.
*/
public toJSON(): UnitData {
const data: UnitData = {
ids: this.#ids,
scale: this.#scale,
offset: this.#offset,
}
if (this.#quantity) {
data.quantity = this.#quantity
}
if (this.#dimensions) {
data.dimensions = this.#dimensions?.toJSON()
}
return data
}
/**
* Equality for units.
*
* @param unit The value to compare against.
* @returns True if the units are equal.
*/
public equals(unit?: HUnit): boolean {
if (!unit) {
return false
}
// Don't take into account `quantity` as this used
// merely for organization.
if (this.ids.length !== unit.ids.length) {
return false
}
for (let i = 0; i < this.ids.length; ++i) {
if (this.ids[i] !== unit.ids[i]) {
return false
}
}
if (
this.#dimensions
? !this.#dimensions.equals(unit.dimensions)
: unit.dimensions
) {
return false
}
if (!HUnit.isApproximate(this.scale, unit.scale)) {
return false
}
if (this.offset !== unit.offset) {
return false
}
return true
}
/**
* @returns A string representation of a unit.
*/
public toString(): string {
return this.symbol
}
/**
* Dump the current state of the unit database to the console output.
*/
public static inspectDb(): void {
console.table(getDb().units.map((unit) => unit.toJSON()))
}
/**
* Convert the unit to the new scalar.
*
* @param scalar The new scalar value.
* @param to The new unit to convert too.
* @returns The new scalar value.
*/
public convertTo(scalar: number, to: HUnit): number {
// Bytes have no dimension so handle as a special case.
if (
!(isByteUnit(this) && isByteUnit(to)) &&
!this.#dimensions?.equals(to.dimensions)
) {
throw new Error(`Inconvertible units: ${this} and ${to}`)
}
return (scalar * this.scale + this.offset - to.offset) / to.scale
}
/**
* Returns true if the two numbers approximately match.
*
* @param a The first number.
* @param b The second number.
* @returns True if the two numbers match.
*/
public static isApproximate(a: number, b: number): boolean {
if (a === b) {
return true
}
// Pretty loose with our approximation because the database
// doesn't have super great resolution for some normalizations.
const t = Math.min(Math.abs(a / 1e3), Math.abs(b / 1e3))
return Math.abs(a - b) <= t
}
}