@oliasoft-open-source/units 5.2.0 → 5.3.0

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.
Files changed (3) hide show
  1. package/README.md +133 -57
  2. package/dist/README.md +133 -57
  3. package/package.json +27 -28
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Oliasoft Unit Handling
2
2
 
3
3
  ##### Table of Contents
4
+
4
5
  - [Introduction](#introduction)
5
6
  - [Install](#install)
6
7
  - [Basic usage](#basic-usage)
@@ -12,7 +13,7 @@
12
13
 
13
14
  ## Introduction
14
15
 
15
- Hi, and welcome to Oliasoft's Unit handling repository! [Oliasoft](https://www.oliasoft.com) is using this to convert numbers between different units in all of our products. As you probably know we are doing various physics calculation that relies on precise definitions for all input data. We need to know the valid *range* and *precision* of a value as well as what *unit* the given value represent. Before doing calculations we therefor convert all numbers to proper JavaScript numbers in a given known unit used in the calculations. We refer to these values as **calculation units**. The users of our products are however allowed to input values, either through the GUI or directly using the APIs, in any unit they prefer. We refer to these values as **input units**. We always store the input value together with its unit *unconverted*, hence input units is the same as the **stored units**. Finally, we offer the users to view all graphs and numbers in their preferred units. We refer these values as **view units** (GUI) or **output units** (API).
16
+ Hi, and welcome to Oliasoft's Unit handling repository! [Oliasoft](https://www.oliasoft.com) is using this to convert numbers between different units in all of our products. As you probably know we are doing various physics calculation that relies on precise definitions for all input data. We need to know the valid _range_ and _precision_ of a value as well as what _unit_ the given value represent. Before doing calculations we therefor convert all numbers to proper JavaScript numbers in a given known unit used in the calculations. We refer to these values as **calculation units**. The users of our products are however allowed to input values, either through the GUI or directly using the APIs, in any unit they prefer. We refer to these values as **input units**. We always store the input value together with its unit _unconverted_, hence input units is the same as the **stored units**. Finally, we offer the users to view all graphs and numbers in their preferred units. We refer these values as **view units** (GUI) or **output units** (API).
16
17
 
17
18
  All values with numbers are stored as a string with the following special format;
18
19
 
@@ -20,19 +21,17 @@ All values with numbers are stored as a string with the following special format
20
21
 
21
22
  Number here is very flexible and all of the following is valid "number" strings:
22
23
 
23
- * 2.13
24
- * -2,13
25
- * 4e-05
26
- * 4E+3
27
- * -120,000.02
28
- * -20 1/2
29
-
30
- I.e. it support both . and , as comma/thousands separator, exponents and fractions. Examples of different values can be seen in the [units tests](src\__tests__\units.test.ts). By storing the inputted value without any conversions, we maintain valuable precision where needed and also provide the user with recognizable values. We use *base* units, as described in for example International System of Units (SI), for conversions. By converting all units to the base first, through *intermediate convesions*, we are able to keep the amount of conversion permutations to a minimum. The same unit can exists in multiple *quantities* and we allow custom quantity categories for application flexibility.
24
+ - 2.13
25
+ - -2,13
26
+ - 4e-05
27
+ - 4E+3
28
+ - -120,000.02
29
+ - -20 1/2
31
30
 
31
+ I.e. it support both . and , as comma/thousands separator, exponents and fractions. Examples of different values can be seen in the [units tests](src__tests__\units.test.ts). By storing the inputted value without any conversions, we maintain valuable precision where needed and also provide the user with recognizable values. We use _base_ units, as described in for example International System of Units (SI), for conversions. By converting all units to the base first, through _intermediate convesions_, we are able to keep the amount of conversion permutations to a minimum. The same unit can exists in multiple _quantities_ and we allow custom quantity categories for application flexibility.
32
32
 
33
33
  ## Install
34
34
 
35
-
36
35
  ```bash
37
36
  npm i @oliasoft-open-source/units --save
38
37
  ```
@@ -44,6 +43,7 @@ import { convertAndGetValue, withUnit } from '@oliasoft-open-source/units';
44
43
  ## Basic usage
45
44
 
46
45
  ### Adding units to a number
46
+
47
47
  As mentioned in the introduction this repository is working with numbers and units represented in a special string format. In order to get a string with number and unit use [withUnit](#withunitvalue-stringnumber-unit-stringnull-defaultval). Example:
48
48
 
49
49
  ```js
@@ -61,7 +61,8 @@ const [value, unit] = split(myValueWithUnit); // ['3.14, 'cm']
61
61
  > Never manipulate the strings directly in case we ever change the syntax!
62
62
 
63
63
  ### Converting number to calculation units
64
- Before doing any calculations with variables stored in our special string format, we need to convert the values from the *stored units* to the *calculation units*. Knowing the target unit we will use the [convertAndGetValue](#convertandgetvaluenumwithunit-tounit-fromunit) method for converting and returning the number in the target calculation unit:
64
+
65
+ Before doing any calculations with variables stored in our special string format, we need to convert the values from the _stored units_ to the _calculation units_. Knowing the target unit we will use the [convertAndGetValue](#convertandgetvaluenumwithunit-tounit-fromunit) method for converting and returning the number in the target calculation unit:
65
66
 
66
67
  ```javascript
67
68
  const value = convertAndGetValue(number, toUnit, fromUnit);
@@ -77,11 +78,12 @@ value = convertAndGetValue('10|m', 'in'); // 393.7007874015748
77
78
  value = convertAndGetValue('10', 'in', 'm'); // 393.7007874015748
78
79
  ```
79
80
 
80
- If you want to convert values *without* units directly to another unit, you can use the [to](#tovalue-fromunit-tounit) method. Both pure numbers and string representing numbers are converted.
81
+ If you want to convert values _without_ units directly to another unit, you can use the [to](#tovalue-fromunit-tounit) method. Both pure numbers and string representing numbers are converted.
81
82
 
82
83
  ```js
83
84
  const convertedValue = to(10, 'm', 'in'); // 393.7007874015748
84
85
  ```
86
+
85
87
  same as
86
88
 
87
89
  ```js
@@ -89,28 +91,30 @@ const convertedValue = to('10', 'm', 'in'); // 393.7007874015748
89
91
  ```
90
92
 
91
93
  ### Working with Quantities
92
- **Quantities** are lists of units that represent the same measurement of a given quantity, for example *length* measured in *m*, *in*, *ft* etc. We have a few different methods for getting the units and quantities. One can use [getQuantities](#get-list-of-all-defined-quantities) to get a list of all the defined quantities or unit categories if you will. Given a quantity there's multiple methods to get units for that quantity. Use [getUnitsForQuantity](#getunitsforquantityquantity) to get a pure list of units, or you can get a list of the objects following the `AltUnitWithLabel` interface by calling [getAltUnitsListByQuantity](#getaltunitslistbyquantityquantity) which will also return formatted labels for the given unit. For example, m³ for cubic meters. The same labels can also be looked up directly by calling [label](#labelunitkey). To get the base unit for a given quantity one can use the method [unitFromQuantity](#unitfromquantityquantity).
93
94
 
95
+ **Quantities** are lists of units that represent the same measurement of a given quantity, for example _length_ measured in _m_, _in_, _ft_ etc. We have a few different methods for getting the units and quantities. One can use [getQuantities](#get-list-of-all-defined-quantities) to get a list of all the defined quantities or unit categories if you will. Given a quantity there's multiple methods to get units for that quantity. Use [getUnitsForQuantity](#getunitsforquantityquantity) to get a pure list of units, or you can get a list of the objects following the `AltUnitWithLabel` interface by calling [getAltUnitsListByQuantity](#getaltunitslistbyquantityquantity) which will also return formatted labels for the given unit. For example, m³ for cubic meters. The same labels can also be looked up directly by calling [label](#labelunitkey). To get the base unit for a given quantity one can use the method [unitFromQuantity](#unitfromquantityquantity).
94
96
 
95
97
  ```js
96
98
  getQuantities(); // ['acceleration', 'angleGradient', 'angles', 'areaOther', ...]
97
99
  getUnitsForQuantity('angle'); // ['deg', 'rad']
98
- unitFromQuantity('force') // 'N'
100
+ unitFromQuantity('force'); // 'N'
99
101
  ```
100
102
 
101
103
  ### Working with fractions
104
+
102
105
  Since this library is representing numbers and units with strings, we also have the flexibility to work directly with fractions. Use [fraction](#fractionstr) to convert a fraction, given as a string, to the corresponding numeric representation. Similar one can use [asFraction](#asfractionstr) to convert a decimal number to a fraction (if it exist). One can also use the method [numFraction](#numfractionstr) to convert from fraction to number, which will return the input if the conversion fails unlike `fraction` which returns `NaN` for failed conversions.
103
106
 
104
107
  ```js
105
- const half = asFraction('0.5') // '1/2';
106
- const numeric = fraction(half) // 0.5;
107
- fraction('13/0') // Infinity;
108
- fraction('Garbage') // NaN;
109
- numFraction('Garbage') // 'Garbage';
108
+ const half = asFraction('0.5'); // '1/2';
109
+ const numeric = fraction(half); // 0.5;
110
+ fraction('13/0'); // Infinity;
111
+ fraction('Garbage'); // NaN;
112
+ numFraction('Garbage'); // 'Garbage';
110
113
  ```
111
114
 
112
115
  ## Input and conversions
113
- Several methods exist for processing GUI inputs and making sure they are valid numbers. Raw inputs from the user can be fed through [validateAndClean](#validateandcleanpreviousvalue-nexttext) method, which returns a new valid *number-unit* string.
116
+
117
+ Several methods exist for processing GUI inputs and making sure they are valid numbers. Raw inputs from the user can be fed through [validateAndClean](#validateandcleanpreviousvalue-nexttext) method, which returns a new valid _number-unit_ string.
114
118
 
115
119
  ```js
116
120
  validateAndClean('123-', '1234-'); // '1234';
@@ -145,6 +149,7 @@ isNumeric('e20'); // false
145
149
  ```
146
150
 
147
151
  ## Formatting
152
+
148
153
  For printing nice numbers in GUI and reports we offer different helpers. As already mentioned, one can get formatted unit labels, for example m³ for cubic meters, by calling [label](#labelunitkey). Calling [roundNumberWithLabel](#roundnumberwithlabelvalue-roundto--2) gives you a value, rounded to the wanted precision, with unit label. Use [round](#roundnumbernum-round--4) to round just the value. Depending on what value that is stored in the string it can sometimes be hard to know what precision to use for display. The method [getNumberOfDigitsToShow](#getnumberofdigitstoshownum-maxnumdigits--20) will analyse the input and suggest a reasonable precision for you.
149
154
 
150
155
  ```js
@@ -154,7 +159,8 @@ const outputValue = round(1e9, noOfDigits); // 0.0000000001
154
159
  ```
155
160
 
156
161
  ## Working with Tables
157
- Most of the methods in the library works with single *value-unit* strings and for tables we encourage you to store the *value-unit* string directly as keyed objects.
162
+
163
+ Most of the methods in the library works with single _value-unit_ strings and for tables we encourage you to store the _value-unit_ string directly as keyed objects.
158
164
  Example:
159
165
 
160
166
  ```js
@@ -164,7 +170,7 @@ const myTable = [
164
170
  ];
165
171
  ```
166
172
 
167
- That said, we also have support for a special custom table format where the first row specifies the **unit** and the following rows stores pure **numbers** *without unit*.
173
+ That said, we also have support for a special custom table format where the first row specifies the **unit** and the following rows stores pure **numbers** _without unit_.
168
174
 
169
175
  Example:
170
176
 
@@ -172,7 +178,7 @@ Example:
172
178
  const myExcelTable = [
173
179
  ['m', 'ft', 'in', 'kg/m3'],
174
180
  [15, 42, 35, 42],
175
- [16, 43, 36, 50]
181
+ [16, 43, 36, 50],
176
182
  ];
177
183
  ```
178
184
 
@@ -189,11 +195,11 @@ convertTable(['cm', 'ft', 'cm', 'sg'], myExcelTable);
189
195
  */
190
196
  ```
191
197
 
192
-
193
198
  ## Methods
194
199
 
195
200
  ### withUnit(value, unit, defaultVal = '') {...}
196
- #### Get a *value-unit* string, i.e. value with unit splitted by | separator
201
+
202
+ #### Get a _value-unit_ string, i.e. value with unit splitted by | separator
197
203
 
198
204
  ```js
199
205
  withUnit(1.123, 'm'); // '1.123|m'
@@ -201,12 +207,15 @@ withUnit(-10.314, 'K/100m'); // '-10.314|K/100m'
201
207
  ```
202
208
 
203
209
  ### unumWithUnit(numWithUnit, toUnit, fromUnit?) {...}
204
- #### Converts to given *toUnit* and return the converted value with unit
210
+
211
+ #### Converts to given _toUnit_ and return the converted value with unit
212
+
205
213
  ```js
206
214
  unumWithUnit(2.2, 'kg/m3', 'sg'); // '2200|kg/m3'
207
215
  ```
208
216
 
209
217
  ### isEmptyValueWithUnit(val) {...}
218
+
210
219
  #### Checks if input is a string that starts with '|', e.g. '|m' or '|in'
211
220
 
212
221
  ```js
@@ -216,7 +225,9 @@ isEmptyValueWithUnit('m'); // false
216
225
  ```
217
226
 
218
227
  ### isValueWithUnit(value) {...}
228
+
219
229
  #### Takes user input and returns `true` if is value with unit and false in every other case
230
+
220
231
  ```js
221
232
  isValueWithUnit('m'); // false
222
233
  isValueWithUnit('5'); // false
@@ -225,9 +236,10 @@ isValueWithUnit('5|m'); // true
225
236
  ```
226
237
 
227
238
  ### isNumeric(val) {...}
239
+
228
240
  #### Check if provided argument is number
229
241
 
230
- ```js
242
+ ```js
231
243
  isNumeric(1e20); // true
232
244
  isNumeric('1e20'); // true
233
245
  isNumeric(NaN); // false
@@ -235,7 +247,9 @@ isNumeric(Infinity); // false
235
247
  ```
236
248
 
237
249
  ### allNumbers(arr) {...}
250
+
238
251
  #### Check array values if all are numbers
252
+
239
253
  ```js
240
254
  allNumbers([1, 2, 1.2, 5]); // true
241
255
  allNumbers([1, 2, '1.2', 5]); // false
@@ -243,6 +257,7 @@ allNumbers([1, 1, 2, Infinity, 1.2]); // true
243
257
  ```
244
258
 
245
259
  ### formatNumber(number) {...}
260
+
246
261
  #### Outputs "pretty" number (with thousands separators), taken from [this](https://www.wikitechy.com/tutorials/javascript/print-a-number-with-commas-as-thousands-separators-in-javascript)
247
262
 
248
263
  ```js
@@ -252,6 +267,7 @@ formatNumber('100000.123'); // '100,000.123';
252
267
  ```
253
268
 
254
269
  ### charCount(chr, str)
270
+
255
271
  #### Counts all occurence of given character
256
272
 
257
273
  ```js
@@ -261,6 +277,7 @@ charCount(0, '100000.123'); // 5;
261
277
  ```
262
278
 
263
279
  ### validateAndClean(previousValue, nextText) {...}
280
+
264
281
  #### Validates and cleans raw text numeric user input, typically from user input. The previous value is for optionally determining the pre-existing unit. The next text is a raw input string. The return value is reformatted from the next text (removing invalid patterns)
265
282
 
266
283
  ```js
@@ -272,6 +289,7 @@ validateAndClean('2|m', '2e-3'); // '2e-3|m'
272
289
  ```
273
290
 
274
291
  ### getNumberOfDigitsToShow(num, maxNumDigits = 20) {...}
292
+
275
293
  #### Calculates the number of digits to be rounded off, typically for values less than 1 E.g. when trying to format 1e-9 byroundNumber(), the output value will only show '0' if just rounded off with 4 digits fromroundNumber(val) Then it is more useful to calculate the number of digits to be rounded off, and pass this in, i.e.round(val, getNumberOfDigitsToShow(val)) which will return 0.0000000001.
276
294
 
277
295
  ```js
@@ -283,7 +301,9 @@ getNumberOfDigitsToShow('100000.123'); // 4
283
301
  getNumberOfDigitsToShow('100000.123', 3); // 3
284
302
  getNumberOfDigitsToShow(0); // 4
285
303
  ```
304
+
286
305
  ### round(num, round = 4)
306
+
287
307
  #### Formating / rounding number provided in argument
288
308
 
289
309
  ```js
@@ -291,11 +311,13 @@ round(1.11231231); // 1.1123
291
311
  round('100000.123'); // '100000.123'
292
312
  round('100000.123', 2); // '100000.12'
293
313
  round('100000.123|m', 2); // '100000.12|m'
294
- round(null, 2) // null
314
+ round(null, 2); // null
295
315
  ```
296
316
 
297
317
  ### roundNumberWithLabel(value, roundTo = 2) {...}
318
+
298
319
  #### Round input value and return with labeled unit
320
+
299
321
  ```js
300
322
  roundNumberWithLabel('1000.1284325|kg/m3'); // '1000.13 kg/m³'
301
323
  roundNumberWithLabel('-999.999991|kg/m3', 5); // '-999.99999 kg/m³'
@@ -303,7 +325,8 @@ roundNumberWithLabel('-999.999999|kg/m3', 5); // '-1000 kg/m³'
303
325
  ```
304
326
 
305
327
  ### fraction(str)
306
- #### Convert set fraction to decimal value will return either number in decimal format, Infinity if fraction is divided by 0
328
+
329
+ #### Convert set fraction to decimal value will return either number in decimal format, Infinity if fraction is divided by 0
307
330
 
308
331
  ```js
309
332
  fraction('1/3'); // 0.333
@@ -316,43 +339,54 @@ fraction([1, 2]); // NaN
316
339
  fraction(''); // NaN
317
340
  ```
318
341
 
319
- ### unitFromQuantity(quantity) {...}
342
+ ### unitFromQuantity(quantity) {...}
343
+
320
344
  #### Get base unit from given quantity
345
+
321
346
  ```js
322
347
  unitFromQuantity('force'); // 'N';
323
- unitFromQuantity('notsupported'); // undefined;
348
+ unitFromQuantity('notsupported'); // undefined;
324
349
  ```
325
350
 
326
351
  ### getAltUnitsListByQuantity(quantity) {...}
352
+
327
353
  #### Get list of alternative units, with labels, for a given quantity.
354
+
328
355
  ```js
329
356
  getAltUnitsListByQuantity('angles'); // [{unit: 'deg', label: '°'}, {unit: 'rad', label: 'rad'}];
330
357
  getAltUnitsListByQuantity('qwe123'); // undefined;
331
358
  ```
332
359
 
333
360
  ### getUnitsForQuantity(quantity) {...}
361
+
334
362
  #### Get list of units for a given quantity
363
+
335
364
  ```js
336
365
  getUnitsForQuantity('force'); // ['tonnes', 'lbf', 'kgf', 'N', 'kN', 'tonneForce', 'klbf']
337
366
  getUnitsForQuantity('depth'); // ['m', 'ft']
338
367
  getUnitsForQuantity('acceleration'); // ['ft/s2', 'm/s2'])
339
- getUnitsForQuantity('angles');// ['deg', 'rad']);
368
+ getUnitsForQuantity('angles'); // ['deg', 'rad']);
340
369
  getUnitsForQuantity('dls'); // ['deg/10m', 'deg/30m', 'deg/100ft'])
341
370
  getUnitsForQuantity('shit'); // undefined
342
371
  ```
343
372
 
344
373
  ### toBase(value, quantity) {...}
374
+
345
375
  #### Convert value to the base unit given by the quantity
376
+
346
377
  ```js
347
378
  toBase('1|m', 'length'); // 1
348
379
  toBase('1|cm', 'length'); // 0.01
349
380
  toBase('1|tonnes', 'weight'); // 1000
350
381
  ```
382
+
351
383
  ### altUnitsList(value, quantity, defaultUnit?) {...}
384
+
352
385
  #### Get list of values, with same precision as the given value, in all the units of the given quantity
386
+
353
387
  ```js
354
388
  altUnitsList('10|m', 'length');
355
- /* [['10', 'm', 'm'],
389
+ /* [['10', 'm', 'm'],
356
390
  ['32.8', 'ft', 'ft'],
357
391
  ['0.01', 'km', 'km'],
358
392
  ['394', 'in', 'in'],
@@ -364,13 +398,17 @@ altUnitsList('180', 'deg');
364
398
  ['3.14', 'rad', 'rad']]
365
399
  */
366
400
  ```
401
+
367
402
  ### convertTable(toUnitRow, table, defaultUnitRow?, removeFinalUnitsRow=false) {...}
403
+
368
404
  #### Convert table of values to another unit
405
+
369
406
  ```js
370
407
  const table = [
371
408
  ['m', 'ft', 'in', 'kg/m3'],
372
409
  [15, 42, 35, 42],
373
- [16, 43, 36, 50]];
410
+ [16, 43, 36, 50],
411
+ ];
374
412
  convertTable(['cm', 'ft', 'cm', 'sg'], table);
375
413
  /*
376
414
  [
@@ -381,8 +419,8 @@ convertTable(['cm', 'ft', 'cm', 'sg'], table);
381
419
  */
382
420
  ```
383
421
 
384
-
385
422
  ### getQuantities() {...}
423
+
386
424
  #### Get list of all defined quantities
387
425
 
388
426
  ```js
@@ -390,6 +428,7 @@ getQuantities(); // ['acceleration', 'angleGradient', 'angles', 'areaOther', ...
390
428
  ```
391
429
 
392
430
  ### checkAndCleanDecimalComma(val) {...}
431
+
393
432
  #### Find double dot and comma in value and replace it with decimal dot. For example: 123,4 => 123.4 or 123..4 => 123,4
394
433
 
395
434
  ```js
@@ -399,6 +438,7 @@ checkAndCleanDecimalComma('36..6'); // '36.6';
399
438
  ```
400
439
 
401
440
  ### to(value, fromUnit, toUnit) {...}
441
+
402
442
  #### Convert value to another unit
403
443
 
404
444
  ```js
@@ -416,14 +456,18 @@ to('1,,.12', 'rad', 'deg').toFixed(4); // '64.1713'
416
456
  ```
417
457
 
418
458
  ### split(numWithUnit) {...}
459
+
419
460
  #### Split string into value and unit.
461
+
420
462
  ```js
421
- split('-12,2m'); // ['-12.2', 'm'];
422
- split('-12 1/2m'); // ['-12 1/2', 'm'];
463
+ split('-12,2m'); // ['-12.2', 'm'];
464
+ split('-12 1/2m'); // ['-12 1/2', 'm'];
423
465
  ```
424
466
 
425
467
  ### getValue(numWithUnit) {...}
468
+
426
469
  #### Get unit of the number with unit string ("1|m") will return "m"
470
+
427
471
  ```js
428
472
  getValue('12.2'); // '12.2'
429
473
  getValue('12.2m'); // '12.2'
@@ -433,7 +477,9 @@ getValue('m'); // '';
433
477
  ```
434
478
 
435
479
  ### getUnit(numWithUnit) {...}
480
+
436
481
  #### Get unit of the number with unit string
482
+
437
483
  ```js
438
484
  getUnit('-2|in2'); // 'in2'
439
485
  getUnit('12.2|m'); // 'm'
@@ -442,19 +488,22 @@ getUnit('|m'); // 'm';
442
488
  getUnit('12.2'); // '';
443
489
  ```
444
490
 
445
-
446
491
  ### label(unitKey) {...}
492
+
447
493
  #### Returns a print friendly unit representation
494
+
448
495
  ```js
449
- label('m3'); // 'm³'
496
+ label('m3'); // 'm³'
450
497
  label('1/bar'); // 'bar⁻¹'
451
498
  ```
452
499
 
453
500
  ### convertAndGetValue(numWithUnit, toUnit, fromUnit?) {...}
501
+
454
502
  #### Convert value with unit to another unit Will try to pick `fromUnit` from `numWithUnit` if it was not provided
503
+
455
504
  ```js
456
505
  convertAndGetValue('1 1/2', 'in', 'in'); // 1.5
457
- convertAndGetValue('1 1/2 in', 'in'); // 1.5
506
+ convertAndGetValue('1 1/2 in', 'in'); // 1.5
458
507
  convertAndGetValue('-1 1/2 in', 'in'); // -1.5
459
508
  convertAndGetValue(2.2, 'notsupported', 'notsupported'); // 2.2
460
509
  convertAndGetValue(2.2, 'kg/m3', 'sg'); // 2200
@@ -462,7 +511,9 @@ convertAndGetValue('2.2', 'kg/m3', 'sg'); // 2200
462
511
  ```
463
512
 
464
513
  ### convertSamePrecision(numWithUnit, toUnit, digits?) {...}
514
+
465
515
  #### Convert value with unit to another unit and display it in pretty format. It will preserv the number of digits in the input or alternativly converting to the given number of digits.
516
+
466
517
  ```js
467
518
  convertSamePrecision('1|in', 'cm', 8); // '2.54|cm'
468
519
  convertSamePrecision('102e-6|in', 'cm'); // '0.000259|cm'
@@ -471,14 +522,17 @@ convertSamePrecision('10.000|m', 'in'); // '393.7|in'
471
522
  ```
472
523
 
473
524
  ### asFraction(str) {...}
525
+
474
526
  #### Converts decimal number to fractional format return string with fractional format of set value
527
+
475
528
  ```js
476
529
  asFraction(''); // '0'
477
530
  asFraction('0.1'); // '1/10'
478
531
  ```
479
532
 
480
533
  ### numFraction(str) {...}
481
- #### Convert fraction string to number (return input value if conversion fails) For historical reasons, numFraction returns the string value unmodified if it is not able to convert to a number. This is useful where user inputs are filtered through calls to numFraction. For "detecting" when numFraction fails, check if the return value is a string or a number. If it is a string it means number conversion failed. will return string with decimal format of fraction
534
+
535
+ #### Convert fraction string to number (return input value if conversion fails) For historical reasons, numFraction returns the string value unmodified if it is not able to convert to a number. This is useful where user inputs are filtered through calls to numFraction. For "detecting" when numFraction fails, check if the return value is a string or a number. If it is a string it means number conversion failed. will return string with decimal format of fraction
482
536
 
483
537
  ```js
484
538
  numFraction(''); // ''
@@ -486,9 +540,10 @@ numFraction('1/10'); // 0.1
486
540
  numFraction(null); // null
487
541
  ```
488
542
 
489
-
490
543
  ### cleanNumStr(str){...}
544
+
491
545
  #### Cleaning up and fixing provided number to correct numerical format removing redundant '.' dots, ',' commas, spaces
546
+
492
547
  ```js
493
548
  cleanNumStr('1000,000.1'); // '1000000.1'
494
549
  cleanNumStr('1000,000,000'); // '1000000000'
@@ -497,6 +552,7 @@ cleanNumStr('1000,000,000.1.1'); // '100000000011'
497
552
  ```
498
553
 
499
554
  ### cleanNum(str): {...}
555
+
500
556
  #### Cleaning and fixing numerical string but returns it as number
501
557
 
502
558
  ```js
@@ -505,70 +561,90 @@ cleanNum(',1'); // 0.1
505
561
  ```
506
562
 
507
563
  ### toNum(input, defaultValue?, minimum?) {...}
564
+
508
565
  #### Convert provided argument to number or return it if impossible to convert
566
+
509
567
  ```js
510
568
  toNum(1); // 1
511
569
  toNum(',1'); // 0.1
512
570
  ```
513
571
 
514
572
  ### isNonNumerical(value)
573
+
515
574
  #### Check if value is non numerical
516
575
 
517
576
  ```js
518
- isNonNumerical('123.32asdasds4') // true
519
- isNonNumerical('123.32') // false
577
+ isNonNumerical('123.32asdasds4'); // true
578
+ isNonNumerical('123.32'); // false
520
579
  ```
521
580
 
522
581
  ## Constants
582
+
523
583
  #### LABELS
584
+
524
585
  ##### Units labels
586
+
525
587
  ```js
526
- LABELS.cm // 'cm'
527
- LABELS.lps // 'L/s'
588
+ LABELS.cm; // 'cm'
589
+ LABELS.lps; // 'L/s'
528
590
  ```
529
591
 
530
592
  #### ALT_UNITS
593
+
531
594
  ##### Alternative units grouped by quantity
595
+
532
596
  ```js
533
- ALT_UNITS.angles // ['deg', 'rad']
534
- ALT_UNITS.density // ['sg', 'ppg', 'kg/m3', 'lbm/ft3', 'g/cm3', 'lb/ft3']
597
+ ALT_UNITS.angles; // ['deg', 'rad']
598
+ ALT_UNITS.density; // ['sg', 'ppg', 'kg/m3', 'lbm/ft3', 'g/cm3', 'lb/ft3']
535
599
  ```
536
600
 
537
601
  #### UNIT_FROM_KEY
602
+
538
603
  ##### Units list
604
+
539
605
  ```js
540
- UNIT_FROM_KEY.length // 'm'
541
- UNIT_FROM_KEY.latitude // '°N'
606
+ UNIT_FROM_KEY.length; // 'm'
607
+ UNIT_FROM_KEY.latitude; // '°N'
542
608
  ```
543
609
 
544
610
  #### KNOWN_CONVERSIONS
611
+
545
612
  ##### Conversions list where each key 'from unit|to unit' pair
613
+
546
614
  ```js
547
- KNOWN_CONVERSIONS['m|mm'](1) // 1000
615
+ KNOWN_CONVERSIONS['m|mm'](1); // 1000
548
616
  ```
549
617
 
550
618
  #### DEPRECATED_UNITS
619
+
551
620
  ##### List of deprecated units
621
+
552
622
  ```js
553
- DEPRECATED_UNITS['N-m'] // 'Nm'
554
- DEPRECATED_UNITS['ft-lbf'] // 'ftlbf'
623
+ DEPRECATED_UNITS['N-m']; // 'Nm'
624
+ DEPRECATED_UNITS['ft-lbf']; // 'ftlbf'
555
625
  ```
556
626
 
557
627
  #### UNIT_ALIASES
628
+
558
629
  ##### This list is mapping from legal alternative unit names to our selected unit name
630
+
559
631
  ```js
560
- UNIT_ALIASES['lbs/ft'] // 'lb/ft'
632
+ UNIT_ALIASES['lbs/ft']; // 'lb/ft'
561
633
  ```
562
634
 
563
635
  #### INTERMEDIATE_CONVERSIONS
636
+
564
637
  ##### Intermediate conversions
638
+
565
639
  ```js
566
- INTERMEDIATE_CONVERSIONS.mm // 'm'
567
- INTERMEDIATE_CONVERSIONS.t // 'kg'
640
+ INTERMEDIATE_CONVERSIONS.mm; // 'm'
641
+ INTERMEDIATE_CONVERSIONS.t; // 'kg'
568
642
  ```
569
643
 
570
644
  #### SPECIAL_NUMBERS_STRING
645
+
571
646
  ##### Special numbers in string format
647
+
572
648
  ```js
573
- SPECIAL_NUMBERS_STRING // ['NaN', '-Infinity', 'Infinity']
649
+ SPECIAL_NUMBERS_STRING; // ['NaN', '-Infinity', 'Infinity']
574
650
  ```
package/dist/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Oliasoft Unit Handling
2
2
 
3
3
  ##### Table of Contents
4
+
4
5
  - [Introduction](#introduction)
5
6
  - [Install](#install)
6
7
  - [Basic usage](#basic-usage)
@@ -12,7 +13,7 @@
12
13
 
13
14
  ## Introduction
14
15
 
15
- Hi, and welcome to Oliasoft's Unit handling repository! [Oliasoft](https://www.oliasoft.com) is using this to convert numbers between different units in all of our products. As you probably know we are doing various physics calculation that relies on precise definitions for all input data. We need to know the valid *range* and *precision* of a value as well as what *unit* the given value represent. Before doing calculations we therefor convert all numbers to proper JavaScript numbers in a given known unit used in the calculations. We refer to these values as **calculation units**. The users of our products are however allowed to input values, either through the GUI or directly using the APIs, in any unit they prefer. We refer to these values as **input units**. We always store the input value together with its unit *unconverted*, hence input units is the same as the **stored units**. Finally, we offer the users to view all graphs and numbers in their preferred units. We refer these values as **view units** (GUI) or **output units** (API).
16
+ Hi, and welcome to Oliasoft's Unit handling repository! [Oliasoft](https://www.oliasoft.com) is using this to convert numbers between different units in all of our products. As you probably know we are doing various physics calculation that relies on precise definitions for all input data. We need to know the valid _range_ and _precision_ of a value as well as what _unit_ the given value represent. Before doing calculations we therefor convert all numbers to proper JavaScript numbers in a given known unit used in the calculations. We refer to these values as **calculation units**. The users of our products are however allowed to input values, either through the GUI or directly using the APIs, in any unit they prefer. We refer to these values as **input units**. We always store the input value together with its unit _unconverted_, hence input units is the same as the **stored units**. Finally, we offer the users to view all graphs and numbers in their preferred units. We refer these values as **view units** (GUI) or **output units** (API).
16
17
 
17
18
  All values with numbers are stored as a string with the following special format;
18
19
 
@@ -20,19 +21,17 @@ All values with numbers are stored as a string with the following special format
20
21
 
21
22
  Number here is very flexible and all of the following is valid "number" strings:
22
23
 
23
- * 2.13
24
- * -2,13
25
- * 4e-05
26
- * 4E+3
27
- * -120,000.02
28
- * -20 1/2
29
-
30
- I.e. it support both . and , as comma/thousands separator, exponents and fractions. Examples of different values can be seen in the [units tests](src\__tests__\units.test.ts). By storing the inputted value without any conversions, we maintain valuable precision where needed and also provide the user with recognizable values. We use *base* units, as described in for example International System of Units (SI), for conversions. By converting all units to the base first, through *intermediate convesions*, we are able to keep the amount of conversion permutations to a minimum. The same unit can exists in multiple *quantities* and we allow custom quantity categories for application flexibility.
24
+ - 2.13
25
+ - -2,13
26
+ - 4e-05
27
+ - 4E+3
28
+ - -120,000.02
29
+ - -20 1/2
31
30
 
31
+ I.e. it support both . and , as comma/thousands separator, exponents and fractions. Examples of different values can be seen in the [units tests](src__tests__\units.test.ts). By storing the inputted value without any conversions, we maintain valuable precision where needed and also provide the user with recognizable values. We use _base_ units, as described in for example International System of Units (SI), for conversions. By converting all units to the base first, through _intermediate convesions_, we are able to keep the amount of conversion permutations to a minimum. The same unit can exists in multiple _quantities_ and we allow custom quantity categories for application flexibility.
32
32
 
33
33
  ## Install
34
34
 
35
-
36
35
  ```bash
37
36
  npm i @oliasoft-open-source/units --save
38
37
  ```
@@ -44,6 +43,7 @@ import { convertAndGetValue, withUnit } from '@oliasoft-open-source/units';
44
43
  ## Basic usage
45
44
 
46
45
  ### Adding units to a number
46
+
47
47
  As mentioned in the introduction this repository is working with numbers and units represented in a special string format. In order to get a string with number and unit use [withUnit](#withunitvalue-stringnumber-unit-stringnull-defaultval). Example:
48
48
 
49
49
  ```js
@@ -61,7 +61,8 @@ const [value, unit] = split(myValueWithUnit); // ['3.14, 'cm']
61
61
  > Never manipulate the strings directly in case we ever change the syntax!
62
62
 
63
63
  ### Converting number to calculation units
64
- Before doing any calculations with variables stored in our special string format, we need to convert the values from the *stored units* to the *calculation units*. Knowing the target unit we will use the [convertAndGetValue](#convertandgetvaluenumwithunit-tounit-fromunit) method for converting and returning the number in the target calculation unit:
64
+
65
+ Before doing any calculations with variables stored in our special string format, we need to convert the values from the _stored units_ to the _calculation units_. Knowing the target unit we will use the [convertAndGetValue](#convertandgetvaluenumwithunit-tounit-fromunit) method for converting and returning the number in the target calculation unit:
65
66
 
66
67
  ```javascript
67
68
  const value = convertAndGetValue(number, toUnit, fromUnit);
@@ -77,11 +78,12 @@ value = convertAndGetValue('10|m', 'in'); // 393.7007874015748
77
78
  value = convertAndGetValue('10', 'in', 'm'); // 393.7007874015748
78
79
  ```
79
80
 
80
- If you want to convert values *without* units directly to another unit, you can use the [to](#tovalue-fromunit-tounit) method. Both pure numbers and string representing numbers are converted.
81
+ If you want to convert values _without_ units directly to another unit, you can use the [to](#tovalue-fromunit-tounit) method. Both pure numbers and string representing numbers are converted.
81
82
 
82
83
  ```js
83
84
  const convertedValue = to(10, 'm', 'in'); // 393.7007874015748
84
85
  ```
86
+
85
87
  same as
86
88
 
87
89
  ```js
@@ -89,28 +91,30 @@ const convertedValue = to('10', 'm', 'in'); // 393.7007874015748
89
91
  ```
90
92
 
91
93
  ### Working with Quantities
92
- **Quantities** are lists of units that represent the same measurement of a given quantity, for example *length* measured in *m*, *in*, *ft* etc. We have a few different methods for getting the units and quantities. One can use [getQuantities](#get-list-of-all-defined-quantities) to get a list of all the defined quantities or unit categories if you will. Given a quantity there's multiple methods to get units for that quantity. Use [getUnitsForQuantity](#getunitsforquantityquantity) to get a pure list of units, or you can get a list of the objects following the `AltUnitWithLabel` interface by calling [getAltUnitsListByQuantity](#getaltunitslistbyquantityquantity) which will also return formatted labels for the given unit. For example, m³ for cubic meters. The same labels can also be looked up directly by calling [label](#labelunitkey). To get the base unit for a given quantity one can use the method [unitFromQuantity](#unitfromquantityquantity).
93
94
 
95
+ **Quantities** are lists of units that represent the same measurement of a given quantity, for example _length_ measured in _m_, _in_, _ft_ etc. We have a few different methods for getting the units and quantities. One can use [getQuantities](#get-list-of-all-defined-quantities) to get a list of all the defined quantities or unit categories if you will. Given a quantity there's multiple methods to get units for that quantity. Use [getUnitsForQuantity](#getunitsforquantityquantity) to get a pure list of units, or you can get a list of the objects following the `AltUnitWithLabel` interface by calling [getAltUnitsListByQuantity](#getaltunitslistbyquantityquantity) which will also return formatted labels for the given unit. For example, m³ for cubic meters. The same labels can also be looked up directly by calling [label](#labelunitkey). To get the base unit for a given quantity one can use the method [unitFromQuantity](#unitfromquantityquantity).
94
96
 
95
97
  ```js
96
98
  getQuantities(); // ['acceleration', 'angleGradient', 'angles', 'areaOther', ...]
97
99
  getUnitsForQuantity('angle'); // ['deg', 'rad']
98
- unitFromQuantity('force') // 'N'
100
+ unitFromQuantity('force'); // 'N'
99
101
  ```
100
102
 
101
103
  ### Working with fractions
104
+
102
105
  Since this library is representing numbers and units with strings, we also have the flexibility to work directly with fractions. Use [fraction](#fractionstr) to convert a fraction, given as a string, to the corresponding numeric representation. Similar one can use [asFraction](#asfractionstr) to convert a decimal number to a fraction (if it exist). One can also use the method [numFraction](#numfractionstr) to convert from fraction to number, which will return the input if the conversion fails unlike `fraction` which returns `NaN` for failed conversions.
103
106
 
104
107
  ```js
105
- const half = asFraction('0.5') // '1/2';
106
- const numeric = fraction(half) // 0.5;
107
- fraction('13/0') // Infinity;
108
- fraction('Garbage') // NaN;
109
- numFraction('Garbage') // 'Garbage';
108
+ const half = asFraction('0.5'); // '1/2';
109
+ const numeric = fraction(half); // 0.5;
110
+ fraction('13/0'); // Infinity;
111
+ fraction('Garbage'); // NaN;
112
+ numFraction('Garbage'); // 'Garbage';
110
113
  ```
111
114
 
112
115
  ## Input and conversions
113
- Several methods exist for processing GUI inputs and making sure they are valid numbers. Raw inputs from the user can be fed through [validateAndClean](#validateandcleanpreviousvalue-nexttext) method, which returns a new valid *number-unit* string.
116
+
117
+ Several methods exist for processing GUI inputs and making sure they are valid numbers. Raw inputs from the user can be fed through [validateAndClean](#validateandcleanpreviousvalue-nexttext) method, which returns a new valid _number-unit_ string.
114
118
 
115
119
  ```js
116
120
  validateAndClean('123-', '1234-'); // '1234';
@@ -145,6 +149,7 @@ isNumeric('e20'); // false
145
149
  ```
146
150
 
147
151
  ## Formatting
152
+
148
153
  For printing nice numbers in GUI and reports we offer different helpers. As already mentioned, one can get formatted unit labels, for example m³ for cubic meters, by calling [label](#labelunitkey). Calling [roundNumberWithLabel](#roundnumberwithlabelvalue-roundto--2) gives you a value, rounded to the wanted precision, with unit label. Use [round](#roundnumbernum-round--4) to round just the value. Depending on what value that is stored in the string it can sometimes be hard to know what precision to use for display. The method [getNumberOfDigitsToShow](#getnumberofdigitstoshownum-maxnumdigits--20) will analyse the input and suggest a reasonable precision for you.
149
154
 
150
155
  ```js
@@ -154,7 +159,8 @@ const outputValue = round(1e9, noOfDigits); // 0.0000000001
154
159
  ```
155
160
 
156
161
  ## Working with Tables
157
- Most of the methods in the library works with single *value-unit* strings and for tables we encourage you to store the *value-unit* string directly as keyed objects.
162
+
163
+ Most of the methods in the library works with single _value-unit_ strings and for tables we encourage you to store the _value-unit_ string directly as keyed objects.
158
164
  Example:
159
165
 
160
166
  ```js
@@ -164,7 +170,7 @@ const myTable = [
164
170
  ];
165
171
  ```
166
172
 
167
- That said, we also have support for a special custom table format where the first row specifies the **unit** and the following rows stores pure **numbers** *without unit*.
173
+ That said, we also have support for a special custom table format where the first row specifies the **unit** and the following rows stores pure **numbers** _without unit_.
168
174
 
169
175
  Example:
170
176
 
@@ -172,7 +178,7 @@ Example:
172
178
  const myExcelTable = [
173
179
  ['m', 'ft', 'in', 'kg/m3'],
174
180
  [15, 42, 35, 42],
175
- [16, 43, 36, 50]
181
+ [16, 43, 36, 50],
176
182
  ];
177
183
  ```
178
184
 
@@ -189,11 +195,11 @@ convertTable(['cm', 'ft', 'cm', 'sg'], myExcelTable);
189
195
  */
190
196
  ```
191
197
 
192
-
193
198
  ## Methods
194
199
 
195
200
  ### withUnit(value, unit, defaultVal = '') {...}
196
- #### Get a *value-unit* string, i.e. value with unit splitted by | separator
201
+
202
+ #### Get a _value-unit_ string, i.e. value with unit splitted by | separator
197
203
 
198
204
  ```js
199
205
  withUnit(1.123, 'm'); // '1.123|m'
@@ -201,12 +207,15 @@ withUnit(-10.314, 'K/100m'); // '-10.314|K/100m'
201
207
  ```
202
208
 
203
209
  ### unumWithUnit(numWithUnit, toUnit, fromUnit?) {...}
204
- #### Converts to given *toUnit* and return the converted value with unit
210
+
211
+ #### Converts to given _toUnit_ and return the converted value with unit
212
+
205
213
  ```js
206
214
  unumWithUnit(2.2, 'kg/m3', 'sg'); // '2200|kg/m3'
207
215
  ```
208
216
 
209
217
  ### isEmptyValueWithUnit(val) {...}
218
+
210
219
  #### Checks if input is a string that starts with '|', e.g. '|m' or '|in'
211
220
 
212
221
  ```js
@@ -216,7 +225,9 @@ isEmptyValueWithUnit('m'); // false
216
225
  ```
217
226
 
218
227
  ### isValueWithUnit(value) {...}
228
+
219
229
  #### Takes user input and returns `true` if is value with unit and false in every other case
230
+
220
231
  ```js
221
232
  isValueWithUnit('m'); // false
222
233
  isValueWithUnit('5'); // false
@@ -225,9 +236,10 @@ isValueWithUnit('5|m'); // true
225
236
  ```
226
237
 
227
238
  ### isNumeric(val) {...}
239
+
228
240
  #### Check if provided argument is number
229
241
 
230
- ```js
242
+ ```js
231
243
  isNumeric(1e20); // true
232
244
  isNumeric('1e20'); // true
233
245
  isNumeric(NaN); // false
@@ -235,7 +247,9 @@ isNumeric(Infinity); // false
235
247
  ```
236
248
 
237
249
  ### allNumbers(arr) {...}
250
+
238
251
  #### Check array values if all are numbers
252
+
239
253
  ```js
240
254
  allNumbers([1, 2, 1.2, 5]); // true
241
255
  allNumbers([1, 2, '1.2', 5]); // false
@@ -243,6 +257,7 @@ allNumbers([1, 1, 2, Infinity, 1.2]); // true
243
257
  ```
244
258
 
245
259
  ### formatNumber(number) {...}
260
+
246
261
  #### Outputs "pretty" number (with thousands separators), taken from [this](https://www.wikitechy.com/tutorials/javascript/print-a-number-with-commas-as-thousands-separators-in-javascript)
247
262
 
248
263
  ```js
@@ -252,6 +267,7 @@ formatNumber('100000.123'); // '100,000.123';
252
267
  ```
253
268
 
254
269
  ### charCount(chr, str)
270
+
255
271
  #### Counts all occurence of given character
256
272
 
257
273
  ```js
@@ -261,6 +277,7 @@ charCount(0, '100000.123'); // 5;
261
277
  ```
262
278
 
263
279
  ### validateAndClean(previousValue, nextText) {...}
280
+
264
281
  #### Validates and cleans raw text numeric user input, typically from user input. The previous value is for optionally determining the pre-existing unit. The next text is a raw input string. The return value is reformatted from the next text (removing invalid patterns)
265
282
 
266
283
  ```js
@@ -272,6 +289,7 @@ validateAndClean('2|m', '2e-3'); // '2e-3|m'
272
289
  ```
273
290
 
274
291
  ### getNumberOfDigitsToShow(num, maxNumDigits = 20) {...}
292
+
275
293
  #### Calculates the number of digits to be rounded off, typically for values less than 1 E.g. when trying to format 1e-9 byroundNumber(), the output value will only show '0' if just rounded off with 4 digits fromroundNumber(val) Then it is more useful to calculate the number of digits to be rounded off, and pass this in, i.e.round(val, getNumberOfDigitsToShow(val)) which will return 0.0000000001.
276
294
 
277
295
  ```js
@@ -283,7 +301,9 @@ getNumberOfDigitsToShow('100000.123'); // 4
283
301
  getNumberOfDigitsToShow('100000.123', 3); // 3
284
302
  getNumberOfDigitsToShow(0); // 4
285
303
  ```
304
+
286
305
  ### round(num, round = 4)
306
+
287
307
  #### Formating / rounding number provided in argument
288
308
 
289
309
  ```js
@@ -291,11 +311,13 @@ round(1.11231231); // 1.1123
291
311
  round('100000.123'); // '100000.123'
292
312
  round('100000.123', 2); // '100000.12'
293
313
  round('100000.123|m', 2); // '100000.12|m'
294
- round(null, 2) // null
314
+ round(null, 2); // null
295
315
  ```
296
316
 
297
317
  ### roundNumberWithLabel(value, roundTo = 2) {...}
318
+
298
319
  #### Round input value and return with labeled unit
320
+
299
321
  ```js
300
322
  roundNumberWithLabel('1000.1284325|kg/m3'); // '1000.13 kg/m³'
301
323
  roundNumberWithLabel('-999.999991|kg/m3', 5); // '-999.99999 kg/m³'
@@ -303,7 +325,8 @@ roundNumberWithLabel('-999.999999|kg/m3', 5); // '-1000 kg/m³'
303
325
  ```
304
326
 
305
327
  ### fraction(str)
306
- #### Convert set fraction to decimal value will return either number in decimal format, Infinity if fraction is divided by 0
328
+
329
+ #### Convert set fraction to decimal value will return either number in decimal format, Infinity if fraction is divided by 0
307
330
 
308
331
  ```js
309
332
  fraction('1/3'); // 0.333
@@ -316,43 +339,54 @@ fraction([1, 2]); // NaN
316
339
  fraction(''); // NaN
317
340
  ```
318
341
 
319
- ### unitFromQuantity(quantity) {...}
342
+ ### unitFromQuantity(quantity) {...}
343
+
320
344
  #### Get base unit from given quantity
345
+
321
346
  ```js
322
347
  unitFromQuantity('force'); // 'N';
323
- unitFromQuantity('notsupported'); // undefined;
348
+ unitFromQuantity('notsupported'); // undefined;
324
349
  ```
325
350
 
326
351
  ### getAltUnitsListByQuantity(quantity) {...}
352
+
327
353
  #### Get list of alternative units, with labels, for a given quantity.
354
+
328
355
  ```js
329
356
  getAltUnitsListByQuantity('angles'); // [{unit: 'deg', label: '°'}, {unit: 'rad', label: 'rad'}];
330
357
  getAltUnitsListByQuantity('qwe123'); // undefined;
331
358
  ```
332
359
 
333
360
  ### getUnitsForQuantity(quantity) {...}
361
+
334
362
  #### Get list of units for a given quantity
363
+
335
364
  ```js
336
365
  getUnitsForQuantity('force'); // ['tonnes', 'lbf', 'kgf', 'N', 'kN', 'tonneForce', 'klbf']
337
366
  getUnitsForQuantity('depth'); // ['m', 'ft']
338
367
  getUnitsForQuantity('acceleration'); // ['ft/s2', 'm/s2'])
339
- getUnitsForQuantity('angles');// ['deg', 'rad']);
368
+ getUnitsForQuantity('angles'); // ['deg', 'rad']);
340
369
  getUnitsForQuantity('dls'); // ['deg/10m', 'deg/30m', 'deg/100ft'])
341
370
  getUnitsForQuantity('shit'); // undefined
342
371
  ```
343
372
 
344
373
  ### toBase(value, quantity) {...}
374
+
345
375
  #### Convert value to the base unit given by the quantity
376
+
346
377
  ```js
347
378
  toBase('1|m', 'length'); // 1
348
379
  toBase('1|cm', 'length'); // 0.01
349
380
  toBase('1|tonnes', 'weight'); // 1000
350
381
  ```
382
+
351
383
  ### altUnitsList(value, quantity, defaultUnit?) {...}
384
+
352
385
  #### Get list of values, with same precision as the given value, in all the units of the given quantity
386
+
353
387
  ```js
354
388
  altUnitsList('10|m', 'length');
355
- /* [['10', 'm', 'm'],
389
+ /* [['10', 'm', 'm'],
356
390
  ['32.8', 'ft', 'ft'],
357
391
  ['0.01', 'km', 'km'],
358
392
  ['394', 'in', 'in'],
@@ -364,13 +398,17 @@ altUnitsList('180', 'deg');
364
398
  ['3.14', 'rad', 'rad']]
365
399
  */
366
400
  ```
401
+
367
402
  ### convertTable(toUnitRow, table, defaultUnitRow?, removeFinalUnitsRow=false) {...}
403
+
368
404
  #### Convert table of values to another unit
405
+
369
406
  ```js
370
407
  const table = [
371
408
  ['m', 'ft', 'in', 'kg/m3'],
372
409
  [15, 42, 35, 42],
373
- [16, 43, 36, 50]];
410
+ [16, 43, 36, 50],
411
+ ];
374
412
  convertTable(['cm', 'ft', 'cm', 'sg'], table);
375
413
  /*
376
414
  [
@@ -381,8 +419,8 @@ convertTable(['cm', 'ft', 'cm', 'sg'], table);
381
419
  */
382
420
  ```
383
421
 
384
-
385
422
  ### getQuantities() {...}
423
+
386
424
  #### Get list of all defined quantities
387
425
 
388
426
  ```js
@@ -390,6 +428,7 @@ getQuantities(); // ['acceleration', 'angleGradient', 'angles', 'areaOther', ...
390
428
  ```
391
429
 
392
430
  ### checkAndCleanDecimalComma(val) {...}
431
+
393
432
  #### Find double dot and comma in value and replace it with decimal dot. For example: 123,4 => 123.4 or 123..4 => 123,4
394
433
 
395
434
  ```js
@@ -399,6 +438,7 @@ checkAndCleanDecimalComma('36..6'); // '36.6';
399
438
  ```
400
439
 
401
440
  ### to(value, fromUnit, toUnit) {...}
441
+
402
442
  #### Convert value to another unit
403
443
 
404
444
  ```js
@@ -416,14 +456,18 @@ to('1,,.12', 'rad', 'deg').toFixed(4); // '64.1713'
416
456
  ```
417
457
 
418
458
  ### split(numWithUnit) {...}
459
+
419
460
  #### Split string into value and unit.
461
+
420
462
  ```js
421
- split('-12,2m'); // ['-12.2', 'm'];
422
- split('-12 1/2m'); // ['-12 1/2', 'm'];
463
+ split('-12,2m'); // ['-12.2', 'm'];
464
+ split('-12 1/2m'); // ['-12 1/2', 'm'];
423
465
  ```
424
466
 
425
467
  ### getValue(numWithUnit) {...}
468
+
426
469
  #### Get unit of the number with unit string ("1|m") will return "m"
470
+
427
471
  ```js
428
472
  getValue('12.2'); // '12.2'
429
473
  getValue('12.2m'); // '12.2'
@@ -433,7 +477,9 @@ getValue('m'); // '';
433
477
  ```
434
478
 
435
479
  ### getUnit(numWithUnit) {...}
480
+
436
481
  #### Get unit of the number with unit string
482
+
437
483
  ```js
438
484
  getUnit('-2|in2'); // 'in2'
439
485
  getUnit('12.2|m'); // 'm'
@@ -442,19 +488,22 @@ getUnit('|m'); // 'm';
442
488
  getUnit('12.2'); // '';
443
489
  ```
444
490
 
445
-
446
491
  ### label(unitKey) {...}
492
+
447
493
  #### Returns a print friendly unit representation
494
+
448
495
  ```js
449
- label('m3'); // 'm³'
496
+ label('m3'); // 'm³'
450
497
  label('1/bar'); // 'bar⁻¹'
451
498
  ```
452
499
 
453
500
  ### convertAndGetValue(numWithUnit, toUnit, fromUnit?) {...}
501
+
454
502
  #### Convert value with unit to another unit Will try to pick `fromUnit` from `numWithUnit` if it was not provided
503
+
455
504
  ```js
456
505
  convertAndGetValue('1 1/2', 'in', 'in'); // 1.5
457
- convertAndGetValue('1 1/2 in', 'in'); // 1.5
506
+ convertAndGetValue('1 1/2 in', 'in'); // 1.5
458
507
  convertAndGetValue('-1 1/2 in', 'in'); // -1.5
459
508
  convertAndGetValue(2.2, 'notsupported', 'notsupported'); // 2.2
460
509
  convertAndGetValue(2.2, 'kg/m3', 'sg'); // 2200
@@ -462,7 +511,9 @@ convertAndGetValue('2.2', 'kg/m3', 'sg'); // 2200
462
511
  ```
463
512
 
464
513
  ### convertSamePrecision(numWithUnit, toUnit, digits?) {...}
514
+
465
515
  #### Convert value with unit to another unit and display it in pretty format. It will preserv the number of digits in the input or alternativly converting to the given number of digits.
516
+
466
517
  ```js
467
518
  convertSamePrecision('1|in', 'cm', 8); // '2.54|cm'
468
519
  convertSamePrecision('102e-6|in', 'cm'); // '0.000259|cm'
@@ -471,14 +522,17 @@ convertSamePrecision('10.000|m', 'in'); // '393.7|in'
471
522
  ```
472
523
 
473
524
  ### asFraction(str) {...}
525
+
474
526
  #### Converts decimal number to fractional format return string with fractional format of set value
527
+
475
528
  ```js
476
529
  asFraction(''); // '0'
477
530
  asFraction('0.1'); // '1/10'
478
531
  ```
479
532
 
480
533
  ### numFraction(str) {...}
481
- #### Convert fraction string to number (return input value if conversion fails) For historical reasons, numFraction returns the string value unmodified if it is not able to convert to a number. This is useful where user inputs are filtered through calls to numFraction. For "detecting" when numFraction fails, check if the return value is a string or a number. If it is a string it means number conversion failed. will return string with decimal format of fraction
534
+
535
+ #### Convert fraction string to number (return input value if conversion fails) For historical reasons, numFraction returns the string value unmodified if it is not able to convert to a number. This is useful where user inputs are filtered through calls to numFraction. For "detecting" when numFraction fails, check if the return value is a string or a number. If it is a string it means number conversion failed. will return string with decimal format of fraction
482
536
 
483
537
  ```js
484
538
  numFraction(''); // ''
@@ -486,9 +540,10 @@ numFraction('1/10'); // 0.1
486
540
  numFraction(null); // null
487
541
  ```
488
542
 
489
-
490
543
  ### cleanNumStr(str){...}
544
+
491
545
  #### Cleaning up and fixing provided number to correct numerical format removing redundant '.' dots, ',' commas, spaces
546
+
492
547
  ```js
493
548
  cleanNumStr('1000,000.1'); // '1000000.1'
494
549
  cleanNumStr('1000,000,000'); // '1000000000'
@@ -497,6 +552,7 @@ cleanNumStr('1000,000,000.1.1'); // '100000000011'
497
552
  ```
498
553
 
499
554
  ### cleanNum(str): {...}
555
+
500
556
  #### Cleaning and fixing numerical string but returns it as number
501
557
 
502
558
  ```js
@@ -505,70 +561,90 @@ cleanNum(',1'); // 0.1
505
561
  ```
506
562
 
507
563
  ### toNum(input, defaultValue?, minimum?) {...}
564
+
508
565
  #### Convert provided argument to number or return it if impossible to convert
566
+
509
567
  ```js
510
568
  toNum(1); // 1
511
569
  toNum(',1'); // 0.1
512
570
  ```
513
571
 
514
572
  ### isNonNumerical(value)
573
+
515
574
  #### Check if value is non numerical
516
575
 
517
576
  ```js
518
- isNonNumerical('123.32asdasds4') // true
519
- isNonNumerical('123.32') // false
577
+ isNonNumerical('123.32asdasds4'); // true
578
+ isNonNumerical('123.32'); // false
520
579
  ```
521
580
 
522
581
  ## Constants
582
+
523
583
  #### LABELS
584
+
524
585
  ##### Units labels
586
+
525
587
  ```js
526
- LABELS.cm // 'cm'
527
- LABELS.lps // 'L/s'
588
+ LABELS.cm; // 'cm'
589
+ LABELS.lps; // 'L/s'
528
590
  ```
529
591
 
530
592
  #### ALT_UNITS
593
+
531
594
  ##### Alternative units grouped by quantity
595
+
532
596
  ```js
533
- ALT_UNITS.angles // ['deg', 'rad']
534
- ALT_UNITS.density // ['sg', 'ppg', 'kg/m3', 'lbm/ft3', 'g/cm3', 'lb/ft3']
597
+ ALT_UNITS.angles; // ['deg', 'rad']
598
+ ALT_UNITS.density; // ['sg', 'ppg', 'kg/m3', 'lbm/ft3', 'g/cm3', 'lb/ft3']
535
599
  ```
536
600
 
537
601
  #### UNIT_FROM_KEY
602
+
538
603
  ##### Units list
604
+
539
605
  ```js
540
- UNIT_FROM_KEY.length // 'm'
541
- UNIT_FROM_KEY.latitude // '°N'
606
+ UNIT_FROM_KEY.length; // 'm'
607
+ UNIT_FROM_KEY.latitude; // '°N'
542
608
  ```
543
609
 
544
610
  #### KNOWN_CONVERSIONS
611
+
545
612
  ##### Conversions list where each key 'from unit|to unit' pair
613
+
546
614
  ```js
547
- KNOWN_CONVERSIONS['m|mm'](1) // 1000
615
+ KNOWN_CONVERSIONS['m|mm'](1); // 1000
548
616
  ```
549
617
 
550
618
  #### DEPRECATED_UNITS
619
+
551
620
  ##### List of deprecated units
621
+
552
622
  ```js
553
- DEPRECATED_UNITS['N-m'] // 'Nm'
554
- DEPRECATED_UNITS['ft-lbf'] // 'ftlbf'
623
+ DEPRECATED_UNITS['N-m']; // 'Nm'
624
+ DEPRECATED_UNITS['ft-lbf']; // 'ftlbf'
555
625
  ```
556
626
 
557
627
  #### UNIT_ALIASES
628
+
558
629
  ##### This list is mapping from legal alternative unit names to our selected unit name
630
+
559
631
  ```js
560
- UNIT_ALIASES['lbs/ft'] // 'lb/ft'
632
+ UNIT_ALIASES['lbs/ft']; // 'lb/ft'
561
633
  ```
562
634
 
563
635
  #### INTERMEDIATE_CONVERSIONS
636
+
564
637
  ##### Intermediate conversions
638
+
565
639
  ```js
566
- INTERMEDIATE_CONVERSIONS.mm // 'm'
567
- INTERMEDIATE_CONVERSIONS.t // 'kg'
640
+ INTERMEDIATE_CONVERSIONS.mm; // 'm'
641
+ INTERMEDIATE_CONVERSIONS.t; // 'kg'
568
642
  ```
569
643
 
570
644
  #### SPECIAL_NUMBERS_STRING
645
+
571
646
  ##### Special numbers in string format
647
+
572
648
  ```js
573
- SPECIAL_NUMBERS_STRING // ['NaN', '-Infinity', 'Infinity']
649
+ SPECIAL_NUMBERS_STRING; // ['NaN', '-Infinity', 'Infinity']
574
650
  ```
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@oliasoft-open-source/units",
3
- "version": "5.2.0",
3
+ "version": "5.3.0",
4
4
  "description": "Package for units management",
5
5
  "keywords": [],
6
6
  "license": "MIT",
7
7
  "author": "Oliasoft and contributors",
8
+ "files": [
9
+ "dist/**/*"
10
+ ],
11
+ "type": "module",
8
12
  "main": "./dist/index.cjs",
9
13
  "module": "./dist/index.js",
10
14
  "types": "dist/index.d.ts",
@@ -87,33 +91,10 @@
87
91
  "default": "./dist/interfaces.js"
88
92
  }
89
93
  },
90
- "files": [
91
- "dist/**/*"
92
- ],
93
- "type": "module",
94
- "devEngines": {
95
- "packageManager": {
96
- "name": "pnpm",
97
- "version": "11.3.0",
98
- "onFail": "download"
99
- },
100
- "runtime": {
101
- "name": "node",
102
- "version": "26.3.0",
103
- "onFail": "download"
104
- }
105
- },
106
- "lint-staged": {
107
- "*/**/*.{ts,js,json}": [
108
- "oxlint --fix",
109
- "prettier --write"
110
- ]
111
- },
112
94
  "dependencies": {
113
95
  "fraction.js": "^5.3.4"
114
96
  },
115
97
  "devDependencies": {
116
- "@prettier/plugin-oxc": "^0.1.3",
117
98
  "@types/node": "^22.19.12",
118
99
  "ajv": "^8.18.0",
119
100
  "ajv-errors": "^3.0.0",
@@ -121,26 +102,44 @@
121
102
  "expect": "29.7.0",
122
103
  "husky": "^9.1.7",
123
104
  "lint-staged": "^15.5.2",
105
+ "oxfmt": "^0.61.0",
124
106
  "oxlint": "^1.50.0",
125
- "prettier": "^3.8.1",
126
107
  "publint": "^0.3.18",
127
108
  "tsdown": "^0.21.8",
128
109
  "tsx": "^4.21.0",
129
110
  "typescript": "^5.9.3",
130
111
  "node": "runtime:26.3.0"
131
112
  },
113
+ "lint-staged": {
114
+ "*": [
115
+ "oxlint --fix",
116
+ "oxfmt"
117
+ ]
118
+ },
119
+ "devEngines": {
120
+ "packageManager": {
121
+ "name": "pnpm",
122
+ "version": "11.3.0",
123
+ "onFail": "download"
124
+ },
125
+ "runtime": {
126
+ "name": "node",
127
+ "version": "26.3.0",
128
+ "onFail": "download"
129
+ }
130
+ },
132
131
  "prepack": "pnpm build",
133
132
  "scripts": {
134
133
  "build": "pnpm compile-validators && pnpm run typecheck:build && pnpm run typecheck:scripts && tsdown --config tsdown.config.ts && cp README.md dist/",
135
134
  "lint:check": "oxlint .",
136
135
  "lint:fix": "oxlint --fix .",
137
- "formatting:check": "prettier --check '*/**/*.{ts,js,json}'",
138
- "formatting:fix": "prettier --write '*/**/*.{ts,js,json}'",
136
+ "format:check": "oxfmt --check",
137
+ "format:fix": "oxfmt",
139
138
  "start": "pnpm link && pnpm compile-validators && tsx watch src/index.ts",
140
139
  "typecheck": "tsc -p tsconfig.test.json --noEmit",
141
140
  "typecheck:scripts": "tsc -p tsconfig.scripts.json --noEmit",
142
141
  "typecheck:build": "tsc -p tsconfig.build.json --noEmit",
143
- "test": "pnpm formatting:check && pnpm lint:check && pnpm compile-validators && pnpm run typecheck && pnpm run typecheck:scripts && node --import tsx --import ./test/setup.ts --test --experimental-test-coverage 'src/**/*.test.ts' 'src/**/__tests__/*.ts'",
142
+ "test": "pnpm format:check && pnpm compile-validators && pnpm run typecheck && pnpm run typecheck:scripts && node --import tsx --import ./test/setup.ts --test --experimental-test-coverage 'src/**/*.test.ts' 'src/**/__tests__/*.ts'",
144
143
  "test:watch": "node --watch --import tsx --import ./test/setup.ts --test 'src/**/*.test.ts' 'src/**/__tests__/*.ts'",
145
144
  "watch": "tsc -w -p tsconfig.dev.json",
146
145
  "validate-release-notes": "node --import tsx scripts/validate-release-notes.ts",