-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #171
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 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 |
---|---|---|
|
@@ -29,6 +29,8 @@ declare namespace RamdaAdjunct { | |
|
||
interface Dictionary<T> { [key: string]: T; } | ||
|
||
type DictPred<T> = (value: T, key: string) => boolean; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
guillaumearm
Collaborator
|
||
|
||
export interface Static { | ||
/** | ||
* Checks if input value is `Array`. | ||
|
@@ -488,6 +490,13 @@ declare namespace RamdaAdjunct { | |
(source: object): (obj: object) => object; | ||
}; | ||
|
||
/** | ||
* Returns a partial copy of an object containing only the keys | ||
* that don't satisfy the supplied predicate. | ||
*/ | ||
omitBy<T, U extends Dictionary<T>>(pred: DictPred<T>, obj: U): U; | ||
omitBy<T, U extends Dictionary<T>>(pred: DictPred<T>): (obj: U) => U; | ||
|
||
/** | ||
* Weave a configuration into function returning the runnable monad like `Reader` or `Free`. | ||
*/ | ||
|
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { complement, identity, pickBy, useWith } from 'ramda'; | ||
|
||
/* eslint-disable max-len */ | ||
/** | ||
* Returns a partial copy of an object containing only the keys | ||
* that don't satisfy the supplied predicate. | ||
* | ||
* @func omitBy | ||
* @memberOf RA | ||
* @since {@link https://char0n.github.io/ramda-adjunct/2.6.0|v2.6.0} | ||
* @category Object | ||
* @sig ((v, k) -> Boolean) -> {k: v} -> {k: v} | ||
* @param {!Function} pred A predicate to determine whether or not a key should be included on the output object | ||
* @param {!Object} obj The object to copy from | ||
* @return {!Object} A new object only with properties that don't satisfy `pred` | ||
* | ||
* @example | ||
* | ||
* const isLowerCase = (val, key) => key.toLowerCase() === key; | ||
* RA.omitBy(isLowerCase, {a: 1, b: 2, A: 3, B: 4}); //=> {A: 3, B: 4} | ||
*/ | ||
/* eslint-enable max-len */ | ||
const omitBy = useWith(pickBy, [complement, identity]); | ||
|
||
export default omitBy; |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { notStrictEqual, deepStrictEqual } from 'assert'; | ||
import * as R from 'ramda'; | ||
|
||
import * as RA from '../src/index'; | ||
import eq from './shared/eq'; | ||
|
||
|
||
describe('omitBy', function () { | ||
const obj = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }; | ||
|
||
it('creates a copy of the object', function () { | ||
notStrictEqual(RA.omitBy(R.F, obj), obj); | ||
deepStrictEqual(RA.omitBy(R.F, obj), obj); | ||
}); | ||
|
||
it('when returning truthy, omits the key', function () { | ||
eq(RA.omitBy(R.T, obj), {}); | ||
eq(RA.omitBy(R.always({}), obj), {}); | ||
eq(RA.omitBy(R.always(1), obj), {}); | ||
}); | ||
|
||
it('when returning falsy, keeps the key', function () { | ||
eq(RA.omitBy(R.F, obj), obj); | ||
eq(RA.omitBy(R.always(0), obj), obj); | ||
eq(RA.omitBy(R.always(null), obj), obj); | ||
}); | ||
|
||
it('is called with (val, key, obj)', function () { | ||
eq(RA.omitBy(function (val, key, _obj) { | ||
eq(_obj, obj); | ||
return key === 'd' && val === 4; | ||
}, obj), { a: 1, b: 2, c: 3, e: 5, f: 6 }); | ||
}); | ||
|
||
it('retrieves prototype properties', function () { | ||
const F = function (param) { this.x = param }; | ||
F.prototype.y = 40; F.prototype.z = 50; | ||
const foo = new F(30); | ||
foo.v = 10; foo.w = 20; | ||
eq(RA.omitBy(function (val) { return val >= 45 }, foo), { v: 10, w: 20, x: 30, y: 40 }); | ||
}); | ||
|
||
it('is curried', function () { | ||
const copier = RA.omitBy(R.F); | ||
eq(copier(obj), obj); | ||
}); | ||
}); |
Just realized, this type signature doesn't include 3rd param, probably better would be something like