-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Switch logic of "pluck" and "pick". Update "pluck" to accept ob…
…ject arrays. BREAKING CHANGE: "pick" for extracting one property, "pluck" for multiple. Ramda uses "pick" for plural and "pluck" for singular, RethinkDB the other way around. Going with Rethink.
- Loading branch information
Showing
4 changed files
with
142 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
import { curry } from "../curry/curry" | ||
|
||
const _pick = (keys, source) => { | ||
const result = {} | ||
|
||
for (let i = 0, length = keys.length; i < length; i++) { | ||
const key = keys[i] | ||
const value = source[key] | ||
/** | ||
* @param {string} key Field name to extract values from | ||
* @param {object[]} source Array of objects | ||
* | ||
* @returns {Array} | ||
*/ | ||
const _pick = (key, source) => { | ||
const result = [] | ||
|
||
if (Object.hasOwnProperty.call(source, key)) { | ||
result[key] = value | ||
} | ||
for (let i = 0, length = source.length; i < length; i++) { | ||
result.push(source[i][key]) | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Returns a partial copy of an object containing only the keys specified. | ||
* If the key does not exist, the property is ignored. | ||
* Returns a new list by extracting the same named property off all objects in | ||
* the source list | ||
* | ||
* @tag Object | ||
* @signature ( keys: string[] ) => ( source: Object ): Object | ||
* @param {...any} params | ||
* | ||
* @param {string[]} keys The properties to be filtered out | ||
* @param {object} source The source object | ||
* @returns {Array} | ||
* | ||
* @returns {object} | ||
* @tag Array | ||
* @signature (key: string) => (source: object[]): mixed[] | ||
* | ||
* @example | ||
* pick(["id", "name"])({id: 2, name: "lorem", description: "lorem ipsum"}) | ||
* // => {id: 2, name: lorem} | ||
* pick("position")([{id: 1, position: 3}, {id:2, position: -1}]) | ||
* // => [3, -1] | ||
*/ | ||
export const pick = curry(_pick) | ||
export const pick = (...params) => { | ||
// @signature (key) => (source) | ||
if (params.length <= 1) { | ||
return source => _pick(params[0], source) | ||
} | ||
|
||
// @signature (key, source) | ||
return _pick(...params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,65 @@ | ||
import { curry } from "../curry/curry" | ||
import { map } from "../map/map" | ||
|
||
const _pluck = (field, source) => { | ||
const result = [] | ||
/** | ||
* @param {string[]} keys The properties to be filtered out | ||
* @param {object} source The source object | ||
* | ||
* @returns {object} | ||
*/ | ||
const _pluckOne = (keys, source) => { | ||
const result = {} | ||
|
||
for (let i = 0, length = source.length; i < length; i++) { | ||
result.push(source[i][field]) | ||
for (let i = 0, length = keys.length; i < length; i++) { | ||
const key = keys[i] | ||
const value = source[key] | ||
|
||
if (Object.hasOwnProperty.call(source, key)) { | ||
result[key] = value | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Returns a new list by extracting the same named property off all objects in | ||
* the source list | ||
* @param {string[]} keys | ||
* @param {object|object[]} source | ||
* | ||
* @param {string} field Field name to extract values from | ||
* @param {object[]} source Array of objects | ||
* @returns {object|object[]} | ||
*/ | ||
const _pluck = (keys, source) => | ||
Array.isArray(source) | ||
? map(item => _pluckOne(keys, item), source) | ||
: _pluckOne(keys, source) | ||
|
||
/** | ||
* Returns a partial copy of an object containing only the keys specified. | ||
* If the key does not exist, the property is ignored. | ||
* | ||
* @param {...any} params | ||
* | ||
* @returns {number} | ||
* @returns {object|object[]} | ||
* | ||
* @tag Array | ||
* @signature ( field: string ) => ( source: Object[] ): mixed[] | ||
* @tag Object | ||
* @signature (keys: string[]) => (source: Object): Object | ||
* | ||
* @example | ||
* pluck("position")([{id: 1, position: 3}, {id:2, position: -1}]) | ||
* // => [3, -1] | ||
* pluck( | ||
* ["id", "name"], | ||
* { | ||
* id: 2, | ||
* name: "lorem", | ||
* description: "lorem ipsum" | ||
* } | ||
* ) | ||
* // => {id: 2, name: lorem} | ||
*/ | ||
export const pluck = curry(_pluck) | ||
export const pluck = (...params) => { | ||
// @signature (keys) => (source) | ||
if (params.length <= 1) { | ||
return source => _pluck(params[0], source) | ||
} | ||
|
||
// @signature (keys, source) | ||
return _pluck(...params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters