@amekusa/util.js 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Satoshi Soma
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,60 @@
1
+ # @amekusa/util.js
2
+ General purpose utility for JS
3
+
4
+ ## INSTALL
5
+ ```sh
6
+ npm i @amekusa/util.js
7
+ ```
8
+
9
+ ## USAGE
10
+
11
+ ### ES
12
+ ```js
13
+ // A) import functions
14
+ import {
15
+ arr,
16
+ isEmpty,
17
+ clean,
18
+ merge,
19
+ } from '@amekusa/util.js';
20
+
21
+ // B) import as an object
22
+ import util from '@amekusa/util.js';
23
+ ```
24
+
25
+ ### CJS
26
+ ```js
27
+ // A) import functions
28
+ const {
29
+ arr,
30
+ isEmpty,
31
+ clean,
32
+ merge,
33
+ } = require('@amekusa/util.js');
34
+
35
+ // B) import as an object
36
+ const util = require('@amekusa/util.js');
37
+ ```
38
+
39
+ ## LICENSE
40
+ MIT License
41
+
42
+ Copyright (c) 2024 Satoshi Soma
43
+
44
+ Permission is hereby granted, free of charge, to any person obtaining a copy
45
+ of this software and associated documentation files (the "Software"), to deal
46
+ in the Software without restriction, including without limitation the rights
47
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
48
+ copies of the Software, and to permit persons to whom the Software is
49
+ furnished to do so, subject to the following conditions:
50
+
51
+ The above copyright notice and this permission notice shall be included in all
52
+ copies or substantial portions of the Software.
53
+
54
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
56
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
57
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
58
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
59
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
60
+ SOFTWARE.
@@ -0,0 +1,130 @@
1
+ /*!
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2024 Satoshi Soma
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ /**
26
+ * Coerces the given value into an array.
27
+ * @param {any} x
28
+ * @return {any[]}
29
+ */
30
+ function arr(x) {
31
+ return Array.isArray(x) ? x : [x];
32
+ }
33
+
34
+ /**
35
+ * Returns whether the given value can be considered as "empty".
36
+ * @param {any} x
37
+ * @return {boolean}
38
+ */
39
+ function isEmpty(x) {
40
+ if (Array.isArray(x)) return x.length == 0;
41
+ switch (typeof x) {
42
+ case 'string':
43
+ return !x;
44
+ case 'object':
45
+ if (x === null) return true;
46
+ for (let i in x) return false;
47
+ case 'undefined':
48
+ return true;
49
+ }
50
+ return false;
51
+ }
52
+
53
+ /**
54
+ * Removes "empty" values from the given object or array.
55
+ * @param {object|any[]} x
56
+ * @param {number} recurse - Recursion limit
57
+ * @return {object|any[]} modified `x`
58
+ */
59
+ function clean(x, recurse = 8) {
60
+ if (recurse) {
61
+ if (Array.isArray(x)) {
62
+ let r = [];
63
+ for (let i = 0; i < x.length; i++) {
64
+ let I = clean(x[i], recurse - 1);
65
+ if (!isEmpty(I)) r.push(I);
66
+ }
67
+ return r;
68
+ }
69
+ if (typeof x == 'object') {
70
+ let r = {};
71
+ for (let k in x) {
72
+ let v = clean(x[k], recurse - 1);
73
+ if (!isEmpty(v)) r[k] = v;
74
+ }
75
+ return r;
76
+ }
77
+ }
78
+ return x;
79
+ }
80
+
81
+ /**
82
+ * Merges the 2nd object into the 1st object recursively (deep-merge). The 1st object will be modified.
83
+ * @param {object} x - The 1st object
84
+ * @param {object} y - The 2nd object
85
+ * @param {object} [opts] - Options
86
+ * @param {number} opts.recurse=8 - Recurstion limit. Negative number means unlimited
87
+ * @param {boolean|string} opts.mergeArrays - How to merge arrays
88
+ * - `true`: merge x with y
89
+ * - 'push': push y elements to x
90
+ * - 'concat': concat x and y
91
+ * - other: replace x with y
92
+ * @return {object} The 1st object
93
+ */
94
+ function merge(x, y, opts = {}) {
95
+ if (!('recurse' in opts)) opts.recurse = 8;
96
+ switch (Array.isArray(x) + Array.isArray(y)) {
97
+ case 0: // no array
98
+ if (opts.recurse && x && y && typeof x == 'object' && typeof y == 'object') {
99
+ opts.recurse--;
100
+ for (let k in y) x[k] = merge(x[k], y[k], opts);
101
+ opts.recurse++;
102
+ return x;
103
+ }
104
+ case 1: // 1 array
105
+ return y;
106
+ }
107
+ // 2 arrays
108
+ switch (opts.mergeArrays) {
109
+ case true:
110
+ for (let i = 0; i < y.length; i++) {
111
+ if (!x.includes(y[i])) x.push(y[i]);
112
+ }
113
+ return x;
114
+ case 'push':
115
+ x.push(...y);
116
+ return x;
117
+ case 'concat':
118
+ return x.concat(y);
119
+ }
120
+ return y;
121
+ }
122
+
123
+ var main = {
124
+ arr,
125
+ isEmpty,
126
+ clean,
127
+ merge,
128
+ };
129
+
130
+ export { arr, clean, main as default, isEmpty, merge };
@@ -0,0 +1,138 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /*!
6
+ * MIT License
7
+ *
8
+ * Copyright (c) 2024 Satoshi Soma
9
+ *
10
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ * of this software and associated documentation files (the "Software"), to deal
12
+ * in the Software without restriction, including without limitation the rights
13
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ * copies of the Software, and to permit persons to whom the Software is
15
+ * furnished to do so, subject to the following conditions:
16
+ *
17
+ * The above copyright notice and this permission notice shall be included in all
18
+ * copies or substantial portions of the Software.
19
+ *
20
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ * SOFTWARE.
27
+ */
28
+
29
+ /**
30
+ * Coerces the given value into an array.
31
+ * @param {any} x
32
+ * @return {any[]}
33
+ */
34
+ function arr(x) {
35
+ return Array.isArray(x) ? x : [x];
36
+ }
37
+
38
+ /**
39
+ * Returns whether the given value can be considered as "empty".
40
+ * @param {any} x
41
+ * @return {boolean}
42
+ */
43
+ function isEmpty(x) {
44
+ if (Array.isArray(x)) return x.length == 0;
45
+ switch (typeof x) {
46
+ case 'string':
47
+ return !x;
48
+ case 'object':
49
+ if (x === null) return true;
50
+ for (let i in x) return false;
51
+ case 'undefined':
52
+ return true;
53
+ }
54
+ return false;
55
+ }
56
+
57
+ /**
58
+ * Removes "empty" values from the given object or array.
59
+ * @param {object|any[]} x
60
+ * @param {number} recurse - Recursion limit
61
+ * @return {object|any[]} modified `x`
62
+ */
63
+ function clean(x, recurse = 8) {
64
+ if (recurse) {
65
+ if (Array.isArray(x)) {
66
+ let r = [];
67
+ for (let i = 0; i < x.length; i++) {
68
+ let I = clean(x[i], recurse - 1);
69
+ if (!isEmpty(I)) r.push(I);
70
+ }
71
+ return r;
72
+ }
73
+ if (typeof x == 'object') {
74
+ let r = {};
75
+ for (let k in x) {
76
+ let v = clean(x[k], recurse - 1);
77
+ if (!isEmpty(v)) r[k] = v;
78
+ }
79
+ return r;
80
+ }
81
+ }
82
+ return x;
83
+ }
84
+
85
+ /**
86
+ * Merges the 2nd object into the 1st object recursively (deep-merge). The 1st object will be modified.
87
+ * @param {object} x - The 1st object
88
+ * @param {object} y - The 2nd object
89
+ * @param {object} [opts] - Options
90
+ * @param {number} opts.recurse=8 - Recurstion limit. Negative number means unlimited
91
+ * @param {boolean|string} opts.mergeArrays - How to merge arrays
92
+ * - `true`: merge x with y
93
+ * - 'push': push y elements to x
94
+ * - 'concat': concat x and y
95
+ * - other: replace x with y
96
+ * @return {object} The 1st object
97
+ */
98
+ function merge(x, y, opts = {}) {
99
+ if (!('recurse' in opts)) opts.recurse = 8;
100
+ switch (Array.isArray(x) + Array.isArray(y)) {
101
+ case 0: // no array
102
+ if (opts.recurse && x && y && typeof x == 'object' && typeof y == 'object') {
103
+ opts.recurse--;
104
+ for (let k in y) x[k] = merge(x[k], y[k], opts);
105
+ opts.recurse++;
106
+ return x;
107
+ }
108
+ case 1: // 1 array
109
+ return y;
110
+ }
111
+ // 2 arrays
112
+ switch (opts.mergeArrays) {
113
+ case true:
114
+ for (let i = 0; i < y.length; i++) {
115
+ if (!x.includes(y[i])) x.push(y[i]);
116
+ }
117
+ return x;
118
+ case 'push':
119
+ x.push(...y);
120
+ return x;
121
+ case 'concat':
122
+ return x.concat(y);
123
+ }
124
+ return y;
125
+ }
126
+
127
+ var main = {
128
+ arr,
129
+ isEmpty,
130
+ clean,
131
+ merge,
132
+ };
133
+
134
+ exports.arr = arr;
135
+ exports.clean = clean;
136
+ exports.default = main;
137
+ exports.isEmpty = isEmpty;
138
+ exports.merge = merge;
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@amekusa/util.js",
3
+ "private": false,
4
+ "publishConfig": {
5
+ "access": "public",
6
+ "registry": "https://registry.npmjs.org/"
7
+ },
8
+ "version": "1.0.0",
9
+ "description": "General purpose utility for JS",
10
+ "type": "module",
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/import/bundle.js",
17
+ "require": "./dist/require/bundle.cjs"
18
+ }
19
+ },
20
+ "scripts": {
21
+ "dev": "npm-watch",
22
+ "test": "c8 mocha",
23
+ "lint": "eslint .",
24
+ "build": "npm run lint && rollup -c rollup.js",
25
+ "clean": "rm -rf ./dist",
26
+ "prod": "npm run clean; NODE_ENV=production npm run build",
27
+ "docs": "npm run docs:gen && npm run docs:publish",
28
+ "docs:gen": "npm run docs:clean; jsdoc -c jsdoc.json && cd docs/@amekusa/util.js && ln -sfn $npm_package_version latest",
29
+ "docs:clean": "rm -rf docs/@amekusa/util.js/$npm_package_version",
30
+ "docs:publish": "git subtree push --prefix docs/@amekusa/util.js origin gh-pages"
31
+ },
32
+ "watch": {
33
+ "build": {
34
+ "inherit": true,
35
+ "patterns": ["src"],
36
+ "extensions": "js"
37
+ },
38
+ "test": {
39
+ "inherit": true,
40
+ "patterns": ["test", "dist/**"],
41
+ "extensions": "js",
42
+ "delay": 500
43
+ }
44
+ },
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "git+https://github.com/amekusa/util.js.git"
48
+ },
49
+ "keywords": [
50
+ "utility",
51
+ "general"
52
+ ],
53
+ "author": "Satoshi Soma (https://amekusa.com)",
54
+ "license": "MIT",
55
+ "devDependencies": {
56
+ "@amekusa/nodeutil": "^1.5.1"
57
+ }
58
+ }