Skip to content

Commit

Permalink
add is-regexp function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stoeffel committed Feb 11, 2015
1 parent eec0d12 commit b6f54d3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ var isObject = require('101/is-object');
[{}, { foo: 1 }, 100].map(isObject); // [true, true, false]
```

## isRegExp

Check if a value is an instance of RegExp

```js
var isRegExp = require('101/is-regexp');

[new RegExp('.*'), /.*/, {}, 1].map(isRegExp); // [true, true, false, false]
```

## isString

Functional version of val typeof 'string'
Expand Down
18 changes: 18 additions & 0 deletions is-regexp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @module 101/is-regexp
*/

/**
* Check if a value is an instance of RegExp
* @function module:101/is-regexp
* @param {*} val - value checked to be an instance of RegExp
* @return {boolean} Whether the value is an object or not
*/
var exists = require('./exists');

module.exports = function isRegExp (val) {
return typeof val === 'object' &&
exists(val) &&
Object.prototype.toString.call(val) == '[object RegExp]';
};

5 changes: 1 addition & 4 deletions pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var isObject = require('./is-object');
var isRegExp = require('./is-regexp');

/**
* Returns a new object with the specified keys (with key values from obj).
Expand Down Expand Up @@ -50,7 +51,3 @@ function copy (from, to) {
}
};
}

function isRegExp (obj) {
return Object.prototype.toString.call(obj) == '[object RegExp]';
}
30 changes: 30 additions & 0 deletions test/test-isregexp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var Lab = require('lab');
var lab = exports.lab = Lab.script();

var describe = lab.describe;
var it = lab.it;
var expect = Lab.expect;

var isRegExp = require('../is-regexp');

describe('isRegExp', function () {
it('should return true for instance of RegExp', function(done) {
var regexp = new RegExp('.*');
expect(isRegExp(regexp)).to.be.true;
expect(isRegExp(/.*/)).to.be.true;
done();
});

it('should return false for non-regexp', function(done) {
expect(isRegExp({})).to.be.false;
expect(isRegExp(['foo'])).to.be.false;
expect(isRegExp('foo')).to.be.false;
expect(isRegExp(101)).to.be.false;
expect(isRegExp(function () {})).to.be.false;
expect(isRegExp(null)).to.be.false;
expect(isRegExp(undefined)).to.be.false;
expect(isRegExp(new String('hey'))).to.be.false;
expect(isRegExp(new Number(101))).to.be.false;
done();
});
});

0 comments on commit b6f54d3

Please sign in to comment.