Skip to content

Instantly share code, notes, and snippets.

@titouancreach
Created June 20, 2018 09:18
Show Gist options
  • Save titouancreach/4894ac13d04338159da0816fa3016cb1 to your computer and use it in GitHub Desktop.
Save titouancreach/4894ac13d04338159da0816fa3016cb1 to your computer and use it in GitHub Desktop.
const matchString = (s, patterns) => {
if (patterns.length === 0) {
throw new Error('Not exhaustive string matching');
}
const [matching, ...rest] = patterns;
const [re, fn] = matching;
if (re.test(s)) {
const matches = re.exec(s);
return matches ? fn(...matches) : fn();
}
return matchString(s, rest);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment