@domql/utils 2.5.156 → 2.5.161

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.
@@ -0,0 +1,124 @@
1
+ const stringIncludesAny = (str, characters) => {
2
+ for (const char of characters) {
3
+ if (str.includes(char)) {
4
+ return true;
5
+ }
6
+ }
7
+ return false;
8
+ };
9
+ const trimStringFromSymbols = (str, characters) => {
10
+ const pattern = new RegExp(`[${characters.join("\\")}]`, "g");
11
+ return str.replace(pattern, "");
12
+ };
13
+ const brackRegex = {
14
+ 2: /\{\{\s*((?:\.\.\/)+)?([^}\s]+)\s*\}\}/g,
15
+ 3: /\{\{\{\s*((?:\.\.\/)+)?([^}\s]+)\s*\}\}\}/g
16
+ };
17
+ const replaceLiteralsWithObjectFields = (str, state, options = {}) => {
18
+ if (!str.includes(options.bracketsLength === 3 ? "{{{" : "{{"))
19
+ return str;
20
+ const reg = brackRegex[options.bracketsLength || 2];
21
+ return str.replace(reg, (_, parentPath, variable) => {
22
+ if (parentPath) {
23
+ const parentLevels = parentPath.match(options.bracketsLength === 3 ? /\.\.\.\//g : /\.\.\//g).length;
24
+ let parentState = state;
25
+ for (let i = 0; i < parentLevels; i++) {
26
+ parentState = parentState.parent;
27
+ if (!parentState) {
28
+ return "";
29
+ }
30
+ }
31
+ const value = parentState[variable.trim()];
32
+ return value !== void 0 ? `${value}` : "";
33
+ } else {
34
+ const value = state[variable.trim()];
35
+ return value !== void 0 ? `${value}` : "";
36
+ }
37
+ });
38
+ };
39
+ const lowercaseFirstLetter = (inputString) => {
40
+ return `${inputString.charAt(0).toLowerCase()}${inputString.slice(1)}`;
41
+ };
42
+ const findKeyPosition = (str, key) => {
43
+ const lines = str.split("\n");
44
+ let startLineNumber = -1;
45
+ let endLineNumber = -1;
46
+ let startColumn = -1;
47
+ let endColumn = -1;
48
+ const keyPattern = new RegExp(`\\b${key}\\b\\s*:\\s*`);
49
+ let braceCount = 0;
50
+ let foundKey = false;
51
+ for (let i = 0; i < lines.length; i++) {
52
+ if (keyPattern.test(lines[i]) && !foundKey) {
53
+ foundKey = true;
54
+ startLineNumber = i + 1;
55
+ startColumn = lines[i].indexOf(key) + 1;
56
+ if (lines[i].includes("{}")) {
57
+ endLineNumber = startLineNumber;
58
+ endColumn = lines[i].indexOf("{}") + 3;
59
+ break;
60
+ }
61
+ const line = lines[i].slice(startColumn + key.length);
62
+ if (line.includes("{") || line.includes("[")) {
63
+ braceCount = 1;
64
+ } else {
65
+ endLineNumber = i + 1;
66
+ endColumn = lines[i].length + 1;
67
+ break;
68
+ }
69
+ } else if (foundKey) {
70
+ braceCount += (lines[i].match(/{/g) || []).length;
71
+ braceCount += (lines[i].match(/\[/g) || []).length;
72
+ braceCount -= (lines[i].match(/}/g) || []).length;
73
+ braceCount -= (lines[i].match(/]/g) || []).length;
74
+ if (braceCount === 0) {
75
+ endLineNumber = i + 1;
76
+ endColumn = lines[i].lastIndexOf("}") !== -1 ? lines[i].lastIndexOf("}") + 2 : lines[i].length + 1;
77
+ break;
78
+ }
79
+ }
80
+ }
81
+ return {
82
+ startColumn,
83
+ endColumn,
84
+ startLineNumber,
85
+ endLineNumber
86
+ };
87
+ };
88
+ const replaceOctalEscapeSequences = (str) => {
89
+ const octalRegex = /\\([0-7]{1,3})/g;
90
+ return str.replace(octalRegex, (match, p1) => {
91
+ const octalValue = parseInt(p1, 8);
92
+ const char = String.fromCharCode(octalValue);
93
+ return char;
94
+ });
95
+ };
96
+ const encodeNewlines = (str) => {
97
+ return str.split("\n").join("/////n").split("`").join("/////tilde").split("$").join("/////dlrsgn");
98
+ };
99
+ const decodeNewlines = (encodedStr) => {
100
+ return encodedStr.split("/////n").join("\n").split("/////tilde").join("`").split("/////dlrsgn").join("$");
101
+ };
102
+ const customEncodeURIComponent = (str) => {
103
+ return str.split("").map((char) => {
104
+ if (/[^a-zA-Z0-9\s]/.test(char)) {
105
+ return "%" + char.charCodeAt(0).toString(16).toUpperCase();
106
+ }
107
+ return char;
108
+ }).join("");
109
+ };
110
+ const customDecodeURIComponent = (encodedStr) => {
111
+ return encodedStr.replace(/%[0-9A-Fa-f]{2}/g, (match) => String.fromCharCode(parseInt(match.slice(1), 16)));
112
+ };
113
+ export {
114
+ customDecodeURIComponent,
115
+ customEncodeURIComponent,
116
+ decodeNewlines,
117
+ encodeNewlines,
118
+ findKeyPosition,
119
+ lowercaseFirstLetter,
120
+ replaceLiteralsWithObjectFields,
121
+ replaceOctalEscapeSequences,
122
+ stringIncludesAny,
123
+ trimStringFromSymbols
124
+ };
@@ -0,0 +1,145 @@
1
+ const HTML_TAGS = {
2
+ root: [
3
+ "body",
4
+ "html"
5
+ ],
6
+ head: [
7
+ "title",
8
+ "base",
9
+ "meta",
10
+ "style",
11
+ "noscript",
12
+ "script"
13
+ ],
14
+ body: [
15
+ "string",
16
+ "style",
17
+ "fragment",
18
+ "a",
19
+ "abbr",
20
+ "acronym",
21
+ "address",
22
+ "applet",
23
+ "area",
24
+ "article",
25
+ "aside",
26
+ "audio",
27
+ "b",
28
+ "basefont",
29
+ "bdi",
30
+ "bdo",
31
+ "big",
32
+ "blockquote",
33
+ "br",
34
+ "button",
35
+ "canvas",
36
+ "caption",
37
+ "center",
38
+ "cite",
39
+ "code",
40
+ "search",
41
+ "col",
42
+ "colgroup",
43
+ "data",
44
+ "datalist",
45
+ "dd",
46
+ "del",
47
+ "details",
48
+ "dfn",
49
+ "dialog",
50
+ "dir",
51
+ "div",
52
+ "dl",
53
+ "dt",
54
+ "em",
55
+ "embed",
56
+ "fieldset",
57
+ "figcaption",
58
+ "figure",
59
+ "font",
60
+ "footer",
61
+ "form",
62
+ "frame",
63
+ "frameset",
64
+ "h1",
65
+ "h2",
66
+ "h3",
67
+ "h4",
68
+ "h5",
69
+ "h6",
70
+ "head",
71
+ "header",
72
+ "hr",
73
+ "i",
74
+ "iframe",
75
+ "img",
76
+ "input",
77
+ "ins",
78
+ "kbd",
79
+ "label",
80
+ "legend",
81
+ "li",
82
+ "link",
83
+ "main",
84
+ "map",
85
+ "mark",
86
+ "meter",
87
+ "nav",
88
+ "noframes",
89
+ "noscript",
90
+ "object",
91
+ "ol",
92
+ "optgroup",
93
+ "option",
94
+ "output",
95
+ "p",
96
+ "param",
97
+ "picture",
98
+ "pre",
99
+ "progress",
100
+ "hgroup",
101
+ "q",
102
+ "rp",
103
+ "rt",
104
+ "ruby",
105
+ "s",
106
+ "samp",
107
+ "script",
108
+ "section",
109
+ "select",
110
+ "small",
111
+ "source",
112
+ "span",
113
+ "strike",
114
+ "strong",
115
+ "sub",
116
+ "summary",
117
+ "sup",
118
+ "table",
119
+ "tbody",
120
+ "td",
121
+ "template",
122
+ "hgroup",
123
+ "textarea",
124
+ "tfoot",
125
+ "th",
126
+ "thead",
127
+ "time",
128
+ "tr",
129
+ "track",
130
+ "tt",
131
+ "u",
132
+ "ul",
133
+ "var",
134
+ "video",
135
+ "wbr",
136
+ // SVG
137
+ "svg",
138
+ "path"
139
+ ]
140
+ };
141
+ const isValidHtmlTag = (arg) => HTML_TAGS.body.includes(arg);
142
+ export {
143
+ HTML_TAGS,
144
+ isValidHtmlTag
145
+ };
@@ -0,0 +1,64 @@
1
+ import { isHtmlElement, isNode } from "./node";
2
+ const isObject = (arg) => {
3
+ if (arg === null)
4
+ return false;
5
+ return typeof arg === "object" && arg.constructor === Object;
6
+ };
7
+ const isString = (arg) => typeof arg === "string";
8
+ const isNumber = (arg) => typeof arg === "number";
9
+ const isFunction = (arg) => typeof arg === "function";
10
+ const isBoolean = (arg) => arg === true || arg === false;
11
+ const isNull = (arg) => arg === null;
12
+ const isArray = (arg) => Array.isArray(arg);
13
+ const isDate = (d) => d instanceof Date;
14
+ const isObjectLike = (arg) => {
15
+ if (arg === null)
16
+ return false;
17
+ return typeof arg === "object";
18
+ };
19
+ const isDefined = (arg) => {
20
+ return isObject(arg) || isObjectLike(arg) || isString(arg) || isNumber(arg) || isFunction(arg) || isArray(arg) || isObjectLike(arg) || isBoolean(arg) || isDate(arg) || isNull(arg);
21
+ };
22
+ const isUndefined = (arg) => {
23
+ return arg === void 0;
24
+ };
25
+ const TYPES = {
26
+ boolean: isBoolean,
27
+ array: isArray,
28
+ object: isObject,
29
+ string: isString,
30
+ date: isDate,
31
+ number: isNumber,
32
+ null: isNull,
33
+ function: isFunction,
34
+ objectLike: isObjectLike,
35
+ node: isNode,
36
+ htmlElement: isHtmlElement,
37
+ defined: isDefined
38
+ };
39
+ const is = (arg) => {
40
+ return (...args) => {
41
+ return args.map((val) => TYPES[val](arg)).filter((v) => v).length > 0;
42
+ };
43
+ };
44
+ const isNot = (arg) => {
45
+ return (...args) => {
46
+ return args.map((val) => TYPES[val](arg)).filter((v) => v).length === 0;
47
+ };
48
+ };
49
+ export {
50
+ TYPES,
51
+ is,
52
+ isArray,
53
+ isBoolean,
54
+ isDate,
55
+ isDefined,
56
+ isFunction,
57
+ isNot,
58
+ isNull,
59
+ isNumber,
60
+ isObject,
61
+ isObjectLike,
62
+ isString,
63
+ isUndefined
64
+ };
package/object.js CHANGED
@@ -74,142 +74,96 @@ export const clone = (obj, excludeFrom = []) => {
74
74
  return o
75
75
  }
76
76
 
77
- // Clone anything deeply but excludeFrom keys given in 'excludeFrom'
78
- export const deepCloneExclude = (obj, excludeFrom = []) => {
79
- if (isArray(obj)) {
80
- return obj.map(x => deepCloneExclude(x, excludeFrom))
81
- }
82
-
83
- const o = {}
84
- for (const k in obj) {
85
- const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, k)
86
- if (!hasOwnProperty || excludeFrom.includes(k) || k.startsWith('__')) continue
87
-
88
- let v = obj[k]
89
-
90
- if (k === 'extend' && isArray(v)) {
91
- v = mergeArrayExclude(v, excludeFrom)
92
- }
93
-
94
- if (isArray(v)) {
95
- o[k] = v.map(x => deepCloneExclude(x, excludeFrom))
96
- } else if (isObject(v)) {
97
- o[k] = deepCloneExclude(v, excludeFrom)
98
- } else o[k] = v
99
- }
100
-
101
- return o
102
- }
103
-
104
77
  // Merge array, but exclude keys listed in 'excl'z
105
- export const mergeArrayExclude = (arr, excl = []) => {
106
- return arr.reduce((acc, curr) => deepMerge(acc, deepCloneExclude(curr, excl)), {})
78
+ export const mergeArrayExclude = (arr, exclude = []) => {
79
+ return arr.reduce((acc, curr) => deepMerge(acc, deepClone(curr, { exclude })), {})
107
80
  }
108
-
109
81
  /**
110
- * Deep cloning of object
82
+ * Enhanced deep clone function that combines features from multiple implementations
83
+ * @param {any} obj - Object to clone
84
+ * @param {Object} options - Configuration options
85
+ * @param {string[]} options.exclude - Properties to exclude from cloning
86
+ * @param {boolean} options.cleanUndefined - Remove undefined values
87
+ * @param {boolean} options.cleanNull - Remove null values
88
+ * @param {Window} options.window - Window object for cross-frame cloning
89
+ * @param {WeakMap} options.visited - WeakMap for tracking circular references
90
+ * @param {boolean} options.handleExtend - Whether to handle 'extend' arrays specially
91
+ * @returns {any} Cloned object
111
92
  */
112
- export const deepClone = (obj, exclude = [], cleanUndefined = false, visited = new WeakMap()) => {
113
- // Handle non-object types, null, and ignored types
114
- if (!isObjectLike(obj) || isDOMNode(obj)) return obj
115
-
116
- // Check for circular references
117
- if (visited.has(obj)) return visited.get(obj)
118
-
119
- // Create a new object or array
120
- const clone = isArray(obj) ? [] : {}
93
+ export const deepClone = (obj, options = {}) => {
94
+ const {
95
+ exclude = [],
96
+ cleanUndefined = false,
97
+ cleanNull = false,
98
+ window: targetWindow,
99
+ visited = new WeakMap(),
100
+ handleExtend = false
101
+ } = options
121
102
 
122
- // Store the clone in the WeakMap to handle circular references
123
- visited.set(obj, clone)
124
-
125
- // Iterate over the properties of the object
126
- for (const key in obj) {
127
- if (Object.prototype.hasOwnProperty.call(obj, key) && !exclude.includes(key)) {
128
- const value = obj[key]
129
-
130
- if (isDOMNode(value)) {
131
- // Skip cloning for DOM nodes
132
- clone[key] = value
133
- } else if (key === 'extend' && isArray(value)) {
134
- clone[key] = mergeArray(value, exclude)
135
- } else if (isObjectLike(value)) {
136
- clone[key] = deepClone(value, exclude, cleanUndefined, visited)
137
- } else {
138
- clone[key] = value
139
- }
140
- }
103
+ // Handle non-object types and special cases
104
+ if (!isObjectLike(obj) || isDOMNode(obj)) {
105
+ return obj
141
106
  }
142
107
 
143
- return clone
144
- }
145
- // export const deepClone = (obj, excludeFrom = [], cleanUndefined = false) => {
146
- // const o = isArray(obj) ? [] : {}
147
- // for (const prop in obj) {
148
- // if (!Object.prototype.hasOwnProperty.call(obj, prop)) continue
149
- // // if (prop === 'node' || prop === 'parent' || prop === 'root' || prop === '__element') {
150
- // // console.warn('recursive clonning is called', obj)
151
- // // continue
152
- // // }
153
- // if (prop === '__proto__') continue
154
- // if (excludeFrom.includes(prop) || prop.startsWith('__')) continue
155
- // let objProp = obj[prop]
156
- // if (cleanUndefined && isUndefined(objProp)) continue
157
- // if (prop === 'extend' && isArray(objProp)) {
158
- // objProp = mergeArray(objProp)
159
- // }
160
- // if (isObjectLike(objProp)) {
161
- // // queueMicrotask(() => {
162
- // o[prop] = deepClone(objProp, excludeFrom, cleanUndefined)
163
- // // })
164
- // } else o[prop] = objProp
165
- // }
166
- // return o
167
- // }
168
-
169
- /**
170
- * Deep cloning of object
171
- */
172
- export const deepCloneWithExtend = (obj, excludeFrom = ['node'], options = {}, visited = new WeakSet()) => {
173
- // Check if the value is object-like before trying to track it in visited
174
- if (isObjectLike(obj)) {
175
- if (visited.has(obj)) {
176
- return obj // Return the object if it was already cloned
177
- }
178
- visited.add(obj) // Add to visited set only if it's an object
108
+ // Handle circular references
109
+ if (visited.has(obj)) {
110
+ return visited.get(obj)
179
111
  }
180
112
 
181
- const o = options.window
113
+ // Create appropriate container based on type and window context
114
+ const clone = targetWindow
182
115
  ? isArray(obj)
183
- ? new options.window.Array([])
184
- : new options.window.Object({})
116
+ ? new targetWindow.Array()
117
+ : new targetWindow.Object()
185
118
  : isArray(obj)
186
119
  ? []
187
120
  : {}
188
121
 
189
- for (const prop in obj) {
190
- if (!Object.prototype.hasOwnProperty.call(obj, prop)) continue
122
+ // Store the clone to handle circular references
123
+ visited.set(obj, clone)
191
124
 
192
- const objProp = obj[prop]
125
+ // Clone properties
126
+ for (const key in obj) {
127
+ if (!Object.prototype.hasOwnProperty.call(obj, key)) continue
128
+
129
+ // Skip excluded properties
130
+ if (exclude.includes(key) || key.startsWith('__') || key === '__proto__') continue
193
131
 
194
- if (
195
- excludeFrom.includes(prop) ||
196
- prop.startsWith('__') ||
197
- (options.cleanUndefined && isUndefined(objProp)) ||
198
- (options.cleanNull && isNull(objProp))
199
- ) {
132
+ const value = obj[key]
133
+
134
+ // Skip based on cleanup options
135
+ if ((cleanUndefined && isUndefined(value)) || (cleanNull && isNull(value))) continue
136
+
137
+ // Handle special cases
138
+ if (isDOMNode(value)) {
139
+ clone[key] = value
140
+ continue
141
+ }
142
+
143
+ // Handle 'extend' array if enabled
144
+ if (handleExtend && key === 'extend' && isArray(value)) {
145
+ clone[key] = mergeArray(value, exclude)
200
146
  continue
201
147
  }
202
148
 
203
- if (isObjectLike(objProp)) {
204
- o[prop] = deepCloneWithExtend(objProp, excludeFrom, options, visited)
205
- } else if (isFunction(objProp) && options.window) {
206
- o[prop] = (options.window || window).eval('(' + objProp.toString() + ')')
149
+ // Handle functions in cross-frame scenario
150
+ if (isFunction(value) && targetWindow) {
151
+ clone[key] = targetWindow.eval('(' + value.toString() + ')')
152
+ continue
153
+ }
154
+
155
+ // Recursively clone objects
156
+ if (isObjectLike(value)) {
157
+ clone[key] = deepClone(value, {
158
+ ...options,
159
+ visited
160
+ })
207
161
  } else {
208
- o[prop] = objProp
162
+ clone[key] = value
209
163
  }
210
164
  }
211
165
 
212
- return o
166
+ return clone
213
167
  }
214
168
 
215
169
  /**
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@domql/utils",
3
- "version": "2.5.156",
3
+ "version": "2.5.161",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "index.js",
7
7
  "main": "index.js",
8
8
  "exports": {
9
9
  ".": {
10
- "kalduna": "./index.js",
10
+ "kalduna": "./dist/esm/index.js",
11
11
  "default": "./dist/cjs/index.js",
12
- "import": "./index.js",
12
+ "import": "./dist/esm/index.js",
13
13
  "require": "./dist/cjs/index.js"
14
14
  }
15
15
  },
@@ -22,10 +22,10 @@
22
22
  "copy:package:cjs": "cp ../../build/package-cjs.json dist/cjs/package.json",
23
23
  "build:esm": "npx esbuild *.js --target=es2017 --format=esm --outdir=dist/esm",
24
24
  "build:cjs": "npx esbuild *.js --target=node16 --format=cjs --outdir=dist/cjs",
25
- "build": "yarn build:cjs",
26
- "prepublish": "rimraf -I dist && yarn build && yarn copy:package:cjs"
25
+ "build": "yarn build:cjs; yarn build:esm",
26
+ "prepublish": "rimraf -I dist; yarn build; yarn copy:package:cjs"
27
27
  },
28
- "gitHead": "7e5943ccb3e1b2dd5386d2cfb31474ef446e1521",
28
+ "gitHead": "39a7af3d77c6b0257d5e1a0d6110850926d83c19",
29
29
  "devDependencies": {
30
30
  "@babel/core": "^7.12.0"
31
31
  }