Skip to content

Commit a55b5e0

Browse files
committed
readme: formatting updates, added missing docs for map-keys
1 parent dd995fa commit a55b5e0

File tree

1 file changed

+69
-30
lines changed

1 file changed

+69
-30
lines changed

README.md

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ Functional methods like forEach, map, filter, and other Array methods for Object
1010
# Index
1111

1212
[chain](https://github.com/tjmehta/object-loops#usage)
13+
1314
[every](https://github.com/tjmehta/object-loops#every)
14-
[find](https://github.com/tjmehta/object-loops#find)
15-
[findKey](https://github.com/tjmehta/object-loops#findKey)
15+
1616
[filter](https://github.com/tjmehta/object-loops#filter)
17+
18+
[findKey](https://github.com/tjmehta/object-loops#findKey)
19+
20+
[find](https://github.com/tjmehta/object-loops#find)
21+
1722
[forEach](https://github.com/tjmehta/object-loops#forEach)
23+
24+
[mapKeys](https://github.com/tjmehta/object-loops#mapKeys)
25+
1826
[map](https://github.com/tjmehta/object-loops#map)
27+
1928
[reduce](https://github.com/tjmehta/object-loops#reduce)
29+
2030
[some](https://github.com/tjmehta/object-loops#some)
2131

2232
# Usage
@@ -88,6 +98,35 @@ allGreaterThan25 // false
8898
*/
8999
```
90100

101+
## filter
102+
103+
Creates a new object with all entries that pass the test implemented by the provided function.
104+
105+
* @param {object} [obj] - object to filter values, not accepted if being used directly on Object.prototype
106+
* @param {function} callback - function to test each value in the object. return true to keep that entry, false otherwise.
107+
* @param {*} [thisArg] - optional. context to bind to callback
108+
* @returns {object} newly created object with filtered values
109+
110+
```js
111+
var filter = require('object-loops/filter')
112+
113+
var obj = {
114+
foo: 10,
115+
bar: 20,
116+
baz: 30,
117+
qux: 40,
118+
}
119+
var filteredObj = filter(obj, function (val, key, obj) {
120+
return val > 25
121+
})
122+
filteredObj /* Only has entries with vals greater than 25
123+
{
124+
baz: 30,
125+
qux: 40
126+
}
127+
*/
128+
```
129+
91130
## find
92131

93132
Find the value of the the object that passes the test implemented by the callback.
@@ -146,59 +185,59 @@ notfound // undefined
146185
*/
147186
```
148187

149-
## filter
188+
## forEach
150189

151-
Creates a new object with all entries that pass the test implemented by the provided function.
190+
Executes a provided function once per each object value.
152191

153-
* @param {object} [obj] - object to filter values, not accepted if being used directly on Object.prototype
154-
* @param {function} callback - function to test each value in the object. return true to keep that entry, false otherwise.
192+
* @param {object} [obj] - object to forEach, not accepted if being used directly on Object.prototype
193+
* @param {function} callback - function that will be invoked once for each key-value pair
155194
* @param {*} [thisArg] - optional. context to bind to callback
156-
* @returns {object} newly created object with filtered values
157195

158196
```js
159-
var filter = require('object-loops/filter')
197+
var forEach = require('object-loops/for-each')
160198

161199
var obj = {
162200
foo: 10,
163201
bar: 20,
164-
baz: 30,
165-
qux: 40,
202+
baz: 30
166203
}
167-
var filteredObj = filter(obj, function (val, key, obj) {
168-
return val > 25
204+
var keyConcat = ''
205+
var valSum = 0
206+
forEach(obj, function (val, key, obj) {
207+
keyConcat += key
208+
valSum += val
169209
})
170-
filteredObj /* Only has entries with vals greater than 25
171-
{
172-
baz: 30,
173-
qux: 40
174-
}
175-
*/
210+
keyConcat // = 'foobarbaz'
211+
valSum // = 60
176212
```
177213

178-
## forEach
214+
## mapKeys
179215

180-
Executes a provided function once per each object value.
216+
Creates a new object with the results of calling a provided function on every key in the object.
181217

182-
* @param {object} [obj] - object to forEach, not accepted if being used directly on Object.prototype
183-
* @param {function} callback - function that will be invoked once for each key-value pair
218+
* @param {object} [obj] - object to map keys, not accepted if being used directly on Object.prototype
219+
* @param {mapKeysCallback} callback - function that produces the new key for the new mapped object
184220
* @param {*} [thisArg] - optional. context to bind to callback
221+
* @returns {object} newly created object with mapped keys
185222

186223
```js
187-
var forEach = require('object-loops/for-each')
224+
var mapKeys = require('object-loops/map-keys')
188225

189226
var obj = {
190227
foo: 10,
191228
bar: 20,
192229
baz: 30
193230
}
194-
var keyConcat = ''
195-
var valSum = 0
196-
forEach(obj, function (val, key, obj) {
197-
keyConcat += key
198-
valSum += val
231+
var mappedObj = mapKeys(obj, function (key, val, obj) {
232+
return key + 'New'
199233
})
200-
keyConcat // = 'foobarbaz'
201-
valSum // = 60
234+
mappedObj /* Each key is concated w/ 'New'
235+
{
236+
fooNew: 10,
237+
barNew: 20,
238+
bazNew: 30
239+
}
240+
*/
202241
```
203242

204243
## map

0 commit comments

Comments
 (0)