@oliasoft-open-source/units 4.2.5 → 4.3.0-beta-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.
Files changed (78) hide show
  1. package/dist/README.md +570 -0
  2. package/dist/index.d.ts +399 -8
  3. package/dist/index.js +2514 -79
  4. package/package.json +33 -34
  5. package/dist/comparison/comparison.d.ts +0 -15
  6. package/dist/comparison/comparison.js +0 -120
  7. package/dist/comparison/comparison.js.map +0 -1
  8. package/dist/comparison/comparison.test.d.ts +0 -1
  9. package/dist/comparison/comparison.test.js +0 -261
  10. package/dist/comparison/comparison.test.js.map +0 -1
  11. package/dist/constants.d.ts +0 -1218
  12. package/dist/constants.js +0 -1305
  13. package/dist/constants.js.map +0 -1
  14. package/dist/index.js.map +0 -1
  15. package/dist/interfaces.d.ts +0 -13
  16. package/dist/interfaces.js +0 -3
  17. package/dist/interfaces.js.map +0 -1
  18. package/dist/internationalization/internationalization.d.ts +0 -4
  19. package/dist/internationalization/internationalization.js +0 -64
  20. package/dist/internationalization/internationalization.js.map +0 -1
  21. package/dist/internationalization/internationalization.test.d.ts +0 -1
  22. package/dist/internationalization/internationalization.test.js +0 -65
  23. package/dist/internationalization/internationalization.test.js.map +0 -1
  24. package/dist/internationalization/utils.d.ts +0 -5
  25. package/dist/internationalization/utils.js +0 -114
  26. package/dist/internationalization/utils.js.map +0 -1
  27. package/dist/numbers/display-number.d.ts +0 -20
  28. package/dist/numbers/display-number.js +0 -141
  29. package/dist/numbers/display-number.js.map +0 -1
  30. package/dist/numbers/display-number.test.d.ts +0 -1
  31. package/dist/numbers/display-number.test.js +0 -182
  32. package/dist/numbers/display-number.test.js.map +0 -1
  33. package/dist/numbers/numbers.d.ts +0 -4
  34. package/dist/numbers/numbers.js +0 -94
  35. package/dist/numbers/numbers.js.map +0 -1
  36. package/dist/numbers/numbers.test.d.ts +0 -1
  37. package/dist/numbers/numbers.test.js +0 -167
  38. package/dist/numbers/numbers.test.js.map +0 -1
  39. package/dist/parse/parse-number.d.ts +0 -13
  40. package/dist/parse/parse-number.js +0 -49
  41. package/dist/parse/parse-number.js.map +0 -1
  42. package/dist/parse/parse-number.test.d.ts +0 -1
  43. package/dist/parse/parse-number.test.js +0 -86
  44. package/dist/parse/parse-number.test.js.map +0 -1
  45. package/dist/rounding/deprecated-rounding.d.ts +0 -4
  46. package/dist/rounding/deprecated-rounding.js +0 -80
  47. package/dist/rounding/deprecated-rounding.js.map +0 -1
  48. package/dist/rounding/rounding.d.ts +0 -13
  49. package/dist/rounding/rounding.js +0 -199
  50. package/dist/rounding/rounding.js.map +0 -1
  51. package/dist/rounding/rounding.test.d.ts +0 -1
  52. package/dist/rounding/rounding.test.js +0 -543
  53. package/dist/rounding/rounding.test.js.map +0 -1
  54. package/dist/tsconfig.build.tsbuildinfo +0 -1
  55. package/dist/units.d.ts +0 -29
  56. package/dist/units.js +0 -365
  57. package/dist/units.js.map +0 -1
  58. package/dist/utils.d.ts +0 -27
  59. package/dist/utils.js +0 -280
  60. package/dist/utils.js.map +0 -1
  61. package/dist/validate/ajv-validators.d.ts +0 -6
  62. package/dist/validate/ajv-validators.js +0 -127
  63. package/dist/validate/ajv-validators.js.map +0 -1
  64. package/dist/validate/errors-transform.d.ts +0 -2
  65. package/dist/validate/errors-transform.js +0 -8
  66. package/dist/validate/errors-transform.js.map +0 -1
  67. package/dist/validate/schemas/index.d.ts +0 -2
  68. package/dist/validate/schemas/index.js +0 -11
  69. package/dist/validate/schemas/index.js.map +0 -1
  70. package/dist/validate/schemas/test.schema.d.ts +0 -12
  71. package/dist/validate/schemas/test.schema.js +0 -15
  72. package/dist/validate/schemas/test.schema.js.map +0 -1
  73. package/dist/validate/schemas/validate-number.schema.d.ts +0 -5
  74. package/dist/validate/schemas/validate-number.schema.js +0 -8
  75. package/dist/validate/schemas/validate-number.schema.js.map +0 -1
  76. package/dist/validate/validate.test.d.ts +0 -1
  77. package/dist/validate/validate.test.js +0 -32
  78. package/dist/validate/validate.test.js.map +0 -1
package/dist/README.md ADDED
@@ -0,0 +1,570 @@
1
+ # Oliasoft Unit Handling
2
+
3
+ ##### Table of Contents
4
+ - [Introduction](#introduction)
5
+ - [Install](#install)
6
+ - [Basic usage](#basic-usage)
7
+ - [Input and conversions](#input-and-conversions)
8
+ - [Formatting](#formatting)
9
+ - [Working with Tables](#working-with-tables)
10
+ - [Methods](#methods)
11
+ - [Constants](#constants)
12
+
13
+ ## Introduction
14
+
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
+
17
+ All values with numbers are stored as a string with the following special format;
18
+
19
+ `'number|unit'`
20
+
21
+ Number here is very flexible and all of the following is valid "number" strings:
22
+
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.
31
+
32
+
33
+ ## Install
34
+
35
+
36
+ ```bash
37
+ npm i @oliasoft/units --save
38
+ ```
39
+
40
+ ## Basic usage
41
+
42
+ ### Adding units to a number
43
+ 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:
44
+
45
+ ```js
46
+ const myValueWithUnit = withUnit(3.14, 'cm'); // '3.14|cm'
47
+ ```
48
+
49
+ It's also possible to convert the number to a different unit before getting the string, by calling [unumWithUnit](#unumwithunitnumwithunit-string--number-tounit-string-fromunit-string). Given a string with unit you can use [getValue](#getvaluenumwithunit) and [getUnit](#getunitnumwithunit) to get the unit part, or use [split](#split-string-into-value-and-unit) to get both as an array.
50
+
51
+ ```js
52
+ const myValue = getValue(myValueWithUnit); // '3.14'
53
+ const myUnit = getUnit(myValueWithUnit); // 'cm'
54
+ const [value, unit] = split(myValueWithUnit); // ['3.14, 'cm']
55
+ ```
56
+
57
+ > Never manipulate the strings directly in case we ever change the syntax!
58
+
59
+ ### Converting number to calculation units
60
+ 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:
61
+
62
+ ```javascript
63
+ const value = convertAndGetValue(number, toUnit, fromUnit);
64
+ ```
65
+
66
+ The first argument is the number with or without unit. The second parameter is the target unit, while the last parameter is an optional from unit that's used as a fallback if the first parameter is given without unit.
67
+
68
+ Examples:
69
+
70
+ ```js
71
+ const value = convertAndGetValue('10|m', 'm'); // 10
72
+ value = convertAndGetValue('10|m', 'in'); // 393.7007874015748
73
+ value = convertAndGetValue('10', 'in', 'm'); // 393.7007874015748
74
+ ```
75
+
76
+ 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.
77
+
78
+ ```js
79
+ const convertedValue = to(10, 'm', 'in'); // 393.7007874015748
80
+ ```
81
+ same as
82
+
83
+ ```js
84
+ const convertedValue = to('10', 'm', 'in'); // 393.7007874015748
85
+ ```
86
+
87
+ ### Working with Quantities
88
+ **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).
89
+
90
+
91
+ ```js
92
+ getQuantities(); // ['acceleration', 'angleGradient', 'angles', 'areaOther', ...]
93
+ getUnitsForQuantity('angle'); // ['deg', 'rad']
94
+ unitFromQuantity('force') // 'N'
95
+ ```
96
+
97
+ ### Working with fractions
98
+ 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.
99
+
100
+ ```js
101
+ const half = asFraction('0.5') // '1/2';
102
+ const numeric = fraction(half) // 0.5;
103
+ fraction('13/0') // Infinity;
104
+ fraction('Garbage') // NaN;
105
+ numFraction('Garbage') // 'Garbage';
106
+ ```
107
+
108
+ ## Input and conversions
109
+ 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.
110
+
111
+ ```js
112
+ validateAndClean('123-', '1234-'); // '1234';
113
+ validateAndClean('123|m', '1234'); // '1234|m'
114
+ ```
115
+
116
+ Other similar method is [cleanNumStr](#cleannumstrstr) and [toNum](#tonuminput-defaultvalue-minimum) which works directly with numbers or numbers given as string. For directly returning the number use [cleanNum](#cleannumstr) which is identical to [clenNumStr] except for the return value.
117
+
118
+ ```js
119
+ cleanNumStr('1000,000,000.1.1'); // '100000000011'
120
+ toNum(',1'); // 0.1
121
+ cleanNumStr(',1')); // '0.1';
122
+ cleanNum(',1')); // 0.1
123
+ ```
124
+
125
+ For showing a list of all values in different units use [altUnitsList](#altunitslistvalue-quantity-defaultunit) which returns a list of the value in all units defined in the given quantity. In order to convert between units with similar precision one can use [convertSamePrecision](#convertsameprecisionnumwithunit-tounit-digits).
126
+
127
+ ```js
128
+ altUnitsList('180', 'deg'); // [['180', 'deg', '°'], ['3.14', 'rad', 'rad']]
129
+ ```
130
+
131
+ Given a value-unit string we offer a few methods for query the nature of it. Use [isEmptyValueWithUnit](#isemptyvaluewithunitval) to check if the unit is un-initialized (empty) and related use [isValueWithUnit](#isvaluewithunitvalue-stringnumber) to check if a string contains both a value and an "valid" unit. Valid here means an unit known to our repository. Use [isNumeric](#isnumericval) to check if a string represents a valid javascript number.
132
+
133
+ ```js
134
+ isEmptyValueWithUnit('10|m'); // false
135
+ isEmptyValueWithUnit('|m'); // true
136
+ isValueWithUnit('10|m'); // true
137
+ isValueWithUnit('10'); // false
138
+ isNumeric(1e20); //true
139
+ isNumeric('1e20'); //true
140
+ isNumeric('e20'); // false
141
+ ```
142
+
143
+ ## Formatting
144
+ 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.
145
+
146
+ ```js
147
+ const forPrint = roundNumberWithLabel('1000.1284325|kg/m3'); // '1000.13 kg/m³'
148
+ const noOfDigits = getNumberOfDigitsToShow(1e9); // 12
149
+ const outputValue = round(1e9, noOfDigits); // 0.0000000001
150
+ ```
151
+
152
+ ## Working with Tables
153
+ 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.
154
+ Example:
155
+
156
+ ```js
157
+ const myTable = [
158
+ { depth: '15|m', anotherDepth: '42|ft', od: '35|in', density: '42|kg/m3' },
159
+ { depth: '16|m', anotherDepth: '43|ft', od: '36|in', density: '43|kg/m3' },
160
+ ];
161
+ ```
162
+
163
+ 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*.
164
+
165
+ Example:
166
+
167
+ ```js
168
+ const myExcelTable = [
169
+ ['m', 'ft', 'in', 'kg/m3'],
170
+ [15, 42, 35, 42],
171
+ [16, 43, 36, 50]
172
+ ];
173
+ ```
174
+
175
+ This then represent the values 15 m, 42 ft, 35 in, 42 kg/m³, 16 m, 50 in, etc. This format is convenient for storing data imported/exported to i.e. Excel. With data in this shape you can call [convertTable](#convert-table-of-values-to-another-unit) to convert all values in the table to different units.
176
+
177
+ ```js
178
+ convertTable(['cm', 'ft', 'cm', 'sg'], myExcelTable);
179
+ /*
180
+ [
181
+ ['cm', 'ft', 'cm', 'sg'],
182
+ [1500, 42, 88.9, 0.042],
183
+ [1600, 43, 91.44, 0.05]
184
+ ]
185
+ */
186
+ ```
187
+
188
+
189
+ ## Methods
190
+
191
+ ### withUnit(value, unit, defaultVal = '') {...}
192
+ #### Get a *value-unit* string, i.e. value with unit splitted by | separator
193
+
194
+ ```js
195
+ withUnit(1.123, 'm'); // '1.123|m'
196
+ withUnit(-10.314, 'K/100m'); // '-10.314|K/100m'
197
+ ```
198
+
199
+ ### unumWithUnit(numWithUnit, toUnit, fromUnit?) {...}
200
+ #### Converts to given *toUnit* and return the converted value with unit
201
+ ```js
202
+ unumWithUnit(2.2, 'kg/m3', 'sg'); // '2200|kg/m3'
203
+ ```
204
+
205
+ ### isEmptyValueWithUnit(val) {...}
206
+ #### Checks if input is a string that starts with '|', e.g. '|m' or '|in'
207
+
208
+ ```js
209
+ isEmptyValueWithUnit('|m'); // true
210
+ isEmptyValueWithUnit(NaN); // false
211
+ isEmptyValueWithUnit('m'); // false
212
+ ```
213
+
214
+ ### isValueWithUnit(value) {...}
215
+ #### Takes user input and returns `true` if is value with unit and false in every other case
216
+ ```js
217
+ isValueWithUnit('m'); // false
218
+ isValueWithUnit('5'); // false
219
+ isValueWithUnit(5); // false
220
+ isValueWithUnit('5|m'); // true
221
+ ```
222
+
223
+ ### isNumeric(val) {...}
224
+ #### Check if provided argument is number
225
+
226
+ ```js
227
+ isNumeric(1e20); // true
228
+ isNumeric('1e20'); // true
229
+ isNumeric(NaN); // false
230
+ isNumeric(Infinity); // false
231
+ ```
232
+
233
+ ### allNumbers(arr) {...}
234
+ #### Check array values if all are numbers
235
+ ```js
236
+ allNumbers([1, 2, 1.2, 5]); // true
237
+ allNumbers([1, 2, '1.2', 5]); // false
238
+ allNumbers([1, 1, 2, Infinity, 1.2]); // true
239
+ ```
240
+
241
+ ### formatNumber(number) {...}
242
+ #### 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)
243
+
244
+ ```js
245
+ formatNumber(1.1); // '1.1'
246
+ formatNumber(100000.123); // '100,000.123';
247
+ formatNumber('100000.123'); // '100,000.123';
248
+ ```
249
+
250
+ ### charCount(chr, str)
251
+ #### Counts all occurence of given character
252
+
253
+ ```js
254
+ charCount('1', '1.1'); // 2;
255
+ charCount(1, '1.1'); // 2;
256
+ charCount(0, '100000.123'); // 5;
257
+ ```
258
+
259
+ ### validateAndClean(previousValue, nextText) {...}
260
+ #### 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)
261
+
262
+ ```js
263
+ validateAndClean('123', '1234'); // '1234'
264
+ validateAndClean('123e15', '123e154'); // '123e154'
265
+ validateAndClean('123.|m', '123.'); // '123.|m');
266
+ validateAndClean('-2e50|m', '-2e500'); // '-2e50|m'
267
+ validateAndClean('2|m', '2e-3'); // '2e-3|m'
268
+ ```
269
+
270
+ ### getNumberOfDigitsToShow(num, maxNumDigits = 20) {...}
271
+ #### 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.
272
+
273
+ ```js
274
+ getNumberOfDigitsToShow('1'); // 4
275
+ getNumberOfDigitsToShow(0.1); // 4
276
+ getNumberOfDigitsToShow(1.1); // 4
277
+ getNumberOfDigitsToShow(4.2e-8); // 11
278
+ getNumberOfDigitsToShow('100000.123'); // 4
279
+ getNumberOfDigitsToShow('100000.123', 3); // 3
280
+ getNumberOfDigitsToShow(0); // 4
281
+ ```
282
+ ### round(num, round = 4)
283
+ #### Formating / rounding number provided in argument
284
+
285
+ ```js
286
+ round(1.11231231); // 1.1123
287
+ round('100000.123'); // '100000.123'
288
+ round('100000.123', 2); // '100000.12'
289
+ round('100000.123|m', 2); // '100000.12|m'
290
+ round(null, 2) // null
291
+ ```
292
+
293
+ ### roundNumberWithLabel(value, roundTo = 2) {...}
294
+ #### Round input value and return with labeled unit
295
+ ```js
296
+ roundNumberWithLabel('1000.1284325|kg/m3'); // '1000.13 kg/m³'
297
+ roundNumberWithLabel('-999.999991|kg/m3', 5); // '-999.99999 kg/m³'
298
+ roundNumberWithLabel('-999.999999|kg/m3', 5); // '-1000 kg/m³'
299
+ ```
300
+
301
+ ### fraction(str)
302
+ #### Convert set fraction to decimal value will return either number in decimal format, Infinity if fraction is divided by 0
303
+
304
+ ```js
305
+ fraction('1/3'); // 0.333
306
+ fraction('1/10'); // 0.1
307
+ fraction('13/0'); // Infinity
308
+ fraction('13e2b'); // NaN
309
+ fraction(null); // NaN
310
+ fraction(undefined); // NaN
311
+ fraction([1, 2]); // NaN
312
+ fraction(''); // NaN
313
+ ```
314
+
315
+ ### unitFromQuantity(quantity) {...}
316
+ #### Get base unit from given quantity
317
+ ```js
318
+ unitFromQuantity('force'); // 'N';
319
+ unitFromQuantity('notsupported'); // undefined;
320
+ ```
321
+
322
+ ### getAltUnitsListByQuantity(quantity) {...}
323
+ #### Get list of alternative units, with labels, for a given quantity.
324
+ ```js
325
+ getAltUnitsListByQuantity('angles'); // [{unit: 'deg', label: '°'}, {unit: 'rad', label: 'rad'}];
326
+ getAltUnitsListByQuantity('qwe123'); // undefined;
327
+ ```
328
+
329
+ ### getUnitsForQuantity(quantity) {...}
330
+ #### Get list of units for a given quantity
331
+ ```js
332
+ getUnitsForQuantity('force'); // ['tonnes', 'lbf', 'kgf', 'N', 'kN', 'tonneForce', 'klbf']
333
+ getUnitsForQuantity('depth'); // ['m', 'ft']
334
+ getUnitsForQuantity('acceleration'); // ['ft/s2', 'm/s2'])
335
+ getUnitsForQuantity('angles');// ['deg', 'rad']);
336
+ getUnitsForQuantity('dls'); // ['deg/10m', 'deg/30m', 'deg/100ft'])
337
+ getUnitsForQuantity('shit'); // undefined
338
+ ```
339
+
340
+ ### toBase(value, quantity) {...}
341
+ #### Convert value to the base unit given by the quantity
342
+ ```js
343
+ toBase('1|m', 'length'); // 1
344
+ toBase('1|cm', 'length'); // 0.01
345
+ toBase('1|tonnes', 'weight'); // 1000
346
+ ```
347
+ ### altUnitsList(value, quantity, defaultUnit?) {...}
348
+ #### Get list of values, with same precision as the given value, in all the units of the given quantity
349
+ ```js
350
+ altUnitsList('10|m', 'length');
351
+ /* [['10', 'm', 'm'],
352
+ ['32.8', 'ft', 'ft'],
353
+ ['0.01', 'km', 'km'],
354
+ ['394', 'in', 'in'],
355
+ ['10000', 'mm', 'mm']]
356
+ */
357
+ altUnitsList('180', 'deg');
358
+ /*
359
+ [['180', 'deg', '°'],
360
+ ['3.14', 'rad', 'rad']]
361
+ */
362
+ ```
363
+ ### convertTable(toUnitRow, table, defaultUnitRow?, removeFinalUnitsRow=false) {...}
364
+ #### Convert table of values to another unit
365
+ ```js
366
+ const table = [
367
+ ['m', 'ft', 'in', 'kg/m3'],
368
+ [15, 42, 35, 42],
369
+ [16, 43, 36, 50]];
370
+ convertTable(['cm', 'ft', 'cm', 'sg'], table);
371
+ /*
372
+ [
373
+ ['cm', 'ft', 'cm', 'sg'],
374
+ [1500, 42, 88.9, 0.042],
375
+ [1600, 43, 91.44, 0.05]
376
+ ]
377
+ */
378
+ ```
379
+
380
+
381
+ ### getQuantities() {...}
382
+ #### Get list of all defined quantities
383
+
384
+ ```js
385
+ getQuantities(); // ['acceleration', 'angleGradient', 'angles', 'areaOther', ...]
386
+ ```
387
+
388
+ ### checkAndCleanDecimalComma(val) {...}
389
+ #### Find double dot and comma in value and replace it with decimal dot. For example: 123,4 => 123.4 or 123..4 => 123,4
390
+
391
+ ```js
392
+ checkAndCleanDecimalComma(123); // 123;
393
+ checkAndCleanDecimalComma('36,6'); // '36.6';
394
+ checkAndCleanDecimalComma('36..6'); // '36.6';
395
+ ```
396
+
397
+ ### to(value, fromUnit, toUnit) {...}
398
+ #### Convert value to another unit
399
+
400
+ ```js
401
+ to(1, 'm', 'ft'); // 1 / 0.3048
402
+ to(0, 'C', 'F'); // 32
403
+ to(32, 'F', 'C'); // 0
404
+ to(50, '%', 'fr'); // 0.5
405
+ to(0.125, 'fr', '%'); // 12.5
406
+ to(180, 'deg', 'rad').toFixed(4); // '3.1416'
407
+ to(1, 'rad', 'deg').toFixed(4); // '57.2958'
408
+ to('1,12', 'rad', 'deg').toFixed(4); // '64.1713'
409
+ to('1....12', 'rad', 'deg').toFixed(4); // '64.1713'
410
+ to('1,,12', 'rad', 'deg').toFixed(4); // '64.1713'
411
+ to('1,,.12', 'rad', 'deg').toFixed(4); // '64.1713'
412
+ ```
413
+
414
+ ### split(numWithUnit) {...}
415
+ #### Split string into value and unit.
416
+ ```js
417
+ split('-12,2m'); // ['-12.2', 'm'];
418
+ split('-12 1/2m'); // ['-12 1/2', 'm'];
419
+ ```
420
+
421
+ ### getValue(numWithUnit) {...}
422
+ #### Get unit of the number with unit string ("1|m") will return "m"
423
+ ```js
424
+ getValue('12.2'); // '12.2'
425
+ getValue('12.2m'); // '12.2'
426
+ getValue('12.2|m'); // '12.2'
427
+ getValue('|m'); // '';
428
+ getValue('m'); // '';
429
+ ```
430
+
431
+ ### getUnit(numWithUnit) {...}
432
+ #### Get unit of the number with unit string
433
+ ```js
434
+ getUnit('-2|in2'); // 'in2'
435
+ getUnit('12.2|m'); // 'm'
436
+ getUnit('12.2m'); // 'm';
437
+ getUnit('|m'); // 'm';
438
+ getUnit('12.2'); // '';
439
+ ```
440
+
441
+
442
+ ### label(unitKey) {...}
443
+ #### Returns a print friendly unit representation
444
+ ```js
445
+ label('m3'); // 'm³'
446
+ label('1/bar'); // 'bar⁻¹'
447
+ ```
448
+
449
+ ### convertAndGetValue(numWithUnit, toUnit, fromUnit?) {...}
450
+ #### Convert value with unit to another unit Will try to pick `fromUnit` from `numWithUnit` if it was not provided
451
+ ```js
452
+ convertAndGetValue('1 1/2', 'in', 'in'); // 1.5
453
+ convertAndGetValue('1 1/2 in', 'in'); // 1.5
454
+ convertAndGetValue('-1 1/2 in', 'in'); // -1.5
455
+ convertAndGetValue(2.2, 'notsupported', 'notsupported'); // 2.2
456
+ convertAndGetValue(2.2, 'kg/m3', 'sg'); // 2200
457
+ convertAndGetValue('2.2', 'kg/m3', 'sg'); // 2200
458
+ ```
459
+
460
+ ### convertSamePrecision(numWithUnit, toUnit, digits?) {...}
461
+ #### 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.
462
+ ```js
463
+ convertSamePrecision('1|in', 'cm', 8); // '2.54|cm'
464
+ convertSamePrecision('102e-6|in', 'cm'); // '0.000259|cm'
465
+ convertSamePrecision(2.54, 'cm', 1); // '3|cm'
466
+ convertSamePrecision('10.000|m', 'in'); // '393.7|in'
467
+ ```
468
+
469
+ ### asFraction(str) {...}
470
+ #### Converts decimal number to fractional format return string with fractional format of set value
471
+ ```js
472
+ asFraction(''); // '0'
473
+ asFraction('0.1'); // '1/10'
474
+ ```
475
+
476
+ ### numFraction(str) {...}
477
+ #### 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
478
+
479
+ ```js
480
+ numFraction(''); // ''
481
+ numFraction('1/10'); // 0.1
482
+ numFraction(null); // null
483
+ ```
484
+
485
+
486
+ ### cleanNumStr(str){...}
487
+ #### Cleaning up and fixing provided number to correct numerical format removing redundant '.' dots, ',' commas, spaces
488
+ ```js
489
+ cleanNumStr('1000,000.1'); // '1000000.1'
490
+ cleanNumStr('1000,000,000'); // '1000000000'
491
+ cleanNumStr('1/10'); // '1/10'
492
+ cleanNumStr('1000,000,000.1.1'); // '100000000011'
493
+ ```
494
+
495
+ ### cleanNum(str): {...}
496
+ #### Cleaning and fixing numerical string but returns it as number
497
+
498
+ ```js
499
+ cleanNum(1); // 1
500
+ cleanNum(',1'); // 0.1
501
+ ```
502
+
503
+ ### toNum(input, defaultValue?, minimum?) {...}
504
+ #### Convert provided argument to number or return it if impossible to convert
505
+ ```js
506
+ toNum(1); // 1
507
+ toNum(',1'); // 0.1
508
+ ```
509
+
510
+ ### isNonNumerical(value)
511
+ #### Check if value is non numerical
512
+
513
+ ```js
514
+ isNonNumerical('123.32asdasds4') // true
515
+ isNonNumerical('123.32') // false
516
+ ```
517
+
518
+ ## Constants
519
+ #### LABELS
520
+ ##### Units labels
521
+ ```js
522
+ LABELS.cm // 'cm'
523
+ LABELS.lps // 'L/s'
524
+ ```
525
+
526
+ #### ALT_UNITS
527
+ ##### Alternative units grouped by quantity
528
+ ```js
529
+ ALT_UNITS.angles // ['deg', 'rad']
530
+ ALT_UNITS.density // ['sg', 'ppg', 'kg/m3', 'lbm/ft3', 'g/cm3', 'lb/ft3']
531
+ ```
532
+
533
+ #### UNIT_FROM_KEY
534
+ ##### Units list
535
+ ```js
536
+ UNIT_FROM_KEY.length // 'm'
537
+ UNIT_FROM_KEY.latitude // '°N'
538
+ ```
539
+
540
+ #### KNOWN_CONVERSIONS
541
+ ##### Conversions list where each key 'from unit|to unit' pair
542
+ ```js
543
+ KNOWN_CONVERSIONS['m|mm'](1) // 1000
544
+ ```
545
+
546
+ #### DEPRECATED_UNITS
547
+ ##### List of deprecated units
548
+ ```js
549
+ DEPRECATED_UNITS['N-m'] // 'Nm'
550
+ DEPRECATED_UNITS['ft-lbf'] // 'ftlbf'
551
+ ```
552
+
553
+ #### UNIT_ALIASES
554
+ ##### This list is mapping from legal alternative unit names to our selected unit name
555
+ ```js
556
+ UNIT_ALIASES['lbs/ft'] // 'lb/ft'
557
+ ```
558
+
559
+ #### INTERMEDIATE_CONVERSIONS
560
+ ##### Intermediate conversions
561
+ ```js
562
+ INTERMEDIATE_CONVERSIONS.mm // 'm'
563
+ INTERMEDIATE_CONVERSIONS.t // 'kg'
564
+ ```
565
+
566
+ #### SPECIAL_NUMBERS_STRING
567
+ ##### Special numbers in string format
568
+ ```js
569
+ SPECIAL_NUMBERS_STRING // ['NaN', '-Infinity', 'Infinity']
570
+ ```