Skip to content

Commit 60e1599

Browse files
committed
fix: renamed from utility to operators.js
1 parent d7fd518 commit 60e1599

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

src/utility.js renamed to src/operators.js

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,70 +131,123 @@ function processOperators(element, value, exclude = [], parent) {
131131
}
132132

133133
/**
134-
* Synchronously determines and executes the action for a single operator token.
135-
* @returns {string} The final string value for the token.
134+
/**
135+
* Synchronously determines and executes the action for processing a single operator token.
136+
* @param {HTMLElement|null} element - The context element from which to derive values or execute methods.
137+
* @param {string} operator - The operator to apply, indicating what actions or property/method to evaluate.
138+
* @param {string|Array} args - Arguments that may be used by the operator, which could be further processed if they contain a nested operator.
139+
* @param {string} parent - Context in which the function is called, potentially affecting behavior or processing.
140+
* @returns {string} The final resolved value after applying the operator to the given elements.
136141
*/
137142
function resolveOperator(element, operator, args, parent) {
143+
// If args contain any operators (indicated by '$'), process them recursively
138144
if (args && args.includes("$")) {
145+
// Reprocess args to resolve any nested operators
139146
args = processOperators(element, args, "", operator);
140147
}
141148

149+
// Initialize an array of elements to operate on, starting with the single element reference if provided
142150
let targetElements = element ? [element] : [];
151+
152+
// If args are provided as a string, treat it as a selector to find applicable target elements
143153
if (args && typeof args === "string") {
144154
targetElements = queryElements({
145-
element,
146-
selector: args
155+
element, // Use the context element as the base for querying
156+
selector: args // Selector from args to find matching elements
147157
});
158+
159+
// If no elements are found matching the selector in args, return args unmodified
148160
if (!targetElements.length) return args;
149161
}
150162

163+
// Generate a processed value by applying the operator to each of the target elements
151164
let value = processValues(targetElements, operator, args, parent);
165+
166+
// If the result is a string and still contains unresolved operators, process them further
152167
if (value && typeof value === "string" && value.includes("$")) {
168+
// Resolve any remaining operators within the value string
153169
value = processOperators(element, value, parent);
154170
}
155171

172+
// Return the final processed value, fully resolved
156173
return value;
157174
}
158175

159176
/**
160-
* Synchronously aggregates values.
161-
* @returns {string} The aggregated string value.
177+
* Synchronously processes and aggregates values from a set of elements based on a specified operator.
178+
* @param {Array<HTMLElement>} elements - Array of elements to be processed.
179+
* @param {string} operator - The operator to apply to each element, indicating which property or method to use.
180+
* @param {string|Array} args - Arguments that may be passed to the method if the operator corresponds to a function.
181+
* @param {string} parent - Context in which the function is called, possibly influencing behavior (e.g., special handling for "$param").
182+
* @returns {string} The combined string value obtained by processing elements with the specified operator.
162183
*/
163184
function processValues(elements, operator, args, parent) {
185+
// Attempt to fetch a custom operator function associated with the operator
164186
let customOp = customOperators.get(operator);
187+
188+
// Initialize an empty string to accumulate results from processing each element
165189
let aggregatedString = "";
190+
191+
// Iterate over each element in the provided elements array
166192
for (const el of elements) {
193+
// If the element is null or undefined, skip to the next iteration
167194
if (!el) continue;
195+
196+
// Determine the raw value from the custom operator or by accessing a property/method directly on the element
168197
let rawValue = customOp || el?.[operator.substring(1)];
198+
199+
// Check if the rawValue is a function and process it using provided arguments
169200
if (typeof rawValue === "function") {
201+
// If arguments are provided as an array
170202
if (Array.isArray(args)) {
203+
// If there are arguments, exit by returning an empty string (assumes args should not be used here)
171204
if (args.length) {
172205
return "";
173206
}
207+
// Invoke the function using the element and spread array arguments
174208
rawValue = rawValue(el, ...args);
175209
} else {
210+
// Otherwise, invoke the function using the element and direct arguments
176211
rawValue = rawValue(el, args);
177212
}
178213
}
179214

215+
// If the parent context requires parameter resolution
180216
if (parent === "$param") {
217+
// Return the first evaluated rawValue that is not null or undefined
181218
if (rawValue) {
182219
return rawValue;
183220
}
184221
} else {
222+
// Otherwise, append the stringified rawValue to the aggregated result, defaulting to an empty string if it's nullish
185223
aggregatedString += String(rawValue ?? "");
186224
}
187225
}
188226

227+
// Return the final aggregated string containing all processed values
189228
return aggregatedString;
190229
}
191230

231+
/**
232+
* Extracts the subdomain from the current window's hostname.
233+
* @returns {string|null} - The subdomain part of the hostname if it exists, or null if there is none.
234+
*/
192235
function getSubdomain() {
236+
// Retrieve the hostname from the current window's location
193237
const hostname = window.location.hostname;
238+
239+
// Split the hostname into parts divided by dots ('.')
194240
const parts = hostname.split(".");
241+
242+
// Check if the hostname has more than two parts and ensure the last part isn't a number (a common TLD check)
243+
// A typical domain structure might look like "sub.domain.com",
244+
// where "sub" is the subdomain, "domain" is the second-level domain, and "com" is the top-level domain.
195245
if (parts.length > 2 && isNaN(parseInt(parts[parts.length - 1]))) {
246+
// Join all parts except the last two (which are assumed to be the domain and TLD) to get the subdomain
196247
return parts.slice(0, parts.length - 2).join(".");
197248
}
249+
250+
// Return null if there's no valid subdomain structure
198251
return null;
199252
}
200253

0 commit comments

Comments
 (0)