@amekusa/util.js 1.0.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.
@@ -1,4 +1,247 @@
1
1
  /*!
2
+ * === @amekusa/util.js/web === *
3
+ * MIT License
4
+ *
5
+ * Copyright (c) 2024 Satoshi Soma
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ /**
27
+ * Converts non-safe chars in the given string into HTML entities.
28
+ * @param {string} str
29
+ * @return {string}
30
+ */
31
+ function escHTML(str) {
32
+ return `${str}`.replace(escHTML_find, escHTML_replace);
33
+ }
34
+
35
+ const escHtml = escHTML; // alias
36
+
37
+ const escHTML_map = {
38
+ '&': 'amp',
39
+ '"': 'quot',
40
+ "'": 'apos',
41
+ '<': 'lt',
42
+ '>': 'gt'
43
+ };
44
+
45
+ const escHTML_find = new RegExp(`["'<>]|(&(?!${Object.values(escHTML_map).join('|')};))`, 'g');
46
+ // NOTE:
47
+ // - This avoids double-escaping '&' symbols
48
+ // - Regex negative match: (?!word)
49
+
50
+ const escHTML_replace = found => `&${escHTML_map[found]};`;
51
+
52
+ var web = /*#__PURE__*/Object.freeze({
53
+ __proto__: null,
54
+ escHTML: escHTML,
55
+ escHtml: escHtml
56
+ });
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
+
243
+ /*!
244
+ * === @amekusa/util.js === *
2
245
  * MIT License
3
246
  *
4
247
  * Copyright (c) 2024 Satoshi Soma
@@ -31,6 +274,26 @@ function arr(x) {
31
274
  return Array.isArray(x) ? x : [x];
32
275
  }
33
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
+
34
297
  /**
35
298
  * Returns whether the given value can be considered as "empty".
36
299
  * @param {any} x
@@ -127,4 +390,4 @@ var main = {
127
390
  merge,
128
391
  };
129
392
 
130
- export { arr, clean, main as default, isEmpty, merge };
393
+ export { arr, clean, main as default, isArray, isEmpty, isNumOrStr, merge, time, web };
@@ -3,6 +3,249 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  /*!
6
+ * === @amekusa/util.js/web === *
7
+ * MIT License
8
+ *
9
+ * Copyright (c) 2024 Satoshi Soma
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ * of this software and associated documentation files (the "Software"), to deal
13
+ * in the Software without restriction, including without limitation the rights
14
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ * copies of the Software, and to permit persons to whom the Software is
16
+ * furnished to do so, subject to the following conditions:
17
+ *
18
+ * The above copyright notice and this permission notice shall be included in all
19
+ * copies or substantial portions of the Software.
20
+ *
21
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ * SOFTWARE.
28
+ */
29
+
30
+ /**
31
+ * Converts non-safe chars in the given string into HTML entities.
32
+ * @param {string} str
33
+ * @return {string}
34
+ */
35
+ function escHTML(str) {
36
+ return `${str}`.replace(escHTML_find, escHTML_replace);
37
+ }
38
+
39
+ const escHtml = escHTML; // alias
40
+
41
+ const escHTML_map = {
42
+ '&': 'amp',
43
+ '"': 'quot',
44
+ "'": 'apos',
45
+ '<': 'lt',
46
+ '>': 'gt'
47
+ };
48
+
49
+ const escHTML_find = new RegExp(`["'<>]|(&(?!${Object.values(escHTML_map).join('|')};))`, 'g');
50
+ // NOTE:
51
+ // - This avoids double-escaping '&' symbols
52
+ // - Regex negative match: (?!word)
53
+
54
+ const escHTML_replace = found => `&${escHTML_map[found]};`;
55
+
56
+ var web = /*#__PURE__*/Object.freeze({
57
+ __proto__: null,
58
+ escHTML: escHTML,
59
+ escHtml: escHtml
60
+ });
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
+
247
+ /*!
248
+ * === @amekusa/util.js === *
6
249
  * MIT License
7
250
  *
8
251
  * Copyright (c) 2024 Satoshi Soma
@@ -35,6 +278,26 @@ function arr(x) {
35
278
  return Array.isArray(x) ? x : [x];
36
279
  }
37
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
+
38
301
  /**
39
302
  * Returns whether the given value can be considered as "empty".
40
303
  * @param {any} x
@@ -134,5 +397,9 @@ var main = {
134
397
  exports.arr = arr;
135
398
  exports.clean = clean;
136
399
  exports.default = main;
400
+ exports.isArray = isArray;
137
401
  exports.isEmpty = isEmpty;
402
+ exports.isNumOrStr = isNumOrStr;
138
403
  exports.merge = merge;
404
+ exports.time = time;
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.0.0",
8
+ "version": "1.2.0",
9
9
  "description": "General purpose utility for JS",
10
10
  "type": "module",
11
11
  "files": [
@@ -31,15 +31,21 @@
31
31
  },
32
32
  "watch": {
33
33
  "build": {
34
- "inherit": true,
35
- "patterns": ["src"],
36
- "extensions": "js"
34
+ "patterns": [
35
+ "src"
36
+ ],
37
+ "extensions": "js",
38
+ "delay": 100,
39
+ "inherit": true
37
40
  },
38
41
  "test": {
39
- "inherit": true,
40
- "patterns": ["test", "dist/**"],
41
- "extensions": "js",
42
- "delay": 500
42
+ "patterns": [
43
+ "test",
44
+ "dist/**"
45
+ ],
46
+ "extensions": "js,mjs,cjs",
47
+ "delay": 100,
48
+ "inherit": true
43
49
  }
44
50
  },
45
51
  "repository": {
@@ -53,6 +59,6 @@
53
59
  "author": "Satoshi Soma (https://amekusa.com)",
54
60
  "license": "MIT",
55
61
  "devDependencies": {
56
- "@amekusa/nodeutil": "^1.5.1"
62
+ "@amekusa/nodeutil": "^3.3.0"
57
63
  }
58
64
  }