Skip to content

Commit

Permalink
Merge pull request #55 from tristaaan/regexp-pick
Browse files Browse the repository at this point in the history
RegExp pick
  • Loading branch information
tjmehta committed Feb 15, 2015
2 parents ac78e89 + 01ab9cf commit f128ffa
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 6 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,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 Expand Up @@ -411,7 +421,7 @@ var passAny = require('101/pass-any');
## pick

Returns a new object with the specified keys (with key values from obj).
Supports partial functionality (great with array functions, like map).
Supports regular expressions and partial functionality (great with array functions, like map).

```js
var pick = require('101/pick');
Expand All @@ -421,6 +431,7 @@ var obj = {
};

pick(obj, 'foo'); // { foo: 1 }
pick(obj, RegExp('oo$')); // { foo: 1 }
pick(obj, ['foo']); // { foo: 1 }
pick(obj, ['foo', 'bar']); // { foo: 1, bar: 2 }

Expand Down
15 changes: 15 additions & 0 deletions is-regexp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @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
*/

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "common javascript utils that can be required selectively that assume es5+",
"main": "index.js",
"scripts": {
"test": "lab -c test",
"test": "lab -c -t 100 test",
"test-watch": "nodemon --exec lab -c test"
},
"repository": {
Expand Down
17 changes: 13 additions & 4 deletions pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
*/

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

/**
* Returns a new object with the specified keys (with key values from obj).
* When only keys are specified pick returns a partial-function which accepts obj.
* @function module:101/pick
* @param {object} [obj] - object whose keys are picked
* @param {string|array} keys... - keys which will be taken from obj (can be specifieds as args (strings and/or arrays)
* @param {string|regexp|array} keys... - keys which will be taken from obj, can be specifieds as args (strings, regular epxressions, and/or arrays)
* @return {object|function} Object with only the keys specified from the original obj or Partial-function pick (which accepts obj) and returns an object
*/
module.exports = function () {
Expand Down Expand Up @@ -37,8 +38,16 @@ function pick (obj, args) {

function copy (from, to) {
return function (key) {
if (key in from) {
to[key] = from[key];
if (isRegExp(key)) {
Object.keys(from).forEach(function(keyFrom) {
if (key.test(keyFrom)) {
to[keyFrom] = from[keyFrom];
}
});
} else {
if (key in from) {
to[key] = from[key];
}
}
};
}
}
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();
});
});
90 changes: 90 additions & 0 deletions test/test-pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ describe('pick', function () {
});
expect(pick(obj)).to.eql({});
expect(pick(obj, [])).to.eql({});
expect(pick(obj, RegExp('a'))).to.eql({
bar: 1
});
expect(pick(obj, RegExp('a'), 'foo')).to.eql({
foo: 1,
bar: 1
});
expect(pick(obj, [RegExp('q|b')], 'bar')).to.eql({
bar: 1,
qux: 1
});
expect(pick(obj, [RegExp('q'), RegExp('f')], ['bar'])).to.eql({
foo: 1,
bar: 1,
qux: 1
});
expect(pick(obj, [RegExp('q'), 'foo'], 'bar')).to.eql({
foo: 1,
bar: 1,
qux: 1
});
expect(pick(obj, [RegExp('x$'), 'foo'], [RegExp('b')])).to.eql({
foo: 1,
bar: 1,
qux: 1
});
done();
});
it('should pick keys from objects in an array when used with map', function(done) {
Expand Down Expand Up @@ -135,6 +161,70 @@ describe('pick', function () {
bar: 3
}
]);
expect(objs.map(pick(RegExp('q|g')))).to.eql([
{},
{
qux: 2
},
{
goo: 3
}
]);
expect(objs.map(pick(RegExp('BAR', 'i'), 'foo'))).to.eql([
{
bar: 1
},
{
foo: 2,
bar: 2
},
{
foo: 3,
bar: 3
}
]);
expect(objs.map(pick([RegExp('b')], 'foo'))).to.eql([
{
bar: 1
},
{
foo: 2,
bar: 2
},
{
foo: 3,
bar: 3
}
]);
expect(objs.map(pick([RegExp('b'), 'qux'], ['foo']))).to.eql([
{
bar: 1
},
{
bar: 2,
foo: 2,
qux: 2
},
{
foo: 3,
bar: 3
}
]);
expect(objs.map(pick([RegExp('b'), RegExp('^f')], [RegExp('oo$')]))).to.eql([
{
bar: 1
},
{
bar: 2,
foo: 2
},
{
bar: 3,
foo: 3,
koo: 3,
goo: 3
}
]);
expect(objs.map(pick())).to.eql([
{}, {}, {}
]);
Expand Down

0 comments on commit f128ffa

Please sign in to comment.