buildMatchPatternFn.cjs 680 B

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