@ptolemy2002/list-object-utils 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/README.md +24 -0
- package/index.js +159 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# React Library CRA Project
|
|
2
|
+
## Meta
|
|
3
|
+
This is a React Library Created by Ptolemy2002's [cra-template-react-library](https://www.npmjs.com/package/@ptolemy2002/cra-template-react-library) template in combination with [create-react-app](https://www.npmjs.com/package/create-react-app). It contains methods of building and publishing your library to npm.
|
|
4
|
+
For now, the library makes use of React 18 and does not use TypeScript.
|
|
5
|
+
|
|
6
|
+
## Peer Dependencies
|
|
7
|
+
These should be installed in order to use the library, as npm does not automatically add peer dependencies to your project.
|
|
8
|
+
- @types/is-callable: ^1.1.2
|
|
9
|
+
- is-callable: ^1.2.7
|
|
10
|
+
|
|
11
|
+
## Commands
|
|
12
|
+
The following commands exist in the project:
|
|
13
|
+
|
|
14
|
+
- `npm run uninstall` - Uninstalls all dependencies for the library
|
|
15
|
+
- `npm run reinstall` - Uninstalls and then Reinstalls all dependencies for the library
|
|
16
|
+
- `npm run example-uninstall` - Uninstalls all dependencies for the example app
|
|
17
|
+
- `npm run example-install` - Installs all dependencies for the example app
|
|
18
|
+
- `npm run example-reinstall` - Uninstalls and then Reinstalls all dependencies for the example app
|
|
19
|
+
- `npm run example-start` - Starts the example app after building the library
|
|
20
|
+
- `npm run build` - Builds the library
|
|
21
|
+
- `npm run release` - Publishes the library to npm without changing the version
|
|
22
|
+
- `npm run release-patch` - Publishes the library to npm with a patch version bump
|
|
23
|
+
- `npm run release-minor` - Publishes the library to npm with a minor version bump
|
|
24
|
+
- `npm run release-major` - Publishes the library to npm with a major version bump
|
package/index.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/index.js
|
|
30
|
+
var src_exports = {};
|
|
31
|
+
__export(src_exports, {
|
|
32
|
+
isSet: () => isSet,
|
|
33
|
+
listDifference: () => listDifference,
|
|
34
|
+
listPush: () => listPush,
|
|
35
|
+
listRemove: () => listRemove,
|
|
36
|
+
listSet: () => listSet,
|
|
37
|
+
listSwap: () => listSwap,
|
|
38
|
+
listsEqual: () => listsEqual,
|
|
39
|
+
objectDifference: () => objectDifference,
|
|
40
|
+
objectToString: () => objectToString,
|
|
41
|
+
objectsEqual: () => objectsEqual,
|
|
42
|
+
sortWithIndeces: () => sortWithIndeces
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(src_exports);
|
|
45
|
+
var import_is_callable = __toESM(require("is-callable"));
|
|
46
|
+
function listsEqual(a, b) {
|
|
47
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return false;
|
|
48
|
+
if (a === b) return true;
|
|
49
|
+
if (!a || !b) return false;
|
|
50
|
+
if (a.length !== b.length) return false;
|
|
51
|
+
for (let i = 0; i < a.length; ++i) {
|
|
52
|
+
if (Array.isArray(a[i]) && Array.isArray(b[i])) {
|
|
53
|
+
if (!listsEqual(a[i], b[i])) return false;
|
|
54
|
+
} else if (typeof a[i] === "object" && typeof b[i] === "object") {
|
|
55
|
+
if (!objectsEqual(a[i], b[i])) return false;
|
|
56
|
+
} else if (a[i] !== b[i]) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
function objectsEqual(a, b) {
|
|
63
|
+
if (!(typeof a === "object" && !Array.isArray(a)) || !(typeof b === "object" && !Array.isArray(b))) return false;
|
|
64
|
+
if (a === b) return true;
|
|
65
|
+
if (!a || !b) return false;
|
|
66
|
+
if (Object.keys(a).length !== Object.keys(b).length) return false;
|
|
67
|
+
for (const key in a) {
|
|
68
|
+
if (Array.isArray(a[key]) && Array.isArray(b[key])) {
|
|
69
|
+
if (!listsEqual(a[key], b[key])) return false;
|
|
70
|
+
} else if (typeof a[key] === "object" && typeof b[key] === "object") {
|
|
71
|
+
if (!objectsEqual(a[key], b[key])) return false;
|
|
72
|
+
} else if (a[key] !== b[key]) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
function listSwap(list, i, j) {
|
|
79
|
+
const newList = [...list];
|
|
80
|
+
const temp = newList[i];
|
|
81
|
+
newList[i] = newList[j];
|
|
82
|
+
newList[j] = temp;
|
|
83
|
+
return newList;
|
|
84
|
+
}
|
|
85
|
+
function listRemove(list, i) {
|
|
86
|
+
const newList = [...list];
|
|
87
|
+
newList.splice(i, 1);
|
|
88
|
+
return newList;
|
|
89
|
+
}
|
|
90
|
+
function listPush(list, element) {
|
|
91
|
+
return [...list, element];
|
|
92
|
+
}
|
|
93
|
+
function listSet(list, i, element) {
|
|
94
|
+
const newList = [...list];
|
|
95
|
+
newList[i] = element;
|
|
96
|
+
return newList;
|
|
97
|
+
}
|
|
98
|
+
function isSet(list) {
|
|
99
|
+
const seen = [];
|
|
100
|
+
for (let i = 0; i < list.length; i++) {
|
|
101
|
+
if (seen.includes(list[i])) return false;
|
|
102
|
+
seen.push(list[i]);
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
function listDifference(a, b, keyConvert = null, valueConvert = null) {
|
|
107
|
+
if (!(0, import_is_callable.default)(keyConvert)) keyConvert = (x) => x;
|
|
108
|
+
if (!(0, import_is_callable.default)(valueConvert)) valueConvert = (x) => x;
|
|
109
|
+
const result = {};
|
|
110
|
+
a.forEach((element, i) => {
|
|
111
|
+
if (b[i] !== element) {
|
|
112
|
+
if (Array.isArray(element) && Array.isArray(b[i])) {
|
|
113
|
+
const k = keyConvert(i, element, b[i]);
|
|
114
|
+
result[k] = listDifference(element, b[i], keyConvert, valueConvert);
|
|
115
|
+
if (result[k].length === 0) delete result[k];
|
|
116
|
+
} else if (typeof element === "object" && typeof b[i] === "object") {
|
|
117
|
+
const k = keyConvert(i, element, b[i]);
|
|
118
|
+
result[k] = objectDifference(element, b[i], keyConvert, valueConvert);
|
|
119
|
+
if (Object.keys(result[k]).length === 0) delete result[k];
|
|
120
|
+
} else {
|
|
121
|
+
result[keyConvert(i, element, b[i])] = valueConvert(b[i], i, element);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
if (b.length > a.length) {
|
|
126
|
+
for (let i = a.length; i < b.length; i++) {
|
|
127
|
+
result[keyConvert(i, void 0, b[i])] = valueConvert(b[i], i, void 0);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
function objectDifference(a, b, keyConvert = null, valueConvert = null) {
|
|
133
|
+
if (!(0, import_is_callable.default)(keyConvert)) keyConvert = (x) => x;
|
|
134
|
+
if (!(0, import_is_callable.default)(valueConvert)) valueConvert = (x) => x;
|
|
135
|
+
const result = {};
|
|
136
|
+
for (const key in a) {
|
|
137
|
+
if (b[key] !== a[key]) {
|
|
138
|
+
if (Array.isArray(a[key]) && Array.isArray(b[key])) {
|
|
139
|
+
result[keyConvert(key, a[key], b[key])] = listDifference(a[key], b[key], keyConvert, valueConvert);
|
|
140
|
+
} else if (typeof a[key] === "object" && typeof b[key] === "object") {
|
|
141
|
+
result[keyConvert(key, a[key], b[key])] = objectDifference(a[key], b[key], keyConvert, valueConvert);
|
|
142
|
+
} else {
|
|
143
|
+
result[keyConvert(key, a[key], b[key])] = valueConvert(b[key], key, a[key]);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
for (const key in b) {
|
|
148
|
+
if (!a.hasOwnProperty(key)) {
|
|
149
|
+
result[keyConvert(key, void 0, b[key])] = valueConvert(b[key], key, void 0);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
function sortWithIndeces(toSort, sort = () => 0, descending = false) {
|
|
155
|
+
return Array.from(toSort.keys()).sort((a, b) => sort(a, b) * (descending ? -1 : 1)).map((i) => toSort[i]);
|
|
156
|
+
}
|
|
157
|
+
function objectToString(obj) {
|
|
158
|
+
return JSON.stringify(obj, (k, v) => v === void 0 ? null : v);
|
|
159
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ptolemy2002/list-object-utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"files": [
|
|
6
|
+
"index.js"
|
|
7
|
+
],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "esbuild src/index.js --bundle --format=cjs --outfile=index.js --loader:.js=jsx --external:is-callable",
|
|
10
|
+
"postinstall": "npx typesync",
|
|
11
|
+
"uninstall": "bash ./scripts/uninstall.sh",
|
|
12
|
+
"reinstall": "bash ./scripts/reinstall.sh",
|
|
13
|
+
"example-uninstall": "bash ./scripts/example-uninstall.sh",
|
|
14
|
+
"example-install": "bash ./scripts/example-install.sh",
|
|
15
|
+
"example-reinstall": "bash ./scripts/example-reinstall.sh",
|
|
16
|
+
"example-start": "bash ./scripts/example-start.sh",
|
|
17
|
+
"release": "bash ./scripts/release.sh",
|
|
18
|
+
"release-patch": "bash ./scripts/release.sh patch",
|
|
19
|
+
"release-minor": "bash ./scripts/release.sh minor",
|
|
20
|
+
"release-major": "bash ./scripts/release.sh major"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/Ptolemy2002/list-object-utils",
|
|
25
|
+
"directory": "lib"
|
|
26
|
+
},
|
|
27
|
+
"description": "Utilities for working with lists and objects",
|
|
28
|
+
"license": "ISC",
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@types/is-callable": "~1.1.2",
|
|
31
|
+
"is-callable": "^1.2.7"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"esbuild": "^0.23.0"
|
|
35
|
+
}
|
|
36
|
+
}
|