1.0.0 • Published 10 years ago

match.js v1.0.0

Weekly downloads
19
License
GPL-3.0
Repository
github
Last release
10 years ago

Match.js

GitHub stars GitHub forks npm GitHub issues Maintainer

Readme

Code Climate Test Coverage Issue Count Build Status David

Match values like a Boss

Install

npm

npm install --save match.js

Usage

ES7:

import match from 'match.js';
// or
import { match, matchType } from 'match.js';

ES6:

const { match } = require('match.js');

const P_INIT = 1;
const P_READY = 2;
const P_PENDING = 3;
const P_SUCCESS = 4;
const P_FAIL = 9;


const result = (function someFunction(){ return P_SUCCESS; })();

match(result, {
  [P_PENDING]() {
    console.info('Please, wait');
  },

  [P_SUCCESS]() {
    console.log('Success! Let\'s work.');
  },

  [P_FAIL](value, variants) {
    console.error(`Failed with ${value}`, variants);
  }
}, () => console.warning('Default isn\t present')); // default value

// Success! Let's work.

ES5:

var match = require('match.js');

var source = 'value';
var target = match.matchType(source, {
  number: 5 + 12,
  string: 'some',
  function: function(){ return function(){} },
  boolean: false,
}, 'default');

console.log(target); // some

Example

import match, { matchType, matchStrict, matchRegexp } from 'match.js';

// match by value, default is `null`
const result = match(20, {
  10: 'first',
  20: () => match('string', {
    string: 200
  })
});

console.log(result); // 200

// match by type, default is `default`
const result2 = matchType('hello', {
  string: 'that string',
  number: 'that number',
}, 'default');

console.log(result2); // that string


// Do not call function
const result3 = matchStrict('our', {
  your: function() { return 11; },
  my: function() { return 22; },
  our: function() { return 33; },
});

console.log(result3); // function() { return 33; }
console.log(result3()); // 33


const result4 = match('second', {
  first: 1,
  second: function() {
    return 2;
  }
});

console.log(result4); // 2


const result5 = matchRegexp('hi all', {
  '^[a-z]+': true
}, false, 'i');

License

license

1.0.0

10 years ago

0.3.8

10 years ago

0.3.6

10 years ago

0.3.5

10 years ago

0.3.4

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.4

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.0-reserved

10 years ago

0.0.1

10 years ago