timepiece/node_modules/date-fns/locale/_lib/buildMatchFn.mjs

58 lines
1.5 KiB
JavaScript

export function buildMatchFn(args) {
return (string, options = {}) => {
const width = options.width;
const matchPattern =
(width && args.matchPatterns[width]) ||
args.matchPatterns[args.defaultMatchWidth];
const matchResult = string.match(matchPattern);
if (!matchResult) {
return null;
}
const matchedString = matchResult[0];
const parsePatterns =
(width && args.parsePatterns[width]) ||
args.parsePatterns[args.defaultParseWidth];
const key = Array.isArray(parsePatterns)
? findIndex(parsePatterns, (pattern) => pattern.test(matchedString))
: // eslint-disable-next-line @typescript-eslint/no-explicit-any -- I challange you to fix the type
findKey(parsePatterns, (pattern) => pattern.test(matchedString));
let value;
value = args.valueCallback ? args.valueCallback(key) : key;
value = options.valueCallback
? // eslint-disable-next-line @typescript-eslint/no-explicit-any -- I challange you to fix the type
options.valueCallback(value)
: value;
const rest = string.slice(matchedString.length);
return { value, rest };
};
}
function findKey(object, predicate) {
for (const key in object) {
if (
Object.prototype.hasOwnProperty.call(object, key) &&
predicate(object[key])
) {
return key;
}
}
return undefined;
}
function findIndex(array, predicate) {
for (let key = 0; key < array.length; key++) {
if (predicate(array[key])) {
return key;
}
}
return undefined;
}