Giter Site home page Giter Site logo

xmatch's Introduction

xmatch

Simple pattern matching for ES6 (no transpilation!)

Property matching

const { match } = require('xmatch');

match(obj, [
	({ x }) => console.log('x', x),
	({ y }) => console.log('y', y),
	({ z }) => console.log('z', z),
	// Exhaustive match; will throw `xmatch.UnmatchedPatternError` unless uncommented:
	// other => console.error('Something else', other),
]);

Iterable matching

const { match } = require('xmatch');

match(arr, [
	([]) => 'empty',
	([x]) => `x=${x}`,
	([x, y]) => `x=${x},y=${y}`,
	([x, y, ...{ length }]) => `x=${x},y=${y},rest.length=${length}`,
]);

Custom guards

const { match, guard } = require('xmatch');

match(obj, [
	({ command }) => {
		// When you want to match simple values:
		guard(command === 'ignore');
		/* Do nothing */
	},
	({ command }) => {
		// Or, say, match result of regex:
		let [, name, args] = guard(command.match(/^(\w+):(.*)$/));
		console.log({ name, args });
	},
	({ command }) => {
		throw new Error(`Invalid command: ${command}`);
	},
]);

Shape assertions

const { guard } = require('xmatch');

const { x, y } = guard({ x: 1, y: 2 }); // OK
const { x, y } = guard({ x: 1, z: 2 }); // throws `xmatch.UnmatchedPatternError`

Known issues

  • You can't use literals directly in patterns (this is limitation of ES6 syntax, can be fixed as part of https://github.com/tc39/proposal-pattern-matching).
  • You can't use default values for parameters. This is limitation of the way matching is implemented, and you'll have to resolve defaults yourself if that's what you want.
  • Nested match will propagate to outer matches just like guards, causing "parent" branches to be unmatched. I consider this as a feature, but let me know if you think a different behaviour makes sense.
  • Trying to further destructure or access undefined propertie of an object will also trigger the match guard (#1). This is tricky to workaround without changing the syntax, but I'll look into it (and happy to hear any suggestions).
  • This uses dynamic metaprogramming via Proxy which might have undesirable performance effect on hot code paths. If your benchmarks suggest it's causing critical performance issues, consider using transpiler plugins instead.
  • Proxy is not implemented in pre-ES6 browsers and can't be polyfilled, so use this only if you're okay with the supported target set: https://caniuse.com/#feat=proxy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.