@sapphire/duration 1.1.0-next.e3fa75f.0 → 1.1.0-next.edd1210.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 CHANGED
@@ -18,7 +18,10 @@
18
18
  - [Features](#features)
19
19
  - [Installation](#installation)
20
20
  - [Usage](#usage)
21
- - [Basic Usage](#basic-usage)
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!
@@ -109,6 +249,12 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
109
249
  <td align="center"><a href="https://enes.ovh/"><img src="https://avatars.githubusercontent.com/u/61084101?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Enes Genç</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=enxg" title="Code">💻</a></td>
110
250
  <td align="center"><a href="https://github.com/muchnameless"><img src="https://avatars.githubusercontent.com/u/12682826?v=4?s=100" width="100px;" alt=""/><br /><sub><b>muchnameless</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=muchnameless" title="Code">💻</a></td>
111
251
  <td align="center"><a href="https://github.com/r-priyam"><img src="https://avatars.githubusercontent.com/u/50884372?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Priyam</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=r-priyam" title="Code">💻</a></td>
252
+ <td align="center"><a href="https://github.com/legendhimslef"><img src="https://avatars.githubusercontent.com/u/69213593?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Voxelli</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=legendhimslef" title="Code">💻</a></td>
253
+ <td align="center"><a href="https://github.com/CitTheDev"><img src="https://avatars.githubusercontent.com/u/94020875?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Cit The Dev</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=CitTheDev" title="Code">💻</a></td>
254
+ </tr>
255
+ <tr>
256
+ <td align="center"><a href="https://www.goestav.com/"><img src="https://avatars.githubusercontent.com/u/27970303?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Goestav</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=goestav" title="Code">💻</a></td>
257
+ <td align="center"><a href="https://github.com/didinele"><img src="https://avatars.githubusercontent.com/u/27137376?v=4?s=100" width="100px;" alt=""/><br /><sub><b>DD</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=didinele" title="Code">💻</a></td>
112
258
  </tr>
113
259
  </table>
114
260
 
@@ -187,9 +187,8 @@ var SapphireDuration = (function (exports) {
187
187
  exports.Time = Time;
188
188
  exports.TimeTypes = TimeTypes;
189
189
 
190
- Object.defineProperty(exports, '__esModule', { value: true });
191
-
192
190
  return exports;
193
191
 
194
192
  })({});
193
+ //# sourceMappingURL=out.js.map
195
194
  //# sourceMappingURL=index.global.js.map
package/dist/index.js CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var __defProp = Object.defineProperty;
6
4
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
5
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
@@ -187,4 +185,5 @@ exports.Duration = Duration;
187
185
  exports.DurationFormatter = DurationFormatter;
188
186
  exports.Time = Time;
189
187
  exports.TimeTypes = TimeTypes;
188
+ //# sourceMappingURL=out.js.map
190
189
  //# sourceMappingURL=index.js.map
package/dist/index.mjs CHANGED
@@ -178,4 +178,5 @@ function addUnit(time, unit, separator) {
178
178
  __name(addUnit, "addUnit");
179
179
 
180
180
  export { DEFAULT_SEPARATORS, DEFAULT_UNITS, Duration, DurationFormatter, Time, TimeTypes };
181
+ //# sourceMappingURL=out.js.map
181
182
  //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire/duration",
3
- "version": "1.1.0-next.e3fa75f.0",
3
+ "version": "1.1.0-next.edd1210.0",
4
4
  "description": "A time duration utility library for JavaScript.",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
@@ -57,12 +57,12 @@
57
57
  "access": "public"
58
58
  },
59
59
  "devDependencies": {
60
- "@favware/cliff-jumper": "^1.8.8",
61
- "@vitest/coverage-c8": "^0.23.4",
62
- "tsup": "^6.2.3",
63
- "typedoc": "^0.23.15",
64
- "typedoc-json-parser": "^5.0.1",
65
- "typescript": "^4.8.4",
66
- "vitest": "^0.23.4"
60
+ "@favware/cliff-jumper": "^1.9.0",
61
+ "@vitest/coverage-c8": "^0.25.2",
62
+ "tsup": "^6.5.0",
63
+ "typedoc": "^0.23.21",
64
+ "typedoc-json-parser": "^7.0.1",
65
+ "typescript": "^4.9.3",
66
+ "vitest": "^0.25.2"
67
67
  }
68
68
  }