@cocreate/utils 1.41.1 → 1.42.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.42.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.41.2...v1.42.0) (2026-03-01)
2
+
3
+
4
+ ### Features
5
+
6
+ * add utility functions for date validation and operator processing ([6649ca4](https://github.com/CoCreate-app/CoCreate-utils/commit/6649ca45218adafa14030e998c8a004da30a9397))
7
+
8
+ ## [1.41.2](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.41.1...v1.41.2) (2026-02-28)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * root factory variable Module ([c723787](https://github.com/CoCreate-app/CoCreate-utils/commit/c7237877d833976c821eeb322fd18b3e92f7fd37))
14
+
1
15
  ## [1.41.1](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.41.0...v1.41.1) (2026-02-04)
2
16
 
3
17
 
package/README.md CHANGED
@@ -31,11 +31,46 @@ For a complete guide and working demo refer to the [doumentation](https://cocrea
31
31
  $ npm i @cocreate/utils
32
32
  ```
33
33
 
34
- ## yarn
35
-
36
- ```shell
37
- $ yarn install @cocreate/utils
38
- ```
34
+ ## yarn
35
+
36
+ ```shell
37
+ $ yarn install @cocreate/utils
38
+ ```
39
+
40
+ ## Module Usage
41
+
42
+ ### ESM (tree-shaking)
43
+
44
+ ```js
45
+ import { uid, ObjectId } from "@cocreate/utils";
46
+ ```
47
+
48
+ ### ESM subpath imports
49
+
50
+ ```js
51
+ import { getValueFromObject } from "@cocreate/utils/getValueFromObject";
52
+ import { queryElements } from "@cocreate/utils/queryElements";
53
+ ```
54
+
55
+ ### CommonJS (Node)
56
+
57
+ ```js
58
+ const { uid, ObjectId } = require("@cocreate/utils");
59
+ const { getValueFromObject } = require("@cocreate/utils/getValueFromObject");
60
+ ```
61
+
62
+ ## Build
63
+
64
+ ```shell
65
+ # Build UMD + ESM + CJS
66
+ npm run build
67
+
68
+ # Build only module outputs
69
+ npm run build:modules
70
+
71
+ # Build only UMD bundle
72
+ npm run build:umd
73
+ ```
39
74
 
40
75
  # Table of Contents
41
76
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/utils",
3
- "version": "1.41.1",
3
+ "version": "1.42.0",
4
4
  "description": "A simple utils component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "utils",
@@ -17,7 +17,11 @@
17
17
  },
18
18
  "scripts": {
19
19
  "start": "npx webpack --config webpack.config.js",
20
- "build": "npx webpack --mode=production --config webpack.config.js",
20
+ "build:umd": "npx webpack --mode=production --config webpack.config.js",
21
+ "build:modules:esm": "npx esbuild src/*.js --outdir=dist/esm --format=esm --target=es2017 && node -e \"require('fs').writeFileSync('dist/esm/package.json', '{\\n \\\"type\\\": \\\"module\\\"\\n}\\n')\"",
22
+ "build:modules:cjs": "npx esbuild src/*.js --outdir=dist/cjs --format=cjs --target=node18",
23
+ "build:modules": "npm run build:modules:esm && npm run build:modules:cjs",
24
+ "build": "npm run build:umd && npm run build:modules",
21
25
  "dev": "npx webpack --config webpack.config.js --watch",
22
26
  "postinstall": "node -e \"const { execSync } = require('child_process'); try { execSync('coc --version', { stdio: 'ignore' }); } catch (error) { try { execSync('npm install -g @cocreate/cli', { stdio: 'inherit' }); console.log('Installed \"@cocreate/cli\" globally.'); } catch (error) { console.error('Failed to install \"@cocreate/cli\" globally:', error); } }\""
23
27
  },
@@ -35,7 +39,129 @@
35
39
  "type": "GitHub Sponsors ❤",
36
40
  "url": "https://github.com/sponsors/CoCreate-app"
37
41
  },
38
- "main": "./src/index.js",
42
+ "main": "./dist/cjs/index.js",
43
+ "module": "./dist/esm/index.js",
44
+ "exports": {
45
+ ".": {
46
+ "import": "./dist/esm/index.js",
47
+ "require": "./dist/cjs/index.js"
48
+ },
49
+ "./getRelativePath": {
50
+ "import": "./dist/esm/getRelativePath.js",
51
+ "require": "./dist/cjs/getRelativePath.js"
52
+ },
53
+ "./ObjectId": {
54
+ "import": "./dist/esm/ObjectId.js",
55
+ "require": "./dist/cjs/ObjectId.js"
56
+ },
57
+ "./uid": {
58
+ "import": "./dist/esm/uid.js",
59
+ "require": "./dist/cjs/uid.js"
60
+ },
61
+ "./checkValue": {
62
+ "import": "./dist/esm/checkValue.js",
63
+ "require": "./dist/cjs/checkValue.js"
64
+ },
65
+ "./isValidDate": {
66
+ "import": "./dist/esm/isValidDate.js",
67
+ "require": "./dist/cjs/isValidDate.js"
68
+ },
69
+ "./objectToSearchParams": {
70
+ "import": "./dist/esm/objectToSearchParams.js",
71
+ "require": "./dist/cjs/objectToSearchParams.js"
72
+ },
73
+ "./dotNotationToObject": {
74
+ "import": "./dist/esm/dotNotationToObject.js",
75
+ "require": "./dist/cjs/dotNotationToObject.js"
76
+ },
77
+ "./objectToDotNotation": {
78
+ "import": "./dist/esm/objectToDotNotation.js",
79
+ "require": "./dist/cjs/objectToDotNotation.js"
80
+ },
81
+ "./getValueFromObject": {
82
+ "import": "./dist/esm/getValueFromObject.js",
83
+ "require": "./dist/cjs/getValueFromObject.js"
84
+ },
85
+ "./createUpdate": {
86
+ "import": "./dist/esm/createUpdate.js",
87
+ "require": "./dist/cjs/createUpdate.js"
88
+ },
89
+ "./domParser": {
90
+ "import": "./dist/esm/domParser.js",
91
+ "require": "./dist/cjs/domParser.js"
92
+ },
93
+ "./parseTextToHtml": {
94
+ "import": "./dist/esm/parseTextToHtml.js",
95
+ "require": "./dist/cjs/parseTextToHtml.js"
96
+ },
97
+ "./escapeHtml": {
98
+ "import": "./dist/esm/escapeHtml.js",
99
+ "require": "./dist/cjs/escapeHtml.js"
100
+ },
101
+ "./cssPath": {
102
+ "import": "./dist/esm/cssPath.js",
103
+ "require": "./dist/cjs/cssPath.js"
104
+ },
105
+ "./queryElements": {
106
+ "import": "./dist/esm/queryElements.js",
107
+ "require": "./dist/cjs/queryElements.js"
108
+ },
109
+ "./checkMediaQueries": {
110
+ "import": "./dist/esm/queryElements.js",
111
+ "require": "./dist/cjs/queryElements.js"
112
+ },
113
+ "./queryData": {
114
+ "import": "./dist/esm/dataQuery.js",
115
+ "require": "./dist/cjs/dataQuery.js"
116
+ },
117
+ "./searchData": {
118
+ "import": "./dist/esm/dataQuery.js",
119
+ "require": "./dist/cjs/dataQuery.js"
120
+ },
121
+ "./sortData": {
122
+ "import": "./dist/esm/dataQuery.js",
123
+ "require": "./dist/cjs/dataQuery.js"
124
+ },
125
+ "./getAttributes": {
126
+ "import": "./dist/esm/attributes.js",
127
+ "require": "./dist/cjs/attributes.js"
128
+ },
129
+ "./getAttributeNames": {
130
+ "import": "./dist/esm/attributes.js",
131
+ "require": "./dist/cjs/attributes.js"
132
+ },
133
+ "./setAttributeNames": {
134
+ "import": "./dist/esm/attributes.js",
135
+ "require": "./dist/cjs/attributes.js"
136
+ },
137
+ "./safeParse": {
138
+ "import": "./dist/esm/safeParse.js",
139
+ "require": "./dist/cjs/safeParse.js"
140
+ },
141
+ "./processOperators": {
142
+ "import": "./dist/esm/operators.js",
143
+ "require": "./dist/cjs/operators.js"
144
+ },
145
+ "./processOperatorsAsync": {
146
+ "import": "./dist/esm/operators.js",
147
+ "require": "./dist/cjs/operators.js"
148
+ },
149
+ "./clickedElement": {
150
+ "import": "./dist/esm/clickedElement.js",
151
+ "require": "./dist/cjs/clickedElement.js"
152
+ },
153
+ "./init-browser": {
154
+ "import": "./dist/esm/init-browser.js",
155
+ "require": "./dist/cjs/init-browser.js"
156
+ },
157
+ "./package.json": "./package.json"
158
+ },
159
+ "sideEffects": [
160
+ "./dist/esm/init-browser.js",
161
+ "./dist/cjs/init-browser.js",
162
+ "./dist/CoCreate-utils.js",
163
+ "./dist/CoCreate-utils.min.js"
164
+ ],
39
165
  "devDependencies": {
40
166
  "css-loader": "^5.1.3",
41
167
  "esbuild": "^0.25.2",
@@ -0,0 +1,41 @@
1
+ let counter = 0;
2
+
3
+ export function ObjectId(inputId) {
4
+ if (inputId && /^[0-9a-fA-F]{24}$/.test(inputId)) {
5
+ return {
6
+ timestamp: inputId.substring(0, 8),
7
+ processId: inputId.substring(8, 20),
8
+ counter: inputId.substring(20),
9
+ toString: function () {
10
+ return this.timestamp + this.processId + this.counter;
11
+ }
12
+ };
13
+ } else if (inputId) {
14
+ throw new Error("Invalid ObjectId provided.");
15
+ }
16
+
17
+ const timestampHex = Math.floor(
18
+ new Date(new Date().toISOString()).getTime() / 1000
19
+ )
20
+ .toString(16)
21
+ .padStart(8, "0");
22
+ const processIdHex = Math.floor(Math.random() * 0x100000000000)
23
+ .toString(16)
24
+ .padStart(12, "0");
25
+
26
+ counter = (counter + 1) % 10000;
27
+ if (counter < 2) {
28
+ counter = Math.floor(Math.random() * (5000 - 100 + 1)) + 100;
29
+ }
30
+
31
+ const counterHex = counter.toString(16).padStart(4, "0");
32
+
33
+ return {
34
+ timestamp: timestampHex,
35
+ processId: processIdHex,
36
+ counter: counterHex,
37
+ toString: function () {
38
+ return this.timestamp + this.processId + this.counter;
39
+ }
40
+ };
41
+ }
@@ -0,0 +1,48 @@
1
+ export function getAttributes(el) {
2
+ if (!el) return;
3
+
4
+ let attributes = window.CoCreateConfig.attributes;
5
+ let object = {};
6
+
7
+ for (let attribute of el.attributes) {
8
+ let variable = attributes[attribute.name];
9
+ if (variable) {
10
+ object[variable] = el.getAttribute(attribute.name);
11
+ }
12
+ }
13
+
14
+ return object;
15
+ }
16
+
17
+ export function getAttributeNames(variables) {
18
+ let reversedObject = {};
19
+ for (const key of Object.keys(CoCreateConfig.attributes)) {
20
+ reversedObject[CoCreateConfig.attributes[key]] = key;
21
+ }
22
+
23
+ let attributes = [];
24
+ for (const variable of variables) {
25
+ let attribute = reversedObject[variable];
26
+ if (attribute) attributes.push(attribute);
27
+ }
28
+ return attributes;
29
+ }
30
+
31
+ export function setAttributeNames(attributes, overWrite) {
32
+ let reversedObject = {};
33
+ for (const key of Object.keys(CoCreateConfig.attributes)) {
34
+ reversedObject[CoCreateConfig.attributes[key]] = key;
35
+ }
36
+
37
+ for (const attribute of Object.keys(attributes)) {
38
+ const variable = attributes[attribute];
39
+ if (!reversedObject[variable] || overWrite != false)
40
+ reversedObject[variable] = attribute;
41
+ }
42
+
43
+ let revertObject = {};
44
+ for (const key of Object.keys(reversedObject)) {
45
+ revertObject[reversedObject[key]] = key;
46
+ }
47
+ CoCreateConfig.attributes = revertObject;
48
+ }
@@ -0,0 +1,4 @@
1
+ export function checkValue(value) {
2
+ if (/{{\s*([\w\W]+)\s*}}/g.test(value)) return false;
3
+ else return true;
4
+ }
@@ -0,0 +1,28 @@
1
+ export function clickedElement() {
2
+ document.addEventListener("click", (e) => {
3
+ document.clickedElement = e.target;
4
+ });
5
+
6
+ try {
7
+ let frames = document.querySelectorAll("iframe");
8
+ for (let frame of frames) {
9
+ try {
10
+ let frameDocument = frame.contentDocument;
11
+ if (!frameDocument.clickedElementListenerAdded) {
12
+ frameDocument.addEventListener("click", (e) => {
13
+ frameDocument.clickedElement = e.target;
14
+ });
15
+
16
+ frameDocument.clickedElementListenerAdded = true;
17
+ }
18
+ } catch (iframeError) {
19
+ console.log(
20
+ `Cross-origin frame handling failed for: ${frame}`,
21
+ iframeError
22
+ );
23
+ }
24
+ }
25
+ } catch (e) {
26
+ console.log("Top-level frame document handling failed:", e);
27
+ }
28
+ }
package/src/core.js ADDED
@@ -0,0 +1,6 @@
1
+ export { getRelativePath } from "./getRelativePath.js";
2
+ export { ObjectId } from "./ObjectId.js";
3
+ export { uid } from "./uid.js";
4
+ export { checkValue } from "./checkValue.js";
5
+ export { isValidDate } from "./isValidDate.js";
6
+ export { objectToSearchParams } from "./objectToSearchParams.js";
@@ -0,0 +1,202 @@
1
+ import { dotNotationToObject } from "./dotNotationToObject.js";
2
+ import { objectToDotNotation } from "./objectToDotNotation.js";
3
+ import { getValueFromObject } from "./getValueFromObject.js";
4
+
5
+ function isEqualArray(arr1, arr2) {
6
+ if (arr1.length !== arr2.length) {
7
+ return false;
8
+ }
9
+ for (let i = 0; i < arr1.length; i++) {
10
+ if (!isEqualObject(arr1[i], arr2[i])) {
11
+ return false;
12
+ }
13
+ }
14
+ return true;
15
+ }
16
+
17
+ function isEqualObject(obj1, obj2) {
18
+ const keys1 = Object.keys(obj1);
19
+ const keys2 = Object.keys(obj2);
20
+
21
+ if (keys1.length !== keys2.length) {
22
+ return false;
23
+ }
24
+
25
+ for (const key of keys1) {
26
+ if (obj1[key] !== obj2[key]) {
27
+ return false;
28
+ }
29
+ }
30
+
31
+ return true;
32
+ }
33
+
34
+ function dotNotationToObjectUpdate(data, object = {}) {
35
+ try {
36
+ for (const key of Object.keys(data)) {
37
+ let newObject = object;
38
+ let keys = key
39
+ .replace(/\[(\d+)\]/g, ".$1")
40
+ .split(".")
41
+ .map((k) => (isNaN(k) ? k : Number(k)));
42
+ let value = data[key];
43
+ let operator;
44
+ if (keys[0].startsWith("$")) operator = keys.shift();
45
+
46
+ let length = keys.length - 1;
47
+ for (let i = 0; i < keys.length; i++) {
48
+ if (length == i) {
49
+ if (operator) {
50
+ let operators = [
51
+ "$rename",
52
+ "$inc",
53
+ "$push",
54
+ "$each",
55
+ "$splice",
56
+ "$unset",
57
+ "$delete",
58
+ "$slice",
59
+ "$pop",
60
+ "$shift",
61
+ "$addToSet",
62
+ "$pull"
63
+ ];
64
+ if (!operators.includes(operator)) continue;
65
+ if (operator === "$rename") {
66
+ newObject[value] = newObject[keys[i]];
67
+ delete newObject[keys[i]];
68
+ } else if (
69
+ operator === "$delete" ||
70
+ operator === "$unset" ||
71
+ operator === "$slice"
72
+ ) {
73
+ if (typeof keys[i] === "number") {
74
+ newObject.splice(keys[i], 1);
75
+ } else {
76
+ delete newObject[keys[i]];
77
+ }
78
+ } else if (operator === "$shift") {
79
+ newObject[keys[i]].shift();
80
+ } else if (operator === "$pop") {
81
+ newObject[keys[i]].pop();
82
+ } else if (operator === "$addToSet") {
83
+ if (!newObject[keys[i]]) newObject[keys[i]] = [];
84
+ let exists;
85
+ if (Array.isArray(value)) {
86
+ exists = newObject[keys[i]].some(
87
+ (item) => Array.isArray(item) && isEqualArray(item, value)
88
+ );
89
+ } else if (
90
+ typeof value === "object" &&
91
+ value !== null
92
+ ) {
93
+ exists = newObject[keys[i]].some(
94
+ (item) =>
95
+ typeof item === "object" && isEqualObject(item, value)
96
+ );
97
+ } else {
98
+ exists = newObject[keys[i]].includes(value);
99
+ }
100
+ if (!exists) newObject[keys[i]].push(value);
101
+ } else if (operator === "$pull") {
102
+ if (!newObject[keys[i]]) continue;
103
+ if (Array.isArray(value)) {
104
+ newObject[keys[i]] = newObject[keys[i]].filter(
105
+ (item) => !Array.isArray(item) || !isEqualArray(item, value)
106
+ );
107
+ } else if (
108
+ typeof value === "object" &&
109
+ value !== null
110
+ ) {
111
+ newObject[keys[i]] = newObject[keys[i]].filter(
112
+ (item) =>
113
+ typeof item !== "object" || !isEqualObject(item, value)
114
+ );
115
+ } else {
116
+ newObject[keys[i]] = newObject[keys[i]].filter(
117
+ (item) => item !== value
118
+ );
119
+ }
120
+ } else if (operator === "$push" || operator === "$splice") {
121
+ if (
122
+ typeof keys[i] === "number" &&
123
+ newObject.length >= keys[i]
124
+ )
125
+ newObject.splice(keys[i], 0, value);
126
+ else if (newObject[keys[i]]) newObject[keys[i]].push(value);
127
+ else newObject[keys[i]] = [value];
128
+ } else if (operator === "$each") {
129
+ if (!Array.isArray(value)) value = [value];
130
+ if (typeof keys[i] === "number")
131
+ newObject.splice(keys[i], 0, ...value);
132
+ else newObject[keys[i]].push(...value);
133
+ } else if (operator === "$inc") {
134
+ if (
135
+ !newObject[keys[i]] ||
136
+ typeof newObject[keys[i]] !== "number"
137
+ )
138
+ newObject[keys[i]] = value;
139
+ else newObject[keys[i]] += value;
140
+ }
141
+ } else if (value === undefined) {
142
+ if (typeof keys[i] === "number") newObject.splice(keys[i], 1);
143
+ else delete newObject[keys[i]];
144
+ } else if (typeof keys[i] === "number") {
145
+ newObject.splice(keys[i], 0, value);
146
+ } else {
147
+ newObject[keys[i]] = value;
148
+ }
149
+ } else if (
150
+ typeof keys[i + 1] === "number" &&
151
+ !Array.isArray(newObject[keys[i]])
152
+ ) {
153
+ newObject[keys[i]] = [];
154
+ } else {
155
+ newObject[keys[i]] = newObject[keys[i]] || {};
156
+ }
157
+ newObject = newObject[keys[i]];
158
+ }
159
+ }
160
+ return object;
161
+ } catch (error) {
162
+ console.log("Error converting dot notation to object", error);
163
+ return false;
164
+ }
165
+ }
166
+
167
+ export function createUpdate(update, data, globalOpertors) {
168
+ let operatorKeys = {};
169
+ if (globalOpertors) operatorKeys = { ...globalOpertors };
170
+
171
+ Object.keys(data).forEach((key) => {
172
+ if (key.startsWith("$")) {
173
+ if (!key.includes("."))
174
+ for (let oldkey of Object.keys(data[key]))
175
+ operatorKeys[key + "." + oldkey] = data[key][oldkey];
176
+ else operatorKeys[key] = data[key];
177
+ } else if (
178
+ typeof data[key] === "string" &&
179
+ data[key].startsWith("$")
180
+ ) {
181
+ operatorKeys[data[key] + "." + key] = data[key];
182
+ } else if (key.endsWith("]")) {
183
+ const regex = /^(.*(?:\[\d+\].*?)?)\[(.*?)\](?:\[\])?$/;
184
+ const match = key.match(regex);
185
+ if (match[2] === "") operatorKeys["$push." + match[1]] = data[key];
186
+ else {
187
+ let index = parseInt(match[2], 10);
188
+ if (Number.isNaN(index))
189
+ operatorKeys[match[2] + "." + match[1]] = data[key];
190
+ else operatorKeys[key] = data[key];
191
+ }
192
+ } else if (key.includes(".")) {
193
+ operatorKeys[key] = data[key];
194
+ } else if (data[key] === undefined) {
195
+ delete update[key];
196
+ } else update[key] = data[key];
197
+ });
198
+
199
+ return dotNotationToObjectUpdate(operatorKeys, update);
200
+ }
201
+
202
+ export { dotNotationToObject, objectToDotNotation, getValueFromObject };
package/src/cssPath.js ADDED
@@ -0,0 +1,50 @@
1
+ export function cssPath(node, container) {
2
+ let pathSplits = [];
3
+ do {
4
+ if (!node || !node.tagName) return false;
5
+ let pathSplit = node.tagName.toLowerCase();
6
+
7
+ if (node.id) {
8
+ pathSplit += "#" + node.id;
9
+ node = "";
10
+ } else {
11
+ let eid = node.getAttribute("eid");
12
+ if (eid && !/{{\s*([\w\W]+)\s*}}/g.test(eid)) {
13
+ pathSplit += `[eid="${eid}"]`;
14
+ node = "";
15
+ } else if (node.parentNode && node.parentNode.children.length > 1) {
16
+ let children = [];
17
+ for (let child of node.parentNode.children) {
18
+ if (child.tagName == node.tagName) children.push(child);
19
+ }
20
+ let index = Array.prototype.indexOf.call(children, node);
21
+
22
+ pathSplit += `:nth-of-type(${index + 1})`;
23
+ }
24
+
25
+ node = node.parentNode;
26
+ if (
27
+ node == null ||
28
+ node.tagName == "HTML" ||
29
+ node.tagName == "BODY" ||
30
+ node.tagName == "DOM-PARSER" ||
31
+ node.nodeName == "#document" ||
32
+ node.hasAttribute("contenteditable")
33
+ )
34
+ node = "";
35
+ }
36
+
37
+ pathSplits.unshift(pathSplit);
38
+ } while (node);
39
+ let path = pathSplits.join(" > ");
40
+ if (path && path.includes("<")) {
41
+ let index = path.lastIndexOf(" >");
42
+ if (index != -1) path = path.slice(0, index);
43
+ else {
44
+ index = path.lastIndexOf("<");
45
+ path = path.slice(0, index);
46
+ }
47
+ }
48
+
49
+ return path;
50
+ }