├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '8' 5 | - '6' 6 | - '4' 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const isGlob = require('is-glob'); 3 | const isRegex = require('is-regex'); 4 | const micromatch = require('micromatch'); 5 | 6 | module.exports = (val, condition) => { 7 | if (typeof condition === 'boolean') { 8 | return condition; 9 | } 10 | 11 | if (typeof condition === 'function') { 12 | return condition(val) === true; 13 | } 14 | 15 | if (isRegex(condition)) { 16 | return condition.test(val); 17 | } 18 | 19 | if (isGlob(condition)) { 20 | return micromatch.isMatch(val, condition); 21 | } 22 | 23 | return val === condition; 24 | }; 25 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Kevin Mårtensson (github.com/kevva) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "match-condition", 3 | "version": "1.1.1", 4 | "description": "Check if a value matches a condition", 5 | "license": "MIT", 6 | "repository": "kevva/match-condition", 7 | "author": { 8 | "name": "Kevin Martensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "check", 23 | "conditional", 24 | "function", 25 | "glob", 26 | "if", 27 | "match", 28 | "regex", 29 | "string", 30 | "value" 31 | ], 32 | "dependencies": { 33 | "is-glob": "^4.0.0", 34 | "is-regex": "^1.0.3", 35 | "micromatch": "^3.1.6" 36 | }, 37 | "devDependencies": { 38 | "ava": "*", 39 | "xo": "*" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # match-condition [![Build Status](https://travis-ci.org/kevva/match-condition.svg?branch=master)](https://travis-ci.org/kevva/match-condition) 2 | 3 | > Check if a value matches a condition 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install match-condition 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const matchCondition = require('match-condition'); 17 | 18 | matchCondition('unicorns', 'unicorns'); 19 | //=> true 20 | 21 | matchCondition('unicorns', /^unicorns$/); 22 | //=> true 23 | 24 | matchCondition('unicorns', '*corns'); 25 | //=> true 26 | 27 | matchCondition('unicorns' val => val === 'unicorns'); 28 | //=> true 29 | ``` 30 | 31 | 32 | ## API 33 | 34 | ### matchCondition(value, condition) 35 | 36 | #### value 37 | 38 | Type: `string` `number` 39 | 40 | Value to be matched. 41 | 42 | #### condition 43 | 44 | Type: `Function` `string` `boolean` `RegExp` 45 | 46 | Condition to match the value against. 47 | 48 | 49 | ## License 50 | 51 | MIT © [Kevin Martensson](http://github.com/kevva) 52 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import m from '.'; 3 | 4 | test(t => { 5 | t.true(m('abc', true)); 6 | t.true(m('abc', 'abc')); 7 | t.true(m('abc', /^abc$/)); 8 | t.true(m('abc', '*bc')); 9 | t.false(m('abc', false)); 10 | t.false(m('abc', 'dfg')); 11 | t.false(m('abc', /^dfg$/)); 12 | t.false(m('abc', '!abc')); 13 | t.true(m('abc', x => x === 'abc')); 14 | t.false(m('abc', x => x === 'dfg')); 15 | }); 16 | --------------------------------------------------------------------------------