@amekusa/util.js 1.1.0 → 1.2.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.
@@ -55,6 +55,191 @@ escHTML: escHTML,
55
55
  escHtml: escHtml
56
56
  });
57
57
 
58
+ /*!
59
+ * === @amekusa/util.js/time === *
60
+ * MIT License
61
+ *
62
+ * Copyright (c) 2024 Satoshi Soma
63
+ *
64
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
65
+ * of this software and associated documentation files (the "Software"), to deal
66
+ * in the Software without restriction, including without limitation the rights
67
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
68
+ * copies of the Software, and to permit persons to whom the Software is
69
+ * furnished to do so, subject to the following conditions:
70
+ *
71
+ * The above copyright notice and this permission notice shall be included in all
72
+ * copies or substantial portions of the Software.
73
+ *
74
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
75
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
76
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
77
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
78
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
79
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
80
+ * SOFTWARE.
81
+ */
82
+
83
+ /**
84
+ * Coerces the given value into a `Date` object.
85
+ * @param {...any} args - A `Date` object or args to pass to `Date()`
86
+ * @return {Date}
87
+ */
88
+ function date(...args) {
89
+ if (!args.length || !args[0]) return new Date();
90
+ if (args[0] instanceof Date) return args[0];
91
+ return new Date(...args);
92
+ }
93
+
94
+ /**
95
+ * Coerces the given value into a number of milliseconds.
96
+ * @param {...args} args - A number or args to pass to `Date()`
97
+ * @return {number} milliseconds
98
+ */
99
+ function ms(...args) {
100
+ if (!args.length || !args[0]) return Date.now();
101
+ let x = args[0];
102
+ if (typeof x == 'number') return x;
103
+ if (x instanceof Date) return x.getTime();
104
+ return (new Date(...args)).getTime();
105
+ }
106
+
107
+ /**
108
+ * Adds the given amount of time to a `Date` object.
109
+ * @param {Date} d - Date object to modify
110
+ * @param {number} amount - Millieconds to add
111
+ * @return {Date} modified Date
112
+ */
113
+ function addTime(d, amount) {
114
+ d.setTime(d.getTime() + amount);
115
+ return d;
116
+ }
117
+
118
+ /**
119
+ * Subtracts the timezone offset from a `Date` object.
120
+ * @param {Date} d - Date object to modify
121
+ * @return {Date} modified Date
122
+ */
123
+ function localize(d) {
124
+ d.setTime(d.getTime() - d.getTimezoneOffset() * 60000);
125
+ return d;
126
+ }
127
+
128
+ /**
129
+ * Quantizes a `Date` object with the given amount of time.
130
+ * @param {Date} d - Date object to modify
131
+ * @param {number} step - Quantization step size
132
+ * @param {string} [method='round'] - `Math` method to apply
133
+ * @return {Date} modified Date
134
+ */
135
+ function quantize(d, step, method = 'round') {
136
+ d.setTime(Math[method](d.getTime() / step) * step);
137
+ return d;
138
+ }
139
+
140
+ /**
141
+ * Alias of `quantize(d, step, 'round')`.
142
+ */
143
+ function round(d, step) {
144
+ return quantize(d, step, 'round');
145
+ }
146
+
147
+ /**
148
+ * Alias of `quantize(d, step, 'floor')`.
149
+ */
150
+ function floor(d, step) {
151
+ return quantize(d, step, 'floor');
152
+ }
153
+
154
+ /**
155
+ * Alias of `quantize(d, step, 'ceil')`.
156
+ */
157
+ function ceil(d, step) {
158
+ return quantize(d, step, 'ceil');
159
+ }
160
+
161
+ /**
162
+ * Returns `YYYY`, `MM`, and `DD` representations of a `Date` object.
163
+ * @param {Date} d - Date object
164
+ * @param {string|object} [format]
165
+ * - If omitted, the return value will be an array consists of the three parts.
166
+ * - If a string is passed, the three parts will be joined with the string as a separator.
167
+ * - If an object is passed, the three parts will be assigned as `Y`, `M`, and `D` properties.
168
+ * @return {string|string[]|object}
169
+ */
170
+ function ymd(d, format = null) {
171
+ let r = [
172
+ d.getFullYear().toString(),
173
+ (d.getMonth() + 1).toString().padStart(2, '0'),
174
+ d.getDate().toString().padStart(2, '0'),
175
+ ];
176
+ if (!format) return r;
177
+ switch (typeof format) {
178
+ case 'string':
179
+ return r.join(format);
180
+ case 'object':
181
+ format.Y = r[0];
182
+ format.M = r[1];
183
+ format.D = r[2];
184
+ return format;
185
+ default:
186
+ throw `invalid type`;
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Returns `hh`, `mm`, and `ss` representations of a `Date` object.
192
+ * @param {Date} d - Date object
193
+ * @param {string|object} [format]
194
+ * - If omited, the return value will be an array consists of the three parts.
195
+ * - If a string is passed, the three parts will be joined with the string as a separator.
196
+ * - If an object is passed, the three parts will be assigned as `h`, `m`, and `s` properties.
197
+ * @return {string|string[]|object}
198
+ */
199
+ function hms(d, format = null) {
200
+ let r = [
201
+ d.getHours().toString().padStart(2, '0'),
202
+ d.getMinutes().toString().padStart(2, '0'),
203
+ d.getSeconds().toString().padStart(2, '0'),
204
+ ];
205
+ if (!format) return r;
206
+ switch (typeof format) {
207
+ case 'string':
208
+ return r.join(format);
209
+ case 'object':
210
+ format.h = r[0];
211
+ format.m = r[1];
212
+ format.s = r[2];
213
+ return format;
214
+ default:
215
+ throw `invalid type`;
216
+ }
217
+ }
218
+
219
+ /**
220
+ * Returns a string representation of the given `Date` in ISO 9075 format, which is standard for MySQL.
221
+ * @param {Date} d - Date object
222
+ * @return {string} a string like `YYYY-MM-DD hh:mm:ss`
223
+ */
224
+ function iso9075(d) {
225
+ return ymd(d, '-') + ' ' + hms(d, ':');
226
+ }
227
+
228
+ var time = /*#__PURE__*/Object.freeze({
229
+ __proto__: null,
230
+ addTime: addTime,
231
+ ceil: ceil,
232
+ date: date,
233
+ floor: floor,
234
+ hms: hms,
235
+ iso9075: iso9075,
236
+ localize: localize,
237
+ ms: ms,
238
+ quantize: quantize,
239
+ round: round,
240
+ ymd: ymd
241
+ });
242
+
58
243
  /*!
59
244
  * === @amekusa/util.js === *
60
245
  * MIT License
@@ -89,6 +274,26 @@ function arr(x) {
89
274
  return Array.isArray(x) ? x : [x];
90
275
  }
91
276
 
277
+ /**
278
+ * Alias of `Array.isArray`.
279
+ * @return {boolean}
280
+ */
281
+ const isArray = Array.isArray;
282
+
283
+ /**
284
+ * Returns whether the given value is a number or a string.
285
+ * @param {any} x
286
+ * @return {boolean}
287
+ */
288
+ function isNumOrStr(x) {
289
+ switch (typeof x) {
290
+ case 'number':
291
+ case 'string':
292
+ return true;
293
+ }
294
+ return false;
295
+ }
296
+
92
297
  /**
93
298
  * Returns whether the given value can be considered as "empty".
94
299
  * @param {any} x
@@ -185,4 +390,4 @@ var main = {
185
390
  merge,
186
391
  };
187
392
 
188
- export { arr, clean, main as default, isEmpty, merge, web };
393
+ export { arr, clean, main as default, isArray, isEmpty, isNumOrStr, merge, time, web };
@@ -59,6 +59,191 @@ escHTML: escHTML,
59
59
  escHtml: escHtml
60
60
  });
61
61
 
62
+ /*!
63
+ * === @amekusa/util.js/time === *
64
+ * MIT License
65
+ *
66
+ * Copyright (c) 2024 Satoshi Soma
67
+ *
68
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
69
+ * of this software and associated documentation files (the "Software"), to deal
70
+ * in the Software without restriction, including without limitation the rights
71
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
72
+ * copies of the Software, and to permit persons to whom the Software is
73
+ * furnished to do so, subject to the following conditions:
74
+ *
75
+ * The above copyright notice and this permission notice shall be included in all
76
+ * copies or substantial portions of the Software.
77
+ *
78
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
79
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
80
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
81
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
82
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
83
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
84
+ * SOFTWARE.
85
+ */
86
+
87
+ /**
88
+ * Coerces the given value into a `Date` object.
89
+ * @param {...any} args - A `Date` object or args to pass to `Date()`
90
+ * @return {Date}
91
+ */
92
+ function date(...args) {
93
+ if (!args.length || !args[0]) return new Date();
94
+ if (args[0] instanceof Date) return args[0];
95
+ return new Date(...args);
96
+ }
97
+
98
+ /**
99
+ * Coerces the given value into a number of milliseconds.
100
+ * @param {...args} args - A number or args to pass to `Date()`
101
+ * @return {number} milliseconds
102
+ */
103
+ function ms(...args) {
104
+ if (!args.length || !args[0]) return Date.now();
105
+ let x = args[0];
106
+ if (typeof x == 'number') return x;
107
+ if (x instanceof Date) return x.getTime();
108
+ return (new Date(...args)).getTime();
109
+ }
110
+
111
+ /**
112
+ * Adds the given amount of time to a `Date` object.
113
+ * @param {Date} d - Date object to modify
114
+ * @param {number} amount - Millieconds to add
115
+ * @return {Date} modified Date
116
+ */
117
+ function addTime(d, amount) {
118
+ d.setTime(d.getTime() + amount);
119
+ return d;
120
+ }
121
+
122
+ /**
123
+ * Subtracts the timezone offset from a `Date` object.
124
+ * @param {Date} d - Date object to modify
125
+ * @return {Date} modified Date
126
+ */
127
+ function localize(d) {
128
+ d.setTime(d.getTime() - d.getTimezoneOffset() * 60000);
129
+ return d;
130
+ }
131
+
132
+ /**
133
+ * Quantizes a `Date` object with the given amount of time.
134
+ * @param {Date} d - Date object to modify
135
+ * @param {number} step - Quantization step size
136
+ * @param {string} [method='round'] - `Math` method to apply
137
+ * @return {Date} modified Date
138
+ */
139
+ function quantize(d, step, method = 'round') {
140
+ d.setTime(Math[method](d.getTime() / step) * step);
141
+ return d;
142
+ }
143
+
144
+ /**
145
+ * Alias of `quantize(d, step, 'round')`.
146
+ */
147
+ function round(d, step) {
148
+ return quantize(d, step, 'round');
149
+ }
150
+
151
+ /**
152
+ * Alias of `quantize(d, step, 'floor')`.
153
+ */
154
+ function floor(d, step) {
155
+ return quantize(d, step, 'floor');
156
+ }
157
+
158
+ /**
159
+ * Alias of `quantize(d, step, 'ceil')`.
160
+ */
161
+ function ceil(d, step) {
162
+ return quantize(d, step, 'ceil');
163
+ }
164
+
165
+ /**
166
+ * Returns `YYYY`, `MM`, and `DD` representations of a `Date` object.
167
+ * @param {Date} d - Date object
168
+ * @param {string|object} [format]
169
+ * - If omitted, the return value will be an array consists of the three parts.
170
+ * - If a string is passed, the three parts will be joined with the string as a separator.
171
+ * - If an object is passed, the three parts will be assigned as `Y`, `M`, and `D` properties.
172
+ * @return {string|string[]|object}
173
+ */
174
+ function ymd(d, format = null) {
175
+ let r = [
176
+ d.getFullYear().toString(),
177
+ (d.getMonth() + 1).toString().padStart(2, '0'),
178
+ d.getDate().toString().padStart(2, '0'),
179
+ ];
180
+ if (!format) return r;
181
+ switch (typeof format) {
182
+ case 'string':
183
+ return r.join(format);
184
+ case 'object':
185
+ format.Y = r[0];
186
+ format.M = r[1];
187
+ format.D = r[2];
188
+ return format;
189
+ default:
190
+ throw `invalid type`;
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Returns `hh`, `mm`, and `ss` representations of a `Date` object.
196
+ * @param {Date} d - Date object
197
+ * @param {string|object} [format]
198
+ * - If omited, the return value will be an array consists of the three parts.
199
+ * - If a string is passed, the three parts will be joined with the string as a separator.
200
+ * - If an object is passed, the three parts will be assigned as `h`, `m`, and `s` properties.
201
+ * @return {string|string[]|object}
202
+ */
203
+ function hms(d, format = null) {
204
+ let r = [
205
+ d.getHours().toString().padStart(2, '0'),
206
+ d.getMinutes().toString().padStart(2, '0'),
207
+ d.getSeconds().toString().padStart(2, '0'),
208
+ ];
209
+ if (!format) return r;
210
+ switch (typeof format) {
211
+ case 'string':
212
+ return r.join(format);
213
+ case 'object':
214
+ format.h = r[0];
215
+ format.m = r[1];
216
+ format.s = r[2];
217
+ return format;
218
+ default:
219
+ throw `invalid type`;
220
+ }
221
+ }
222
+
223
+ /**
224
+ * Returns a string representation of the given `Date` in ISO 9075 format, which is standard for MySQL.
225
+ * @param {Date} d - Date object
226
+ * @return {string} a string like `YYYY-MM-DD hh:mm:ss`
227
+ */
228
+ function iso9075(d) {
229
+ return ymd(d, '-') + ' ' + hms(d, ':');
230
+ }
231
+
232
+ var time = /*#__PURE__*/Object.freeze({
233
+ __proto__: null,
234
+ addTime: addTime,
235
+ ceil: ceil,
236
+ date: date,
237
+ floor: floor,
238
+ hms: hms,
239
+ iso9075: iso9075,
240
+ localize: localize,
241
+ ms: ms,
242
+ quantize: quantize,
243
+ round: round,
244
+ ymd: ymd
245
+ });
246
+
62
247
  /*!
63
248
  * === @amekusa/util.js === *
64
249
  * MIT License
@@ -93,6 +278,26 @@ function arr(x) {
93
278
  return Array.isArray(x) ? x : [x];
94
279
  }
95
280
 
281
+ /**
282
+ * Alias of `Array.isArray`.
283
+ * @return {boolean}
284
+ */
285
+ const isArray = Array.isArray;
286
+
287
+ /**
288
+ * Returns whether the given value is a number or a string.
289
+ * @param {any} x
290
+ * @return {boolean}
291
+ */
292
+ function isNumOrStr(x) {
293
+ switch (typeof x) {
294
+ case 'number':
295
+ case 'string':
296
+ return true;
297
+ }
298
+ return false;
299
+ }
300
+
96
301
  /**
97
302
  * Returns whether the given value can be considered as "empty".
98
303
  * @param {any} x
@@ -192,6 +397,9 @@ var main = {
192
397
  exports.arr = arr;
193
398
  exports.clean = clean;
194
399
  exports.default = main;
400
+ exports.isArray = isArray;
195
401
  exports.isEmpty = isEmpty;
402
+ exports.isNumOrStr = isNumOrStr;
196
403
  exports.merge = merge;
404
+ exports.time = time;
197
405
  exports.web = web;
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org/"
7
7
  },
8
- "version": "1.1.0",
8
+ "version": "1.2.0",
9
9
  "description": "General purpose utility for JS",
10
10
  "type": "module",
11
11
  "files": [
@@ -31,21 +31,21 @@
31
31
  },
32
32
  "watch": {
33
33
  "build": {
34
- "inherit": true,
35
34
  "patterns": [
36
35
  "src"
37
36
  ],
38
37
  "extensions": "js",
39
- "delay": 100
38
+ "delay": 100,
39
+ "inherit": true
40
40
  },
41
41
  "test": {
42
- "inherit": true,
43
42
  "patterns": [
44
43
  "test",
45
44
  "dist/**"
46
45
  ],
47
46
  "extensions": "js,mjs,cjs",
48
- "delay": 100
47
+ "delay": 100,
48
+ "inherit": true
49
49
  }
50
50
  },
51
51
  "repository": {
@@ -59,6 +59,6 @@
59
59
  "author": "Satoshi Soma (https://amekusa.com)",
60
60
  "license": "MIT",
61
61
  "devDependencies": {
62
- "@amekusa/nodeutil": "^1.5.1"
62
+ "@amekusa/nodeutil": "^3.3.0"
63
63
  }
64
64
  }