@sapphire/duration 1.1.0-next.e3fa75f.0 → 1.1.0-next.ef27abe.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/README.md +141 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -18,7 +18,10 @@
|
|
|
18
18
|
- [Features](#features)
|
|
19
19
|
- [Installation](#installation)
|
|
20
20
|
- [Usage](#usage)
|
|
21
|
-
- [
|
|
21
|
+
- [Human Readable Milliseconds](#human-readable-milliseconds)
|
|
22
|
+
- [Parsing a Duration](#parsing-a-duration)
|
|
23
|
+
- [Serializing a Duration](#serializing-a-duration)
|
|
24
|
+
- [Localizing with Durations](#localizing-with-durations)
|
|
22
25
|
- [Buy us some doughnuts](#buy-us-some-doughnuts)
|
|
23
26
|
- [Contributors ✨](#contributors-%E2%9C%A8)
|
|
24
27
|
|
|
@@ -37,6 +40,143 @@ You can use the following command to install this package, or replace `npm insta
|
|
|
37
40
|
npm install @sapphire/duration
|
|
38
41
|
```
|
|
39
42
|
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
**Note:** While this section uses `require`, the imports match 1:1 with ESM imports. For example `const { Duration } = require('@sapphire/duration')` equals `import { Duration } from '@sapphire/duration'`.
|
|
46
|
+
|
|
47
|
+
### Human Readable Milliseconds
|
|
48
|
+
|
|
49
|
+
Milliseconds are often hard for humans to quickly parse, so it's nice to use
|
|
50
|
+
named enum members to make it easier to read.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
// Import the Time enum
|
|
54
|
+
const { Time } = require('@sapphire/duration');
|
|
55
|
+
|
|
56
|
+
setTimeout(() => {
|
|
57
|
+
// Do something in half a second
|
|
58
|
+
}, Time.Second / 2 /* 500 */);
|
|
59
|
+
|
|
60
|
+
setTimeout(() => {
|
|
61
|
+
// Do something in 6 hours
|
|
62
|
+
}, Time.Hour * 6 /* 21600000 */);
|
|
63
|
+
|
|
64
|
+
setTimeout(() => {
|
|
65
|
+
// Do something in 1 day
|
|
66
|
+
}, Time.Day /* 86400000 */);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Parsing a Duration
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
// Import the Duration class
|
|
73
|
+
const { Duration } = require('@sapphire/duration');
|
|
74
|
+
|
|
75
|
+
// Create a Duration from a string
|
|
76
|
+
new Duration('1d3h15m3s').offset; // 98103000
|
|
77
|
+
new Duration('1 day, 3h & 15m, some extra characters, and another 3 seconds').offset; // 98103000
|
|
78
|
+
|
|
79
|
+
// The date from now after the specified duration
|
|
80
|
+
new Duration('1d3h15m3s').fromNow;
|
|
81
|
+
|
|
82
|
+
// Or use a specific date
|
|
83
|
+
new Duration('1d3h15m3s').dateFrom(new Date('2020-01-01T00:00:00.000Z'));
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
<details>
|
|
87
|
+
<summary>
|
|
88
|
+
<b>Show all available tokens</b>
|
|
89
|
+
</summary>
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
new Duration('1 nanosecond').offset; // 0.000001
|
|
93
|
+
new Duration('2 nanoseconds').offset; // 0.000002
|
|
94
|
+
new Duration('1 ns').offset; // 0.000001
|
|
95
|
+
|
|
96
|
+
new Duration('1 millisecond').offset; // 1
|
|
97
|
+
new Duration('2 milliseconds').offset; // 2
|
|
98
|
+
new Duration('1 ms').offset; // 1
|
|
99
|
+
|
|
100
|
+
new Duration('1 second').offset; // 1000
|
|
101
|
+
new Duration('2 seconds').offset; // 2000
|
|
102
|
+
new Duration('1 sec').offset; // 1000
|
|
103
|
+
new Duration('2 secs').offset; // 2000
|
|
104
|
+
new Duration('1 s').offset; // 1000
|
|
105
|
+
|
|
106
|
+
new Duration('1 minute').offset; // 60000
|
|
107
|
+
new Duration('2 minutes').offset; // 120000
|
|
108
|
+
new Duration('1 min').offset; // 60000
|
|
109
|
+
new Duration('2 mins').offset; // 120000
|
|
110
|
+
new Duration('1 m').offset; // 60000
|
|
111
|
+
|
|
112
|
+
new Duration('1 hour').offset; // 3600000
|
|
113
|
+
new Duration('2 hours').offset; // 7200000
|
|
114
|
+
new Duration('1 hr').offset; // 3600000
|
|
115
|
+
new Duration('2 hrs').offset; // 7200000
|
|
116
|
+
new Duration('1 h').offset; // 3600000
|
|
117
|
+
|
|
118
|
+
new Duration('1 day').offset; // 86400000
|
|
119
|
+
new Duration('2 days').offset; // 172800000
|
|
120
|
+
new Duration('1 d').offset; // 86400000
|
|
121
|
+
|
|
122
|
+
new Duration('1 week').offset; // 604800000
|
|
123
|
+
new Duration('2 weeks').offset; // 1209600000
|
|
124
|
+
new Duration('1 wk').offset; // 604800000
|
|
125
|
+
new Duration('2 wks').offset; // 1209600000
|
|
126
|
+
new Duration('1 w').offset; // 604800000
|
|
127
|
+
|
|
128
|
+
new Duration('1 month').offset; // 2629800000
|
|
129
|
+
new Duration('2 months').offset; // 5259600000
|
|
130
|
+
new Duration('1 b').offset; // 2629800000
|
|
131
|
+
new Duration('2 mo').offset; // 5259600000
|
|
132
|
+
|
|
133
|
+
new Duration('1 year').offset; // 31557600000
|
|
134
|
+
new Duration('2 years').offset; // 63115200000
|
|
135
|
+
new Duration('1 yr').offset; // 31557600000
|
|
136
|
+
new Duration('2 yrs').offset; // 63115200000
|
|
137
|
+
new Duration('1 y').offset; // 31557600000
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
</details>
|
|
141
|
+
|
|
142
|
+
### Serializing a Duration
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
// Import the DurationFormatter class
|
|
146
|
+
const { DurationFormatter } = require('@sapphire/duration');
|
|
147
|
+
|
|
148
|
+
const formatter = new DurationFormatter();
|
|
149
|
+
|
|
150
|
+
// Serialize a duration
|
|
151
|
+
formatter.format(98103000); // 1 day 3 hours 15 minutes 3 seconds
|
|
152
|
+
formatter.format(-98103000); // -1 day 3 hours 15 minutes 3 seconds
|
|
153
|
+
|
|
154
|
+
// Serialize a duration with specified precision
|
|
155
|
+
formatter.format(98103000, 2); // 1 day 3 hours
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Localizing with Durations
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
// Import the DurationFormatter class
|
|
162
|
+
const { DurationFormatter, TimeTypes } = require('@sapphire/duration');
|
|
163
|
+
|
|
164
|
+
// Create custom unit names
|
|
165
|
+
const units = {
|
|
166
|
+
[TimeTypes.Year]: {
|
|
167
|
+
1: 'año',
|
|
168
|
+
DEFAULT: 'años'
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// Create a formatter the custom units
|
|
173
|
+
const formatter = new DurationFormatter(units);
|
|
174
|
+
|
|
175
|
+
// Serialize a duration
|
|
176
|
+
formatter.format(31557600000); // 1 año
|
|
177
|
+
formatter.format(63115200000); // 2 años
|
|
178
|
+
```
|
|
179
|
+
|
|
40
180
|
## Buy us some doughnuts
|
|
41
181
|
|
|
42
182
|
Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/duration",
|
|
3
|
-
"version": "1.1.0-next.
|
|
3
|
+
"version": "1.1.0-next.ef27abe.0",
|
|
4
4
|
"description": "A time duration utility library for JavaScript.",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -58,11 +58,11 @@
|
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"@favware/cliff-jumper": "^1.8.8",
|
|
61
|
-
"@vitest/coverage-c8": "^0.
|
|
62
|
-
"tsup": "^6.
|
|
63
|
-
"typedoc": "^0.23.
|
|
64
|
-
"typedoc-json-parser": "^
|
|
61
|
+
"@vitest/coverage-c8": "^0.24.3",
|
|
62
|
+
"tsup": "^6.3.0",
|
|
63
|
+
"typedoc": "^0.23.17",
|
|
64
|
+
"typedoc-json-parser": "^6.0.2",
|
|
65
65
|
"typescript": "^4.8.4",
|
|
66
|
-
"vitest": "^0.
|
|
66
|
+
"vitest": "^0.24.3"
|
|
67
67
|
}
|
|
68
68
|
}
|