Skip to content

Commit 6649ca4

Browse files
committed
feat: add utility functions for date validation and operator processing
- Added `safeParse` for evaluating expressions with context and error handling. - Added `operator.js` for processinhg update operators like `$user_id`, `$parse`, etc.
1 parent 0231b9b commit 6649ca4

26 files changed

+2111
-1372
lines changed

README.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,46 @@ For a complete guide and working demo refer to the [doumentation](https://cocrea
3131
$ npm i @cocreate/utils
3232
```
3333

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+
```
3974

4075
# Table of Contents
4176

package.json

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
},
1818
"scripts": {
1919
"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",
2125
"dev": "npx webpack --config webpack.config.js --watch",
2226
"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); } }\""
2327
},
@@ -35,7 +39,129 @@
3539
"type": "GitHub Sponsors ❤",
3640
"url": "https://github.com/sponsors/CoCreate-app"
3741
},
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+
],
39165
"devDependencies": {
40166
"css-loader": "^5.1.3",
41167
"esbuild": "^0.25.2",

src/ObjectId.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/attributes.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/checkValue.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/clickedElement.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/core.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)