buildMatchPatternFn.js 621 B

1234567891011121314151617181920
  1. export function buildMatchPatternFn(args) {
  2. return (string, options = {}) => {
  3. const matchResult = string.match(args.matchPattern);
  4. if (!matchResult) return null;
  5. const matchedString = matchResult[0];
  6. const parseResult = string.match(args.parsePattern);
  7. if (!parseResult) return null;
  8. let value = args.valueCallback
  9. ? args.valueCallback(parseResult[0])
  10. : parseResult[0];
  11. // [TODO] I challenge you to fix the type
  12. value = options.valueCallback ? options.valueCallback(value) : value;
  13. const rest = string.slice(matchedString.length);
  14. return { value, rest };
  15. };
  16. }