@scaleway/units 1.0.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.
- package/.turbo/turbo-build.log +23 -0
- package/CHANGELOG.md +7 -0
- package/LICENSE.md +21 -0
- package/README.md +164 -0
- package/dist/constants.d.ts +13 -0
- package/dist/constants.js +44 -0
- package/dist/convertDuration.d.ts +40 -0
- package/dist/convertDuration.js +53 -0
- package/dist/convertUnit.d.ts +85 -0
- package/dist/convertUnit.js +83 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/package.json +31 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
$ tsdown
|
|
2
|
+
[34mℹ[39m [34mtsdown v0.22.9[39m powered by [38;2;255;126;23mrolldown v1.2.0[39m
|
|
3
|
+
[34mℹ[39m config file: [4m/home/runner/work/scaleway-lib/scaleway-lib/packages/units/tsdown.config.ts[24m
|
|
4
|
+
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
5
|
+
[34mℹ[39m targets: [34mchrome111, edge111, firefox114, safari16.4, ios16.4[39m
|
|
6
|
+
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
7
|
+
[34mℹ[39m Build start
|
|
8
|
+
|
|
9
|
+
[43m WARN [49m TypeScript 7.0 does not yet have a stable API and is experimental. Some options will be unavailable.
|
|
10
|
+
|
|
11
|
+
[34mℹ[39m Emit types with typescript@7.0.2
|
|
12
|
+
[34mℹ[39m [2mdist/[22m[1mindex.js[22m [2m0.50 kB[22m [2m│ gzip: 0.17 kB[22m
|
|
13
|
+
[34mℹ[39m [2mdist/[22mconvertDuration.js [2m2.13 kB[22m [2m│ gzip: 0.78 kB[22m
|
|
14
|
+
[34mℹ[39m [2mdist/[22mconvertUnit.js [2m2.11 kB[22m [2m│ gzip: 0.95 kB[22m
|
|
15
|
+
[34mℹ[39m [2mdist/[22mconstants.js [2m1.04 kB[22m [2m│ gzip: 0.29 kB[22m
|
|
16
|
+
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.ts[22m[39m [2m0.49 kB[22m [2m│ gzip: 0.17 kB[22m
|
|
17
|
+
[34mℹ[39m [2mdist/[22m[32mconvertUnit.d.ts[39m [2m2.43 kB[22m [2m│ gzip: 0.95 kB[22m
|
|
18
|
+
[34mℹ[39m [2mdist/[22m[32mconvertDuration.d.ts[39m [2m1.46 kB[22m [2m│ gzip: 0.64 kB[22m
|
|
19
|
+
[34mℹ[39m [2mdist/[22m[32mconstants.d.ts[39m [2m0.59 kB[22m [2m│ gzip: 0.19 kB[22m
|
|
20
|
+
[34mℹ[39m 8 files, total: 10.75 kB
|
|
21
|
+
[32m✔[39m Build complete in [32m577ms[39m
|
|
22
|
+
[32m✔[39m [2m[attw][22m No problems found [2m(776ms)[22m
|
|
23
|
+
[32m✔[39m [2m[publint][22m No issues found [2m(809ms)[22m
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @scaleway/units
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- [#3511](https://github.com/scaleway/scaleway-lib/pull/3511) [`6df42a7`](https://github.com/scaleway/scaleway-lib/commit/6df42a7d27d3b83320e067d556048c50b1221385) Thanks [@chambo-e](https://github.com/chambo-e)! - introduce @scaleway/units
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Scaleway
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# `@scaleway/units`
|
|
2
|
+
|
|
3
|
+
A JS library to handle units and unit conversion
|
|
4
|
+
|
|
5
|
+
- [Install](#install)
|
|
6
|
+
- [`convertUnits`](#convertunits)
|
|
7
|
+
- [Usage](#usage)
|
|
8
|
+
- [Why two bases?](#why-two-bases)
|
|
9
|
+
- [Supported units](#supported-units)
|
|
10
|
+
- [`convertDuration`](#convertduration)
|
|
11
|
+
- [Usage](#usage-1)
|
|
12
|
+
- [How it works](#how-it-works)
|
|
13
|
+
- [Supported units](#supported-units-1)
|
|
14
|
+
- [⚠️ Months and years are approximations](#-months-and-years-are-approximations)
|
|
15
|
+
- [Constants](#constants)
|
|
16
|
+
- [API](#api)
|
|
17
|
+
- [`convertUnit(amount, options)`](#convertunitamount-options)
|
|
18
|
+
- [`convertDuration(amount, options)`](#convertdurationamount-options)
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
$ pnpm add @scaleway/units
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## `convertUnits`
|
|
27
|
+
|
|
28
|
+
A tiny utility for converting numeric values between unit prefixes (bytes ↔ kilobytes, bits ↔ megabits, grams ↔ kilograms, etc.), supporting both **SI\*- (decimal, base 10) and **IEC\*- (binary, base 2) prefixes.
|
|
29
|
+
|
|
30
|
+
The prefixes themselves are unit-agnostic — `convertUnit` doesn't know or care whether `amount` represents bytes, grams, or anything else. The `base` option (10 or 2) is what determines the actual multiplier, so IEC (binary) prefixes only make sense for quantities that are naturally measured in powers of 2, like data.
|
|
31
|
+
|
|
32
|
+
See [Binary prefix (Wikipedia)](https://en.wikipedia.org/wiki/Binary_prefix) for background on why these two systems exist and how they differ.
|
|
33
|
+
|
|
34
|
+
### Usage
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { convertUnit } from './convertUnit'
|
|
38
|
+
|
|
39
|
+
// 1500 bytes -> kilobytes (decimal / SI)
|
|
40
|
+
convertUnit(1500, { from: 'unit', to: 'kilo' })
|
|
41
|
+
// => 1.5
|
|
42
|
+
|
|
43
|
+
// 2 mebibytes -> bytes (binary / IEC)
|
|
44
|
+
convertUnit(2, { from: 'mega', to: 'unit', base: 2 })
|
|
45
|
+
// => 2097152
|
|
46
|
+
|
|
47
|
+
// 500 gigabytes -> terabytes (decimal / SI)
|
|
48
|
+
convertUnit(500, { from: 'giga', to: 'tera' })
|
|
49
|
+
// => 0.5
|
|
50
|
+
|
|
51
|
+
// 2.5 kilograms -> grams (decimal / SI)
|
|
52
|
+
// convertUnit isn't data-specific — it works for any SI quantity
|
|
53
|
+
convertUnit(2.5, { from: 'kilo', to: 'unit' })
|
|
54
|
+
// => 2500
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Why two bases?
|
|
58
|
+
|
|
59
|
+
| System | Base | Example |
|
|
60
|
+
| ------ | ---- | ---------------------------- |
|
|
61
|
+
| SI | 10 | 1 kilobyte (kB) = 1000 bytes |
|
|
62
|
+
| IEC | 2 | 1 kibibyte = 1024 bytes |
|
|
63
|
+
|
|
64
|
+
Storage vendors typically advertise capacity in SI (decimal) units, while operating systems often report size in IEC (binary) units — this is why a "500 GB" drive shows up as roughly 465 GB in your file explorer. `convertUnit` lets you convert correctly under either convention by passing `base: 10` or `base: 2`.
|
|
65
|
+
|
|
66
|
+
### Supported units
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
unit → kilo → mega → giga → tera → peta → exa → zetta → yotta
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`from` and `to` can be any two of these units, in either direction — the function computes the exponent difference between them for the selected base, so you're not limited to converting to/from `'unit'`.
|
|
73
|
+
|
|
74
|
+
## `convertDuration`
|
|
75
|
+
|
|
76
|
+
A tiny utility for converting numeric durations between time units — milliseconds, seconds, minutes, hours, days, weeks, months, and years.
|
|
77
|
+
|
|
78
|
+
### Usage
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { convertDuration } from './convertDuration'
|
|
82
|
+
|
|
83
|
+
// 90 seconds -> minutes
|
|
84
|
+
convertDuration(90, { from: 'seconds', to: 'minutes' })
|
|
85
|
+
// => 1.5
|
|
86
|
+
|
|
87
|
+
// 3 hours -> milliseconds
|
|
88
|
+
convertDuration(3, { from: 'hours', to: 'milliseconds' })
|
|
89
|
+
// => 10800000
|
|
90
|
+
|
|
91
|
+
// 2 weeks -> days
|
|
92
|
+
convertDuration(2, { from: 'weeks', to: 'days' })
|
|
93
|
+
// => 14
|
|
94
|
+
|
|
95
|
+
// 18 months -> years
|
|
96
|
+
convertDuration(18, { from: 'months', to: 'years' })
|
|
97
|
+
// => 1.5
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### How it works
|
|
101
|
+
|
|
102
|
+
All conversions go through milliseconds as an intermediate step: `amount` is converted from the `from` unit to milliseconds, then from milliseconds to the `to` unit.
|
|
103
|
+
|
|
104
|
+
### Supported units
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
milliseconds | seconds | minutes | hours | days | weeks | months | years
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### ⚠️ Months and years are approximations
|
|
111
|
+
|
|
112
|
+
This utility uses a \*_fixed 365-day year_- with no leap-year adjustment, and derives `months` from it:
|
|
113
|
+
|
|
114
|
+
- 1 year = 365 days
|
|
115
|
+
- 1 month = 365 / 12 ≈ 30.4167 days (\*_not_- a calendar month — every month is treated as the same length)
|
|
116
|
+
|
|
117
|
+
This means:
|
|
118
|
+
|
|
119
|
+
- Conversions involving `months` or `years` will drift from real calendar dates (e.g. they won't account for February being shorter, or leap years adding a day).
|
|
120
|
+
- `convertDuration` is best suited for approximate/relative durations (e.g. "roughly how many hours is 3 months"), not for calendar-accurate date arithmetic. For exact calendar math, use a date library (e.g. `date-fns`, `Luxon`, `Day.js`) instead.
|
|
121
|
+
|
|
122
|
+
## Constants
|
|
123
|
+
|
|
124
|
+
This package also exports some predefined constants:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
SECONDS_IN_MINUTE
|
|
128
|
+
SECONDS_IN_HOUR
|
|
129
|
+
SECONDS_IN_DAY
|
|
130
|
+
SECONDS_IN_MONTH
|
|
131
|
+
DAYS_IN_WEEK
|
|
132
|
+
DAYS_IN_MONTH
|
|
133
|
+
DAYS_IN_YEAR
|
|
134
|
+
HOURS_IN_DAY
|
|
135
|
+
HOURS_IN_MONTH
|
|
136
|
+
MONTHS_IN_YEAR
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## API
|
|
140
|
+
|
|
141
|
+
### `convertUnit(amount, options)`
|
|
142
|
+
|
|
143
|
+
Converts `amount` from one unit prefix to another.
|
|
144
|
+
|
|
145
|
+
| Parameter | Type | Description |
|
|
146
|
+
| -------------- | ----------------------------- | ---------------------------------------------------------- |
|
|
147
|
+
| `amount` | `number` | The value to convert. |
|
|
148
|
+
| `options.from` | `Unit` \*(default: `'unit'`)- | The prefix `amount` is currently expressed in. |
|
|
149
|
+
| `options.to` | `Unit` \*(default: `'unit'`)- | The prefix to convert `amount` into. |
|
|
150
|
+
| `options.base` | `2 \| 10` \*(default: `10`)- | `10` for SI/decimal prefixes, `2` for IEC/binary prefixes. |
|
|
151
|
+
|
|
152
|
+
Returns a `number`: `amount` expressed in the `to` unit.
|
|
153
|
+
|
|
154
|
+
### `convertDuration(amount, options)`
|
|
155
|
+
|
|
156
|
+
Converts `amount` from one time unit to another.
|
|
157
|
+
|
|
158
|
+
| Parameter | Type | Description |
|
|
159
|
+
| -------------- | -------------------------------- | -------------------------------------------- |
|
|
160
|
+
| `amount` | `number` | The duration to convert. |
|
|
161
|
+
| `options.from` | `Unit` \*(default: `'seconds'`)- | The unit `amount` is currently expressed in. |
|
|
162
|
+
| `options.to` | `Unit` \*(default: `'seconds'`)- | The unit to convert `amount` into. |
|
|
163
|
+
|
|
164
|
+
Returns a `number`: `amount` expressed in the `to` unit.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/constants.d.ts
|
|
2
|
+
declare const SECONDS_IN_MINUTE: number;
|
|
3
|
+
declare const SECONDS_IN_HOUR: number;
|
|
4
|
+
declare const SECONDS_IN_DAY: number;
|
|
5
|
+
declare const SECONDS_IN_MONTH: number;
|
|
6
|
+
declare const DAYS_IN_WEEK: number;
|
|
7
|
+
declare const DAYS_IN_MONTH: number;
|
|
8
|
+
declare const DAYS_IN_YEAR: number;
|
|
9
|
+
declare const HOURS_IN_DAY: number;
|
|
10
|
+
declare const HOURS_IN_MONTH: number;
|
|
11
|
+
declare const MONTHS_IN_YEAR: number;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { DAYS_IN_MONTH, DAYS_IN_WEEK, DAYS_IN_YEAR, HOURS_IN_DAY, HOURS_IN_MONTH, MONTHS_IN_YEAR, SECONDS_IN_DAY, SECONDS_IN_HOUR, SECONDS_IN_MINUTE, SECONDS_IN_MONTH };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { convertDuration } from "./convertDuration.js";
|
|
2
|
+
//#region src/constants.ts
|
|
3
|
+
const SECONDS_IN_MINUTE = convertDuration(1, {
|
|
4
|
+
from: "minutes",
|
|
5
|
+
to: "seconds"
|
|
6
|
+
});
|
|
7
|
+
const SECONDS_IN_HOUR = convertDuration(1, {
|
|
8
|
+
from: "hours",
|
|
9
|
+
to: "seconds"
|
|
10
|
+
});
|
|
11
|
+
const SECONDS_IN_DAY = convertDuration(1, {
|
|
12
|
+
from: "days",
|
|
13
|
+
to: "seconds"
|
|
14
|
+
});
|
|
15
|
+
const SECONDS_IN_MONTH = convertDuration(1, {
|
|
16
|
+
from: "months",
|
|
17
|
+
to: "seconds"
|
|
18
|
+
});
|
|
19
|
+
const DAYS_IN_WEEK = convertDuration(1, {
|
|
20
|
+
from: "weeks",
|
|
21
|
+
to: "days"
|
|
22
|
+
});
|
|
23
|
+
const DAYS_IN_MONTH = convertDuration(1, {
|
|
24
|
+
from: "months",
|
|
25
|
+
to: "days"
|
|
26
|
+
});
|
|
27
|
+
const DAYS_IN_YEAR = convertDuration(1, {
|
|
28
|
+
from: "years",
|
|
29
|
+
to: "days"
|
|
30
|
+
});
|
|
31
|
+
const HOURS_IN_DAY = convertDuration(1, {
|
|
32
|
+
from: "days",
|
|
33
|
+
to: "hours"
|
|
34
|
+
});
|
|
35
|
+
const HOURS_IN_MONTH = convertDuration(1, {
|
|
36
|
+
from: "months",
|
|
37
|
+
to: "hours"
|
|
38
|
+
});
|
|
39
|
+
const MONTHS_IN_YEAR = convertDuration(1, {
|
|
40
|
+
from: "years",
|
|
41
|
+
to: "months"
|
|
42
|
+
});
|
|
43
|
+
//#endregion
|
|
44
|
+
export { DAYS_IN_MONTH, DAYS_IN_WEEK, DAYS_IN_YEAR, HOURS_IN_DAY, HOURS_IN_MONTH, MONTHS_IN_YEAR, SECONDS_IN_DAY, SECONDS_IN_HOUR, SECONDS_IN_MINUTE, SECONDS_IN_MONTH };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region src/convertDuration.d.ts
|
|
2
|
+
type Unit = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years';
|
|
3
|
+
type ConvertUnitArgs = {
|
|
4
|
+
from?: Unit;
|
|
5
|
+
to?: Unit;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Converts a numeric duration from one time unit to another.
|
|
9
|
+
*
|
|
10
|
+
* `amount` is first converted to milliseconds using `from`, then converted
|
|
11
|
+
* to the target unit using `to`. `months` and `years` are approximations
|
|
12
|
+
* based on a 365-day year (no leap-year adjustment), so a "month" is
|
|
13
|
+
* `365 / 12 ≈ 30.42` days — not a calendar month.
|
|
14
|
+
*
|
|
15
|
+
* @param amount - The numeric duration to convert.
|
|
16
|
+
* @param options - Conversion options.
|
|
17
|
+
* @param options.from - The unit `amount` is currently expressed in. Defaults to `'seconds'`.
|
|
18
|
+
* @param options.to - The unit to convert `amount` into. Defaults to `'seconds'`.
|
|
19
|
+
*
|
|
20
|
+
* @returns `amount` expressed in the `to` unit.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // 90 seconds -> minutes
|
|
24
|
+
* convertDuration(90, { from: 'seconds', to: 'minutes' }) // 1.5
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // 3 hours -> milliseconds
|
|
28
|
+
* convertDuration(3, { from: 'hours', to: 'milliseconds' }) // 10800000
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // 2 weeks -> days
|
|
32
|
+
* convertDuration(2, { from: 'weeks', to: 'days' }) // 14
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // 18 months -> years (approximate, non-calendar months)
|
|
36
|
+
* convertDuration(18, { from: 'months', to: 'years' }) // 1.5
|
|
37
|
+
*/
|
|
38
|
+
declare const convertDuration: (amount: number, { from, to }: ConvertUnitArgs) => number;
|
|
39
|
+
//#endregion
|
|
40
|
+
export { convertDuration };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//#region src/convertDuration.ts
|
|
2
|
+
const MILLISECONDS_IN_SECOND = 1e3;
|
|
3
|
+
const SECONDS_IN_MINUTE = 60;
|
|
4
|
+
const MINUTES_IN_HOUR = 60;
|
|
5
|
+
const HOURS_IN_DAY = 24;
|
|
6
|
+
const MONTHS_IN_YEAR = 12;
|
|
7
|
+
const DAYS_IN_YEAR = 365;
|
|
8
|
+
const millisecondsInUnit = {
|
|
9
|
+
milliseconds: 1,
|
|
10
|
+
seconds: MILLISECONDS_IN_SECOND,
|
|
11
|
+
minutes: MILLISECONDS_IN_SECOND * SECONDS_IN_MINUTE,
|
|
12
|
+
hours: MILLISECONDS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR,
|
|
13
|
+
days: MILLISECONDS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY,
|
|
14
|
+
weeks: MILLISECONDS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY * 7,
|
|
15
|
+
months: MILLISECONDS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY * (DAYS_IN_YEAR / MONTHS_IN_YEAR * HOURS_IN_DAY / HOURS_IN_DAY),
|
|
16
|
+
years: MILLISECONDS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY * DAYS_IN_YEAR
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Converts a numeric duration from one time unit to another.
|
|
20
|
+
*
|
|
21
|
+
* `amount` is first converted to milliseconds using `from`, then converted
|
|
22
|
+
* to the target unit using `to`. `months` and `years` are approximations
|
|
23
|
+
* based on a 365-day year (no leap-year adjustment), so a "month" is
|
|
24
|
+
* `365 / 12 ≈ 30.42` days — not a calendar month.
|
|
25
|
+
*
|
|
26
|
+
* @param amount - The numeric duration to convert.
|
|
27
|
+
* @param options - Conversion options.
|
|
28
|
+
* @param options.from - The unit `amount` is currently expressed in. Defaults to `'seconds'`.
|
|
29
|
+
* @param options.to - The unit to convert `amount` into. Defaults to `'seconds'`.
|
|
30
|
+
*
|
|
31
|
+
* @returns `amount` expressed in the `to` unit.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* // 90 seconds -> minutes
|
|
35
|
+
* convertDuration(90, { from: 'seconds', to: 'minutes' }) // 1.5
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // 3 hours -> milliseconds
|
|
39
|
+
* convertDuration(3, { from: 'hours', to: 'milliseconds' }) // 10800000
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* // 2 weeks -> days
|
|
43
|
+
* convertDuration(2, { from: 'weeks', to: 'days' }) // 14
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* // 18 months -> years (approximate, non-calendar months)
|
|
47
|
+
* convertDuration(18, { from: 'months', to: 'years' }) // 1.5
|
|
48
|
+
*/
|
|
49
|
+
const convertDuration = (amount, { from = "seconds", to = "seconds" }) => {
|
|
50
|
+
return amount * millisecondsInUnit[from] / millisecondsInUnit[to];
|
|
51
|
+
};
|
|
52
|
+
//#endregion
|
|
53
|
+
export { convertDuration };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
//#region src/convertUnit.d.ts
|
|
2
|
+
declare const exponents: {
|
|
3
|
+
readonly unit: {
|
|
4
|
+
readonly si: 3;
|
|
5
|
+
readonly iec: 0;
|
|
6
|
+
};
|
|
7
|
+
readonly kilo: {
|
|
8
|
+
readonly si: 6;
|
|
9
|
+
readonly iec: 10;
|
|
10
|
+
};
|
|
11
|
+
readonly mega: {
|
|
12
|
+
readonly si: 9;
|
|
13
|
+
readonly iec: 20;
|
|
14
|
+
};
|
|
15
|
+
readonly giga: {
|
|
16
|
+
readonly si: 12;
|
|
17
|
+
readonly iec: 30;
|
|
18
|
+
};
|
|
19
|
+
readonly tera: {
|
|
20
|
+
readonly si: 15;
|
|
21
|
+
readonly iec: 40;
|
|
22
|
+
};
|
|
23
|
+
readonly peta: {
|
|
24
|
+
readonly si: 18;
|
|
25
|
+
readonly iec: 50;
|
|
26
|
+
};
|
|
27
|
+
readonly exa: {
|
|
28
|
+
readonly si: 21;
|
|
29
|
+
readonly iec: 60;
|
|
30
|
+
};
|
|
31
|
+
readonly zetta: {
|
|
32
|
+
readonly si: 24;
|
|
33
|
+
readonly iec: 70;
|
|
34
|
+
};
|
|
35
|
+
readonly yotta: {
|
|
36
|
+
readonly si: 27;
|
|
37
|
+
readonly iec: 80;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
type Unit = keyof typeof exponents;
|
|
41
|
+
type ConvertUnitArgs = {
|
|
42
|
+
from?: Unit;
|
|
43
|
+
to?: Unit;
|
|
44
|
+
base?: 2 | 10;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Converts a numeric amount from one unit prefix to another, using either
|
|
48
|
+
* SI (decimal, base 10) or IEC (binary, base 2) prefixes.
|
|
49
|
+
*
|
|
50
|
+
* The conversion is purely based on the exponent difference between `from`
|
|
51
|
+
* and `to` for the chosen base, so any pair of supported units can be
|
|
52
|
+
* converted directly in either direction (e.g. `mega` → `unit`, or
|
|
53
|
+
* `tera` → `giga`). This isn't limited to bytes/bits — any decimal-prefixed
|
|
54
|
+
* quantity (grams, meters, etc.) works the same way with `base: 10`.
|
|
55
|
+
*
|
|
56
|
+
* @see https://en.wikipedia.org/wiki/Binary_prefix
|
|
57
|
+
*
|
|
58
|
+
* @param amount - The numeric value to convert.
|
|
59
|
+
* @param options - Conversion options.
|
|
60
|
+
* @param options.from - Unit prefix of `amount`. Defaults to `'unit'` (no prefix).
|
|
61
|
+
* @param options.to - Unit prefix to convert `amount` into. Defaults to `'unit'` (no prefix).
|
|
62
|
+
* @param options.base - `10` for SI/decimal prefixes (1 kilo = 1000) or `2` for
|
|
63
|
+
* IEC/binary prefixes (1 kilo = 1024). Defaults to `10`.
|
|
64
|
+
*
|
|
65
|
+
* @returns `amount` expressed in the `to` unit.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // 1500 bytes -> kilobytes (decimal)
|
|
69
|
+
* convertUnit(1500, { from: 'unit', to: 'kilo' }) // 1.5
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* // 2 mebibytes -> bytes (binary)
|
|
73
|
+
* convertUnit(2, { from: 'mega', to: 'unit', base: 2 }) // 2097152
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* // 500 gigabytes -> terabytes (decimal)
|
|
77
|
+
* convertUnit(500, { from: 'giga', to: 'tera' }) // 0.5
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* // 2.5 kilograms -> grams (decimal) — works for any SI quantity, not just data
|
|
81
|
+
* convertUnit(2.5, { from: 'kilo', to: 'unit' }) // 2500
|
|
82
|
+
*/
|
|
83
|
+
declare const convertUnit: (amount: number, { from, to, base }: ConvertUnitArgs) => number;
|
|
84
|
+
//#endregion
|
|
85
|
+
export { convertUnit };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
//#region src/convertUnit.ts
|
|
2
|
+
const exponents = {
|
|
3
|
+
unit: {
|
|
4
|
+
si: 3,
|
|
5
|
+
iec: 0
|
|
6
|
+
},
|
|
7
|
+
kilo: {
|
|
8
|
+
si: 6,
|
|
9
|
+
iec: 10
|
|
10
|
+
},
|
|
11
|
+
mega: {
|
|
12
|
+
si: 9,
|
|
13
|
+
iec: 20
|
|
14
|
+
},
|
|
15
|
+
giga: {
|
|
16
|
+
si: 12,
|
|
17
|
+
iec: 30
|
|
18
|
+
},
|
|
19
|
+
tera: {
|
|
20
|
+
si: 15,
|
|
21
|
+
iec: 40
|
|
22
|
+
},
|
|
23
|
+
peta: {
|
|
24
|
+
si: 18,
|
|
25
|
+
iec: 50
|
|
26
|
+
},
|
|
27
|
+
exa: {
|
|
28
|
+
si: 21,
|
|
29
|
+
iec: 60
|
|
30
|
+
},
|
|
31
|
+
zetta: {
|
|
32
|
+
si: 24,
|
|
33
|
+
iec: 70
|
|
34
|
+
},
|
|
35
|
+
yotta: {
|
|
36
|
+
si: 27,
|
|
37
|
+
iec: 80
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Converts a numeric amount from one unit prefix to another, using either
|
|
42
|
+
* SI (decimal, base 10) or IEC (binary, base 2) prefixes.
|
|
43
|
+
*
|
|
44
|
+
* The conversion is purely based on the exponent difference between `from`
|
|
45
|
+
* and `to` for the chosen base, so any pair of supported units can be
|
|
46
|
+
* converted directly in either direction (e.g. `mega` → `unit`, or
|
|
47
|
+
* `tera` → `giga`). This isn't limited to bytes/bits — any decimal-prefixed
|
|
48
|
+
* quantity (grams, meters, etc.) works the same way with `base: 10`.
|
|
49
|
+
*
|
|
50
|
+
* @see https://en.wikipedia.org/wiki/Binary_prefix
|
|
51
|
+
*
|
|
52
|
+
* @param amount - The numeric value to convert.
|
|
53
|
+
* @param options - Conversion options.
|
|
54
|
+
* @param options.from - Unit prefix of `amount`. Defaults to `'unit'` (no prefix).
|
|
55
|
+
* @param options.to - Unit prefix to convert `amount` into. Defaults to `'unit'` (no prefix).
|
|
56
|
+
* @param options.base - `10` for SI/decimal prefixes (1 kilo = 1000) or `2` for
|
|
57
|
+
* IEC/binary prefixes (1 kilo = 1024). Defaults to `10`.
|
|
58
|
+
*
|
|
59
|
+
* @returns `amount` expressed in the `to` unit.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* // 1500 bytes -> kilobytes (decimal)
|
|
63
|
+
* convertUnit(1500, { from: 'unit', to: 'kilo' }) // 1.5
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* // 2 mebibytes -> bytes (binary)
|
|
67
|
+
* convertUnit(2, { from: 'mega', to: 'unit', base: 2 }) // 2097152
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* // 500 gigabytes -> terabytes (decimal)
|
|
71
|
+
* convertUnit(500, { from: 'giga', to: 'tera' }) // 0.5
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* // 2.5 kilograms -> grams (decimal) — works for any SI quantity, not just data
|
|
75
|
+
* convertUnit(2.5, { from: 'kilo', to: 'unit' }) // 2500
|
|
76
|
+
*/
|
|
77
|
+
const convertUnit = (amount, { from = "unit", to = "unit", base = 10 }) => {
|
|
78
|
+
const prefix = base === 10 ? "si" : "iec";
|
|
79
|
+
const power = exponents[from][prefix] - exponents[to][prefix];
|
|
80
|
+
return amount * Math.pow(base, power);
|
|
81
|
+
};
|
|
82
|
+
//#endregion
|
|
83
|
+
export { convertUnit };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { convertUnit } from "./convertUnit.js";
|
|
2
|
+
import { convertDuration } from "./convertDuration.js";
|
|
3
|
+
import { DAYS_IN_MONTH, DAYS_IN_WEEK, DAYS_IN_YEAR, HOURS_IN_DAY, HOURS_IN_MONTH, MONTHS_IN_YEAR, SECONDS_IN_DAY, SECONDS_IN_HOUR, SECONDS_IN_MINUTE, SECONDS_IN_MONTH } from "./constants.js";
|
|
4
|
+
export { DAYS_IN_MONTH, DAYS_IN_WEEK, DAYS_IN_YEAR, HOURS_IN_DAY, HOURS_IN_MONTH, MONTHS_IN_YEAR, SECONDS_IN_DAY, SECONDS_IN_HOUR, SECONDS_IN_MINUTE, SECONDS_IN_MONTH, convertDuration, convertUnit };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { convertUnit } from "./convertUnit.js";
|
|
2
|
+
import { convertDuration } from "./convertDuration.js";
|
|
3
|
+
import { DAYS_IN_MONTH, DAYS_IN_WEEK, DAYS_IN_YEAR, HOURS_IN_DAY, HOURS_IN_MONTH, MONTHS_IN_YEAR, SECONDS_IN_DAY, SECONDS_IN_HOUR, SECONDS_IN_MINUTE, SECONDS_IN_MONTH } from "./constants.js";
|
|
4
|
+
export { DAYS_IN_MONTH, DAYS_IN_WEEK, DAYS_IN_YEAR, HOURS_IN_DAY, HOURS_IN_MONTH, MONTHS_IN_YEAR, SECONDS_IN_DAY, SECONDS_IN_HOUR, SECONDS_IN_MINUTE, SECONDS_IN_MONTH, convertDuration, convertUnit };
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scaleway/units",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Library to handle units and unit conversion",
|
|
5
|
+
"homepage": "https://github.com/scaleway/scaleway-lib/tree/main/packages/units",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/scaleway/scaleway-lib/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/scaleway/scaleway-lib.git",
|
|
13
|
+
"directory": "packages/units"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./dist/index.js",
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"build": "tsdown",
|
|
28
|
+
"test:unit": "vitest --run",
|
|
29
|
+
"test:unit:coverage": "pnpm test:unit --coverage"
|
|
30
|
+
}
|
|
31
|
+
}
|