@jmespath-community/jmespath
Version:
Typescript implementation of the JMESPath Community specification
92 lines • 3.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.upper = exports.trimRight = exports.trimLeft = exports.trim = exports.split = exports.replace = exports.padRight = exports.padLeft = exports.lower = exports.findLast = exports.findFirst = void 0;
const _1 = require(".");
const findFirst = (subject, sub, start, end) => {
if (!subject || !sub) {
return null;
}
start = Math.max((0, _1.ensureInteger)((start = start || 0)), 0);
end = Math.min((0, _1.ensureInteger)((end = end || subject.length)), subject.length);
const offset = subject.slice(start, end).indexOf(sub);
return offset === -1 ? null : offset + start;
};
exports.findFirst = findFirst;
const findLast = (subject, sub, start, end) => {
if (!subject || !sub) {
return null;
}
start = Math.max((0, _1.ensureInteger)((start = start || 0)), 0);
end = Math.min((0, _1.ensureInteger)((end = end || subject.length)), subject.length);
const offset = subject.slice(start, end).lastIndexOf(sub);
const result = offset === -1 ? null : offset + start;
return result;
};
exports.findLast = findLast;
const lower = (subject) => subject.toLowerCase();
exports.lower = lower;
const ensurePadFuncParams = (name, width, padding) => {
padding = padding || ' ';
if (padding.length > 1) {
throw new Error(`invalid value, ${name} expects its 'pad' parameter to be a valid string with a single codepoint`);
}
(0, _1.ensurePositiveInteger)(width);
return padding;
};
const padLeft = (subject, width, padding) => {
padding = ensurePadFuncParams('pad_left', width, padding);
return (subject && subject.padStart(width, padding)) || '';
};
exports.padLeft = padLeft;
const padRight = (subject, width, padding) => {
padding = ensurePadFuncParams('pad_right', width, padding);
return (subject && subject.padEnd(width, padding)) || '';
};
exports.padRight = padRight;
const replace = (subject, string, by, count) => {
if (count === 0) {
return subject;
}
if (!count) {
// emulating es2021: String.prototype.replaceAll()
return subject.split(string).join(by);
}
(0, _1.ensurePositiveInteger)(count);
[...Array(count).keys()].map(() => (subject = subject.replace(string, by)));
return subject;
};
exports.replace = replace;
const split = (subject, search, count) => {
if (subject.length == 0 && search.length === 0) {
return [];
}
if (count === null || count === undefined) {
return subject.split(search);
}
(0, _1.ensurePositiveInteger)(count);
if (count === 0) {
return [subject];
}
const split = subject.split(search);
return [...split.slice(0, count), split.slice(count).join(search)];
};
exports.split = split;
const trim = (subject, chars) => {
return (0, exports.trimLeft)((0, exports.trimRight)(subject, chars), chars);
};
exports.trim = trim;
const trimLeft = (subject, chars) => {
return trimImpl(subject, list => new RegExp(`^[${list}]*(.*?)`), chars);
};
exports.trimLeft = trimLeft;
const trimRight = (subject, chars) => {
return trimImpl(subject, list => new RegExp(`(.*?)[${list}]*\$`), chars);
};
exports.trimRight = trimRight;
const trimImpl = (subject, regExper, chars) => {
const pattern = chars ? chars.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') : '\\s\u0085';
return subject.replace(regExper(pattern), '$1');
};
const upper = (subject) => subject.toUpperCase();
exports.upper = upper;
//# sourceMappingURL=strings.js.map