This is a Babel Plugin intended to make JS-Pattern-Matching compatible with Babel (and, therefore, with non-ES2015 environments)
import match from 'js-pattern-matching';
const sum = (list) => match (list) (
([x,...xs]) => x + sum(xs),
([]) => 0
)
console.log(sum([]));
// prints 0
console.log(sum([1,2,3]));
// prints 6
npm install --save-dev babel-plugin-js-pattern-matching
To use the plugin, first install it with the above command.
To use JS-Pattern-Matching together with Babel, just add the plugin to your .babelrc
file:
{
"presets": [
"es2015"
],
"plugins": [
"babel-plugin-js-pattern-matching",
]
}
The plugin currently relies on the match
function being imported and used with that name. If you import it using another name, it will probably not work. Support for custom names is planned for the next release.