├── .editorconfig ├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE ├── README.markdown ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | charset = utf-8 7 | 8 | [*] 9 | end_of_line = lf 10 | insert_final_newline = true 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #### joe made this: https://goel.io/joe 2 | 3 | #####=== Node ===##### 4 | 5 | # Logs 6 | logs 7 | *.log 8 | 9 | # Runtime data 10 | pids 11 | *.pid 12 | *.seed 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directory 30 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 31 | node_modules 32 | 33 | # Debug log from npm 34 | npm-debug.log 35 | 36 | #####=== OSX ===##### 37 | .DS_Store 38 | .AppleDouble 39 | .LSOverride 40 | 41 | # Icon must end with two \r 42 | Icon 43 | 44 | # Thumbnails 45 | ._* 46 | 47 | # Files that might appear on external disk 48 | .Spotlight-V100 49 | .Trashes 50 | 51 | # Directories potentially created on remote AFP share 52 | .AppleDB 53 | .AppleDesktop 54 | Network Trash Folder 55 | Temporary Items 56 | .apdisk 57 | 58 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "curly": true, 6 | "eqeqeq": true, 7 | "eqnull": true, 8 | "immed": true, 9 | "latedef": "nofunc", 10 | "newcap": false, 11 | "noarg": true, 12 | "undef": true, 13 | "strict": false, 14 | "quotmark": "single", 15 | "laxbreak": true, 16 | "globals" : { 17 | "describe" : false, 18 | "expect" : false, 19 | "it" : false, 20 | "before" : false, 21 | "beforeEach" : false, 22 | "after" : false, 23 | "afterEach" : false 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - 'iojs' 5 | - '0.12' 6 | - '0.10' 7 | before_install: 8 | - "export DISPLAY=:99.0" 9 | - "sh -e /etc/init.d/xvfb start" 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 hemanth.hm 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # node-regexify 2 | 3 | [![Build Status](https://travis-ci.org/regexps/regexify.svg)](https://travis-ci.org/regexps/regexify) [![npm version](https://badge.fury.io/js/regexify.svg)](http://badge.fury.io/js/regexify) 4 | > Regex everything! Well...almost ;) 5 | 6 | ## Installation 7 | 8 | `npm install regexify --save` 9 | 10 | ## Example usage: 11 | 12 | ```js 13 | var regexify = require('regexify'); 14 | var assert = require('assert'); 15 | 16 | assert.deepEqual(regexify(undefined), /.^/); 17 | assert.deepEqual(regexify(false), /.^/); 18 | assert.deepEqual(regexify(null), /.^/); 19 | assert.deepEqual(regexify(''), /.^/); 20 | assert.deepEqual(regexify(['foo', 'bar']), /foo|bar/); 21 | assert.deepEqual(regexify(['foo', './bar']), /foo|\.\/bar/); 22 | assert.deepEqual(regexify('./foobar'),/\.\/foobar/); 23 | assert.deepEqual(regexify('foobar'), /foobar/); 24 | assert.deepEqual(regexify(/foobar/), /foobar/); 25 | ``` 26 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // Inspiration from 6to5 util belt. 2 | 3 | 'use strict'; 4 | 5 | var util = require('util') 6 | ,isString = function (val) { 7 | return (Object.prototype.toString.call(val) === '[object String]'); 8 | } 9 | ,escapeStringRegexp = function(str){ 10 | return String(str).replace(/([.*+?=^!:${}()|[\]\/\\])/g, '\\$1'); 11 | }; 12 | 13 | function regexify(val){ 14 | if (!val) { 15 | return new RegExp(/.^/); 16 | } 17 | if (Array.isArray(val)) { 18 | val = val.map(function(val){ 19 | return escapeStringRegexp(val) 20 | }).join('|'); 21 | return new RegExp(val); 22 | } 23 | if (isString(val)) { 24 | return new RegExp(escapeStringRegexp(val)); 25 | } 26 | if (util.isRegExp(val)) { 27 | return val; 28 | } 29 | throw new TypeError('illegal type: '+ val +' for regexify'); 30 | } 31 | 32 | module.exports = regexify; 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "regexify", 3 | "version": "2.2.0", 4 | "description": "Regex everything!", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/hemanth/regexify.git" 12 | }, 13 | "keywords": [ 14 | "regex", 15 | "util" 16 | ], 17 | "contributors": [ 18 | "hemanth.hm (http://www.h3manth.com)" 19 | ], 20 | "files": [ 21 | "index.js" 22 | ], 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/hemanth/regexify/issues" 26 | }, 27 | "homepage": "https://github.com/hemanth/regexify", 28 | "devDependencies": { 29 | "mocha": "^2.1.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var regexify = require('./index'); 4 | 5 | it('should check for all variations...', function () { 6 | assert.deepEqual(regexify(undefined), /.^/); 7 | assert.deepEqual(regexify(false), /.^/); 8 | assert.deepEqual(regexify(null), /.^/); 9 | assert.deepEqual(regexify(''), /.^/); 10 | assert.deepEqual(regexify(['foo', 'bar']), /foo|bar/); 11 | assert.deepEqual(regexify(['foo', './bar']), /foo|\.\/bar/); 12 | assert.deepEqual(regexify('foobar'), /foobar/); 13 | assert.deepEqual(regexify('./foobar'),/\.\/foobar/); 14 | assert.deepEqual(regexify(/foobar/), /foobar/); 15 | assert.throws(function () { 16 | regexify({}); 17 | }, /illegal type:/); 18 | }); 19 | --------------------------------------------------------------------------------