Skip to content

Commit ae19c09

Browse files
authored
Merge pull request #23 from tjmehta/keys
keys, keysIn, values, valuesIn
2 parents d955d1f + 2261999 commit ae19c09

14 files changed

+340
-21
lines changed

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Functional methods like forEach, map, filter, and other Array methods for Object
2323

2424
[forEach](https://github.com/tjmehta/object-loops#forEach)
2525

26+
[keys](https://github.com/tjmehta/object-loops#keys)
27+
28+
[keysIn](https://github.com/tjmehta/object-loops#keysIn)
29+
2630
[mapKeys](https://github.com/tjmehta/object-loops#mapKeys)
2731

2832
[map](https://github.com/tjmehta/object-loops#map)
@@ -31,6 +35,10 @@ Functional methods like forEach, map, filter, and other Array methods for Object
3135

3236
[some](https://github.com/tjmehta/object-loops#some)
3337

38+
[values](https://github.com/tjmehta/object-loops#values)
39+
40+
[valuesIn](https://github.com/tjmehta/object-loops#valuesIn)
41+
3442
# Usage
3543

3644
#### You can require each method individually `object-loops/<loop>`
@@ -238,6 +246,44 @@ keyConcat // = 'foobarbaz'
238246
valSum // = 60
239247
```
240248

249+
## keys
250+
251+
Equivalent to `Object.keys`. Implemented specifically for chain.
252+
253+
```js
254+
var chain = require('object-loops/chain')
255+
256+
var obj = {
257+
foo: 10,
258+
bar: 20,
259+
baz: 30
260+
}
261+
chain(obj)
262+
.keys()
263+
.toJSON()
264+
// ['foo', 'bar', 'baz']
265+
```
266+
267+
## keysIn
268+
269+
Like to `keys`, but includes enumerable keys from the prototype chain.
270+
271+
```js
272+
var keysIn = require('object-loops/keys-in')
273+
274+
function Person (name) {
275+
this.name = name
276+
}
277+
Person.prototype.getName = function () {
278+
return this.name
279+
}
280+
281+
var person = new Person('foo')
282+
keysIn(obj)
283+
// ['name', 'getName']
284+
// for comparison, `keys` would return ['name']
285+
```
286+
241287
## mapKeys
242288

243289
Creates a new object with the results of calling a provided function on every key in the object.
@@ -345,6 +391,45 @@ anyGreaterThan25 // true
345391
*/
346392
```
347393

394+
## values
395+
396+
Like to `keys`, but returns direct enumerable values.
397+
398+
```js
399+
var values = require('object-loops/values')
400+
401+
function Person (name) {
402+
this.name = name
403+
}
404+
Person.prototype.getName = function () {
405+
return this.name
406+
}
407+
408+
var person = new Person('foo')
409+
values(obj)
410+
// ['foo']
411+
// for comparison, `valuesIn` would return ['foo', Person.prototype.getName]
412+
```
413+
414+
## valuesIn
415+
416+
Like to `keys`, but returns direct enumerable values including prototype chain.
417+
418+
```js
419+
var valuesIn = require('object-loops/values-in')
420+
421+
function Person (name) {
422+
this.name = name
423+
}
424+
Person.prototype.getName = function () {
425+
return this.name
426+
}
427+
428+
var person = new Person('foo')
429+
valuesIn(obj)
430+
// ['foo', Person.prototype.getName]
431+
```
432+
348433
## License
349434

350435
MIT

chain.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ function extendChainPrototype (hideWarnings) {
3636
'findKey',
3737
'find',
3838
'forEach',
39+
'keys',
40+
'keysIn',
3941
'mapKeys',
4042
'map',
4143
'reduce',
42-
'some'
44+
'some',
45+
'values',
46+
'valuesIn'
4347
].forEach(function (methodName) {
4448
var filename = dasherize(methodName)
4549
var filepath = path.join(__dirname, filename)
@@ -58,8 +62,7 @@ function extendChainPrototype (hideWarnings) {
5862

5963
return this
6064
},
61-
enumerable: false,
62-
configurable: envIs('test') // hack for tests
65+
enumerable: false
6366
})
6467
})
6568
}

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ function extendObjectPrototype (hideWarnings) {
2121
'findKey',
2222
'find',
2323
'forEach',
24+
'keys',
25+
'keysIn',
2426
'mapKeys',
2527
'map',
2628
'reduce',
27-
'some'
29+
'some',
30+
'values',
31+
'valuesIn'
2832
].forEach(function (methodName) {
2933
var filename = dasherize(methodName)
3034
var filepath = path.join(__dirname, filename)

keys-in.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @module object-loops/keys-in
3+
*/
4+
5+
/**
6+
* Like to `keys`, but includes enumerable keys from the prototype chain.
7+
* @function module:object-loops/keys-in
8+
* @param {object} [obj] - object to get enumerable keys from
9+
* @returns {array} keys
10+
*/
11+
module.exports = require('101/keys-in')

keys.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @module object-loops/keys
3+
*/
4+
5+
/**
6+
* Equivalent to `Object.keys`. Implemented specifically for chain.
7+
* @function module:object-loops/keys
8+
* @param {object} [obj] - object to get hasOwnProperty enumerable keys from
9+
* @returns {array} keys
10+
*/
11+
module.exports = keys
12+
13+
function keys (obj) {
14+
return Object.keys(obj)
15+
}

map-keys.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ function mapKeys (obj, callback, thisArg) {
1818
throw new TypeError(callback + ' must be a function')
1919
}
2020
var objIsArray = Array.isArray(obj)
21+
var mapped = objIsArray ? [] : {}
2122
if (objIsArray) {
22-
forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach)
23+
obj.forEach(eachCallback)
24+
} else {
25+
forEach(obj, eachCallback)
2326
}
24-
var mapped = objIsArray ? [] : {}
25-
forEach(obj, function (val, key, obj) {
27+
function eachCallback (val, key, obj) {
2628
var newKey = callback.call(thisArg, key, val, obj)
2729
mapped[newKey] = val
28-
})
30+
}
2931
return mapped
3032
}
3133
/**

test/fixtures/reset-object-prototype.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
var methodNames = [
2-
'every',
3-
'inverse',
4-
'filter',
5-
'find',
6-
'findKey',
7-
'forEach',
8-
'map',
9-
'mapKeys',
10-
'reduce',
11-
'some'
12-
]
1+
var methodNames = require('./all-method-names.js')
132

143
module.exports = resetObjectPrototype
154

test/test-chain.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ describe('chain', function () {
2323
.map(mapCb)
2424
.mapKeys(mapKeysCb)
2525
.reduce(reduceCb)
26+
.keys()
27+
.map(mapCb)
28+
.mapKeys(mapKeysCb)
29+
.reduce(reduceCb)
2630
.toJSON()
27-
expect(output).to.deep.equal({ x: 1, y: 1 })
31+
expect(output).to.deep.equal(['x', 'y'])
2832
done()
2933
})
3034
it('should have all methods', function (done) {

test/test-keys-in.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var Code = require('code')
2+
var Lab = require('lab')
3+
var lab = exports.lab = Lab.script()
4+
5+
var describe = lab.describe
6+
var it = lab.it
7+
var before = lab.before
8+
var after = lab.after
9+
var expect = Code.expect
10+
11+
var keysIn = require('../keys-in')
12+
13+
describe('keysIn', function () {
14+
function Person (name) {
15+
this.name = name
16+
}
17+
Person.prototype.getName = function () {
18+
return this.name
19+
}
20+
21+
describe('prototype', function () {
22+
before(function (done) {
23+
require('../index')()
24+
done()
25+
})
26+
after(require('./fixtures/reset-object-prototype'))
27+
it('should get all direct enumerables keys from object', function (done) {
28+
var objKeys = new Person('hey').keysIn()
29+
// assertions
30+
expect(objKeys).to.deep.equal(['name', 'getName'])
31+
done()
32+
})
33+
})
34+
35+
describe('require', function () {
36+
it('should get all direct enumerables keys from object', function (done) {
37+
var objKeys = keysIn(new Person('hey'))
38+
// assertions
39+
expect(objKeys).to.deep.equal(['name', 'getName'])
40+
done()
41+
})
42+
})
43+
})

test/test-keys.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var Code = require('code')
2+
var Lab = require('lab')
3+
var lab = exports.lab = Lab.script()
4+
5+
var describe = lab.describe
6+
var it = lab.it
7+
var before = lab.before
8+
var after = lab.after
9+
var expect = Code.expect
10+
11+
var keys = require('../keys')
12+
13+
describe('keys', function () {
14+
function Person (name) {
15+
this.name = name
16+
}
17+
Person.prototype.getName = function () {
18+
return this.name
19+
}
20+
21+
describe('prototype', function () {
22+
before(function (done) {
23+
require('../index')()
24+
done()
25+
})
26+
after(require('./fixtures/reset-object-prototype'))
27+
it('should get all direct enumerables keys from object', function (done) {
28+
var objKeys = new Person('hey').keys()
29+
// assertions
30+
expect(objKeys).to.deep.equal(['name'])
31+
done()
32+
})
33+
})
34+
35+
describe('require', function () {
36+
it('should get all direct enumerables keys from object', function (done) {
37+
var objKeys = keys(new Person('hey'))
38+
// assertions
39+
expect(objKeys).to.deep.equal(['name'])
40+
done()
41+
})
42+
})
43+
})

0 commit comments

Comments
 (0)