This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
/
SiType.ts
182 lines (161 loc) · 5.07 KB
/
SiType.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
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
/**
* @module
* @overview
*
* This module provides `SiType` enumeration and utilities for working with it.
*
* The `SiType` enumeration consists of a set of predefined values representing
* various units of measurement according to the International System of Units (SI).
*
* The module also provides functions for parsing, stringifying, and checking
* the validity of these values.
*
* ### Using the SiType Enumeration
*
* To use the `SiType` enumeration in your code, you will first need to import it
* from the module that defines it.
*
* You can do this by adding the following line at the top of your file:
*
* ```typescript
* import { SiType } from "./fi/hg/core/types/SiType";
* ```
*
* Once you have imported the SiType enumeration, you can use the constants it
* defines in your code. For example, you could use the SiType.KILO constant in
* a function like this:
*
* ```typescript
* function convertToKilometers (distanceInMeters: number): number {
* return distanceInMeters / 1000;
* }
* const translations = {
* [SiType.KILO]: "kilometers",
* // ...add more translations for other SiType values as needed
* };
* const distanceInKilometers = convertToKilometers(5000); // 5
* console.log(`The distance is ${distanceInKilometers} ${translations[SiType.KILO]}.`); // The distance is 5 kilometers. *
* ```
*
* ### Using the Utility Functions
*
* The module provides three utility functions that you can use to work with
* `SiType` values: `isSiType`, `stringifySiType`, and `parseSiType`.
*
* Here's an example of how you might use these functions in your code:
*
* ```typescript
* import { isSiType, stringifySiType, parseSiType } from "./path/to/si-type-module";
*
* // Check if a value is a valid SiType
* if (isSiType(SiType.MEGA)) {
* console.log("This is a valid SiType value.");
* } else {
* console.log("This is not a valid SiType value.");
* }
*
* // Convert an SiType value to a string
* const siTypeString = stringifySiType(SiType.MEGA); // "MEGA"
*
* // Parse a value into an SiType
* const siTypeValue = parseSiType("mega"); // SiType.MEGA
* ```
*/
import { toUpper } from "../functions/toUpper";
import { isArray } from "./Array";
import { isString } from "./String";
/**
* An enumeration of predefined SI standard values.
*/
export enum SiType {
NONE = "NONE",
KILO = "KILO",
MEGA = "MEGA",
GIGA = "GIGA",
TERA = "TERA",
PETA = "PETA",
EXA = "EXA",
ZETTA = "ZETTA",
YOTTA = "YOTTA"
}
/**
* A type guard function that checks if a value is a valid SiType.
*
* @param value - The value to check.
* @returns {boolean} `true` if the value is a valid SiType, `false` otherwise.
* @throws {None} This function does not throw any exceptions.
* @nosideeffects
*/
export function isSiType (value: any): value is SiType {
switch (value) {
case SiType.NONE:
case SiType.KILO:
case SiType.MEGA:
case SiType.GIGA:
case SiType.TERA:
case SiType.PETA:
case SiType.EXA:
case SiType.ZETTA:
case SiType.YOTTA:
return true;
default:
return false;
}
}
/**
* Converts an SiType value to its string representation.
*
* @param value - The SiType value to stringify.
* @returns {string} The string representation of the SiType value.
* @throws {TypeError} If the provided SiType value is not recognized.
* @nosideeffects
*/
export function stringifySiType (value: SiType): string {
switch (value) {
case SiType.NONE : return 'NONE';
case SiType.KILO : return 'KILO';
case SiType.MEGA : return 'MEGA';
case SiType.GIGA : return 'GIGA';
case SiType.TERA : return 'TERA';
case SiType.PETA : return 'PETA';
case SiType.EXA : return 'EXA';
case SiType.ZETTA : return 'ZETTA';
case SiType.YOTTA : return 'YOTTA';
}
throw new TypeError(`Unsupported SiType value: ${value}`);
}
/**
* Attempts to parse a value into an SiType.
*
* @param value - The value to parse.
* @returns The parsed SiType value, or `undefined` if the value could not be parsed.
* @throws {None} This function does not throw any exceptions.
* @nosideeffects
*/
export function parseSiType (value: any): SiType | undefined {
if (isArray(value)) return undefined;
if (!isString(value)) value = `${value}`;
value = value.length === 1 ? value : toUpper(value);
switch ( value ) {
case '':
case 'NONE' : return SiType.NONE;
case 'k':
case 'KILO' : return SiType.KILO;
case 'M':
case 'MEGA' : return SiType.MEGA;
case 'G':
case 'GIGA' : return SiType.GIGA;
case 'T':
case 'TERA' : return SiType.TERA;
case 'P':
case 'PETA' : return SiType.PETA;
case 'E':
case 'EXA' : return SiType.EXA;
case 'Z':
case 'ZETTA' : return SiType.ZETTA;
case 'Y':
case 'YOTTA' : return SiType.YOTTA;
default : return undefined;
}
}