Release v2.2.0
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
// Shared placeholder parsing and resolution helpers for browser-rendered API content.
|
||||
|
||||
(function () {
|
||||
var root = window;
|
||||
var transformPattern = /^(upper|lower|title)\(\)$/i;
|
||||
|
||||
function resolvePath(value, path) {
|
||||
var current = value;
|
||||
if (!path) {
|
||||
return current;
|
||||
}
|
||||
|
||||
String(path).split('.').forEach(function (segment) {
|
||||
if (current === undefined || current === null) {
|
||||
current = '';
|
||||
return;
|
||||
}
|
||||
|
||||
current = current[segment];
|
||||
});
|
||||
|
||||
return current === undefined || current === null ? '' : current;
|
||||
}
|
||||
|
||||
function parsePlaceholderExpression(expression) {
|
||||
var raw = String(expression || '').trim();
|
||||
var segments = raw ? raw.split('.') : [];
|
||||
var transforms = [];
|
||||
|
||||
while (segments.length) {
|
||||
var candidate = String(segments[segments.length - 1] || '').trim();
|
||||
if (!transformPattern.test(candidate)) {
|
||||
break;
|
||||
}
|
||||
|
||||
transforms.unshift(candidate.replace(/\(\)$/g, '').toLowerCase());
|
||||
segments.pop();
|
||||
}
|
||||
|
||||
return {
|
||||
path: segments.join('.'),
|
||||
transforms: transforms
|
||||
};
|
||||
}
|
||||
|
||||
function applyTransform(value, transform) {
|
||||
var text = String(value === undefined || value === null ? '' : value);
|
||||
|
||||
if (transform === 'lower') {
|
||||
return text.toLowerCase();
|
||||
}
|
||||
|
||||
if (transform === 'upper') {
|
||||
return text.toUpperCase();
|
||||
}
|
||||
|
||||
if (transform === 'title') {
|
||||
return text.toLowerCase().replace(/\b([a-z])/g, function (match, letter) {
|
||||
return String(letter || '').toUpperCase();
|
||||
});
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
function resolvePlaceholderExpression(value, expression) {
|
||||
var parsed = parsePlaceholderExpression(expression);
|
||||
var resolved = resolvePath(value, parsed.path);
|
||||
|
||||
parsed.transforms.forEach(function (transform) {
|
||||
resolved = applyTransform(resolved, transform);
|
||||
});
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
function formatPlaceholderValue(value) {
|
||||
if (value === undefined || value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (typeof value === 'object') {
|
||||
try {
|
||||
return JSON.stringify(value);
|
||||
} catch (_error) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
return String(value);
|
||||
}
|
||||
|
||||
function collectPlaceholderFieldPaths(value) {
|
||||
var output = [];
|
||||
|
||||
function walk(currentValue, prefix) {
|
||||
if (!currentValue || typeof currentValue !== 'object') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Array.isArray(currentValue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Object.keys(currentValue).forEach(function (key) {
|
||||
var nextPath = prefix ? prefix + '.' + key : key;
|
||||
var nextValue = currentValue[key];
|
||||
|
||||
if (nextValue && typeof nextValue === 'object') {
|
||||
walk(nextValue, nextPath);
|
||||
return;
|
||||
}
|
||||
|
||||
if (output.indexOf(nextPath) === -1) {
|
||||
output.push(nextPath);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
walk(value, '');
|
||||
return output;
|
||||
}
|
||||
|
||||
root.placeholderUtils = {
|
||||
resolvePath: resolvePath,
|
||||
parsePlaceholderExpression: parsePlaceholderExpression,
|
||||
resolvePlaceholderExpression: resolvePlaceholderExpression,
|
||||
formatPlaceholderValue: formatPlaceholderValue,
|
||||
collectPlaceholderFieldPaths: collectPlaceholderFieldPaths
|
||||
};
|
||||
}());
|
||||
Reference in New Issue
Block a user