123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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))
- : // [TODO] -- I challenge you to fix the type
- findKey(parsePatterns, (pattern) => pattern.test(matchedString));
- let value;
- value = args.valueCallback ? args.valueCallback(key) : key;
- value = options.valueCallback
- ? // [TODO] -- I challenge 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;
- }
|