[ramda] split tests to separate modules: 'not' - 'pickBy' (#40543)

* re-remove 'move' and 'none' tests from ramda-tests.ts

* move 'not' tests to separate file

* move 'nth' tests to separate file

* move 'nthArg' tests to separate file

* move 'objOf' tests to separate file

* move 'of' tests to separate file

* move 'over' tests to separate file

* move 'pair' tests to separate file

* move 'partial' tests to separate file

* move 'partialRight' tests to separate file

* move 'partition' tests to separate file

* move 'path' tests to separate file

* move 'pathEq' tests to separate file

* move 'pathOr' tests to separate file

* move 'omit' tests to separate file

* move 'once' tests to separate file

* move 'or' tests to separate file

* move 'o' tests to separate file

* move 'pathSatisfies' tests to separate file

* move 'pick' tests to separate file

* move 'pickAll' tests to separate file

* move 'pickBy' tests to separate file
This commit is contained in:
Mike Deverell
2019-11-22 11:29:22 -05:00
committed by Sheetal Nandi
parent 5204fcfeeb
commit 554158f334
23 changed files with 306 additions and 243 deletions

View File

@@ -111,11 +111,6 @@ class F2 {
const b: number[] = coerceArray(1); // => [1]
};
(() => {
R.nthArg(1)("a", "b", "c"); // => 'b'
R.nthArg(-1)("a", "b", "c"); // => 'c'
});
() => {
const fn: (...args: string[]) => string = R.unapply(JSON.stringify);
const res: string = R.unapply(JSON.stringify)(1, 2, 3); // => '[1,2,3]'
@@ -268,34 +263,6 @@ R.times(i, 5);
squareThenDoubleThenTriple(5); // => 150
})();
(() => {
function multiply(a: number, b: number): number {
return a * b;
}
const double = R.partial<number>(multiply, [2]);
double(2); // => 4
function greet(salutation: string, title: string, firstName: string, lastName: string) {
return `${salutation}, ${title} ${firstName} ${lastName}!`;
}
const sayHello = R.partial(greet, ["Hello"]);
const sayHelloToMs = R.partial(sayHello, ["Ms."]);
sayHelloToMs("Jane", "Jones"); // => 'Hello, Ms. Jane Jones!'
const greetMsJaneJones = R.partialRight(greet, ["Ms.", "Jane", "Jones"]);
greetMsJaneJones("Hello"); // => 'Hello, Ms. Jane Jones!'
})();
(() => {
const addOneOnce = R.once((x: number) => x + 1);
addOneOnce(10); // => 11
addOneOnce(addOneOnce(50)); // => 11
const str = R.once<string>(() => 'test')();
})();
(() => {
const numbers = [1, 2, 3];
R.reduce((a, b) => a + b, 10, numbers); // => 16;
@@ -311,11 +278,6 @@ R.times(i, 5);
R.reduceRight(flattenPairs, [], pairs); // => [ 'c', 3, 'b', 2, 'a', 1 ]
})();
(() => {
const b: number[][] = R.of([1]); // => [[1]]
const c: number[] = R.of(1);
});
(() => {
function isOdd(n: number) {
return n % 2 === 1;
@@ -376,16 +338,6 @@ R.times(i, 5);
)([0, 1]); // => [1]
};
() => {
const testPath = ["x", 0, "y"];
const testObj = {x: [{y: 2, z: 3}, {y: 4, z: 5}]};
R.pathEq(testPath, 2, testObj); // => true
R.pathEq(testPath, 2)(testObj); // => true
R.pathEq(testPath)(2)(testObj); // => true
R.pathEq(testPath)(2, testObj); // => true
};
() => {
const xs: { [key: string]: string } = {a: "1", b: "0"};
R.propEq("a", "1", xs); // => true
@@ -467,45 +419,6 @@ interface Obj {
const headLens = R.lensIndex(0);
R.view(headLens, ["a", "b", "c"]); // => 'a'
R.set(headLens, "x", ["a", "b", "c"]); // => ['x', 'b', 'c']
R.over(headLens, R.toUpper, ["a", "b", "c"]); // => ['A', 'b', 'c']
};
() => {
const sampleList = ['a', 'b', 'c', 'd', 'e', 'f'];
R.move<string>(0, 2, sampleList); // => ['b', 'c', 'a', 'd', 'e', 'f']
R.move<string>(-1, 0, sampleList); // => ['f', 'a', 'b', 'c', 'd', 'e'] list rotation
const moveCurried1 = R.move(0, 2);
moveCurried1<string>(sampleList); // => ['b', 'c', 'a', 'd', 'e', 'f']
const moveCurried2 = R.move(0);
moveCurried2<string>(2, sampleList); // => ['b', 'c', 'a', 'd', 'e', 'f']
const moveCurried3 = R.move(0);
const moveCurried4 = moveCurried3(2);
moveCurried4<string>(sampleList); // => ['b', 'c', 'a', 'd', 'e', 'f']
};
() => {
R.none(Number.isNaN, [1, 2, 3]); // => true
R.none(Number.isNaN, [1, 2, 3, NaN]); // => false
R.none(Number.isNaN, [1, 2, 3, NaN]); // => false
};
() => {
const list = ["foo", "bar", "baz", "quux"];
R.nth(1, list); // => 'bar'
R.nth(-1, list); // => 'quux'
R.nth(-99, list); // => undefined
R.nth(-99)(list); // => undefined
};
() => {
R.partition(R.contains("s"), ["sss", "ttt", "foo", "bars"]);
R.partition(R.contains("s"))(["sss", "ttt", "foo", "bars"]);
R.partition((x: number) => x > 2, [1, 2, 3, 4]);
R.partition((x: number) => x > 2)([1, 2, 3, 4]);
};
() => {
@@ -886,23 +799,18 @@ type Pair = KeyValuePair<string, number>;
R.set(xLens, 4, {x: 1, y: 2}); // => {x: 4, y: 2}
R.set(xLens)(4, {x: 1, y: 2}); // => {x: 4, y: 2}
R.set(xLens, 4)({x: 1, y: 2}); // => {x: 4, y: 2}
R.over(xLens, R.negate, {x: 1, y: 2}); // => {x: -1, y: 2}
R.over(xLens, R.negate)({x: 1, y: 2}); // => {x: -1, y: 2}
R.over(xLens)(R.negate, {x: 1, y: 2}); // => {x: -1, y: 2}
};
() => {
const headLens = R.lensIndex(0);
R.view(headLens, ["a", "b", "c"]); // => 'a'
R.set(headLens, "x", ["a", "b", "c"]); // => ['x', 'b', 'c']
R.over(headLens, R.toUpper, ["a", "b", "c"]); // => ['A', 'b', 'c']
};
() => {
const xLens = R.lensProp("x");
R.view(xLens, {x: 1, y: 2}); // => 1
R.set(xLens, 4, {x: 1, y: 2}); // => {x: 4, y: 2}
R.over(xLens, R.negate, {x: 1, y: 2}); // => {x: -1, y: 2}
};
() => {
@@ -911,110 +819,6 @@ type Pair = KeyValuePair<string, number>;
R.view(xyLens, testObj); // => 2
R.set(xyLens, 4, testObj); // => {x: [{y: 4, z: 3}, {y: 4, z: 5}]}
R.over(xyLens, R.negate, testObj); // => {x: [{y: -2, z: 3}, {y: 4, z: 5}]}
};
() => {
const orValue = 11;
const orValueStr = "str";
const testPath = ["x", 0, "y"];
const testObj = {x: [{y: 2, z: 3}, {y: 4, z: 5}]};
const testObjMiss = {c: {b: 2}};
R.pathOr<number>(orValue, testPath, testObj); // => 2
R.pathOr<number>(orValue, testPath)(testObj); // => 2
R.pathOr<number>(orValue)(testPath)(testObj); // => 2
R.pathOr<number>(orValue)(testPath, testObj); // => 2
R.pathOr<number>(orValue, testPath, testObjMiss); // => 11
R.pathOr<number | string>(orValueStr, testPath, testObjMiss); // => "str"
};
() => {
const a: boolean = R.pathSatisfies((x: number) => x > 0, ["x"], {x: 1, y: 2}); // => true
const b: boolean = R.pathSatisfies((x: number) => x > 0, ["x"])({x: 1, y: 2}); // => true
const c: boolean = R.pathSatisfies((x: number) => x > 0)(["x"])({x: 1, y: 2}); // => true
};
() => {
function isPositive(n: number) {
return n > 0;
}
const a1 = R.pickBy(isPositive, {a: 1, b: 2, c: -1, d: 0, e: 5}); // => {a: 1, b: 2, e: 5}
function containsBackground(val: any) {
return val.bgcolor;
}
const colors = {1: {color: "read"}, 2: {color: "black", bgcolor: "yellow"}};
R.pickBy(containsBackground, colors); // => {2: {color: 'black', bgcolor: 'yellow'}}
function isUpperCase(val: number, key: string) {
return key.toUpperCase() === key;
}
R.pickBy(isUpperCase, {a: 1, b: 2, A: 3, B: 4}); // => {A: 3, B: 4}
};
() => {
const a1 = R.pick(["a", "d"], {a: 1, b: 2, c: 3, d: 4}); // => {a: 1, d: 4}
const a2 = R.pick(["a", "e", "f"], {a: 1, b: 2, c: 3, d: 4}); // => {a: 1}
const a3 = R.pick(["a", "e", "f"])({a: 1, b: 2, c: 3, d: 4}); // => {a: 1}
const a4 = R.pick(["a", "e", "f"], [1, 2, 3, 4]); // => {a: 1}
};
() => {
const matchPhrases = (xs: string[]) => R.objOf("must",
R.map(
(x: string) => R.objOf("match_phrase", x),
xs
)
);
const out: { must: Array<{ match_phrase: string }> } =
matchPhrases(["foo", "bar", "baz"]);
};
() => {
const classyGreeting = (name: { last: string; first: string }) =>
`The name's ${name.last}, ${name.first} ${name.last}`;
const yellGreeting = R.o(R.toUpper, classyGreeting);
const str: string = yellGreeting({ first: 'James', last: 'Bond' });
const num: number = R.o(R.multiply(10), R.add(10))(-4);
const num2: number = R.o(R.multiply(10))(R.add(10))(-4);
const num3: number = R.o(R.multiply(10))(R.add(10), -4);
const num4: number = R.o(R.multiply(10), R.add(10), -4);
};
() => {
R.omit(["a", "d"], {a: 1, b: 2, c: 3, d: 4}); // => {b: 2, c: 3}
R.omit(["a", "d"])({a: 1, b: 2, c: 3, d: 4}); // => {b: 2, c: 3}
};
() => {
R.pair("foo", "bar"); // => ['foo', 'bar']
const p = R.pair("foo", 1); // => ['foo', 'bar']
const x: string = p[0];
const y: number = p[1];
};
() => {
const headLens = R.lensIndex(0);
R.over(headLens, R.toUpper, ["foo", "bar", "baz"]); // => ['FOO', 'bar', 'baz']
};
() => {
R.pickAll(["a", "d"], {a: 1, b: 2, c: 3, d: 4}); // => {a: 1, d: 4}
R.pickAll(["a", "d"])({a: 1, b: 2, c: 3, d: 4}); // => {a: 1, d: 4}
R.pickAll(["a", "e", "f"], {a: 1, b: 2, c: 3, d: 4}); // => {a: 1, e: undefined, f: undefined}
R.pickAll(["a", "e", "f"])({a: 1, b: 2, c: 3, d: 4}); // => {a: 1, e: undefined, f: undefined}
};
() => {
function isUpperCase(val: number, key: string) {
return key.toUpperCase() === key;
}
R.pickBy(isUpperCase, {a: 1, b: 2, A: 3, B: 4}); // => {A: 3, B: 4}
};
() => {
@@ -1139,17 +943,6 @@ type Pair = KeyValuePair<string, number>;
/*****************************************************************
* Relation category
*/
() => {
const testPath = ["x", 0, "y"];
const testObj = {x: [{y: 2, z: 3}, {y: 4, z: 5}]};
R.path(testPath, testObj); // => 2
R.path(testPath)(testObj); // => 2
R.path(['a', 'b'])({c: {b: 2}}); // => undefined
R.path(['a', 'b'], {c: {b: 2}}); // => undefined
};
() => {
const sortByAgeDescending = R.sortBy(R.compose<{}, number, number>(R.negate, R.prop("age")));
const alice = {
@@ -1272,38 +1065,3 @@ type Pair = KeyValuePair<string, number>;
R.replace(/([cfk])oo/g, (match, p1, offset) => `${p1}-${offset}`)("coo foo koo"); // => 'c0oo f4oo k8oo'
R.replace(/([cfk])oo/g)((match, p1, offset) => `${p1}-${offset}`) ("coo foo koo"); // => 'c0oo f4oo k8oo'
};
/*****************************************************************
* Logic category
*/
() => {
R.not(true); // => false
R.not(false); // => true
R.not(0); // => true
R.not(1); // => false
};
class Why {
val: boolean;
constructor(val: boolean) {
this.val = val;
}
or(x: boolean) {
return this.val && x;
}
}
() => {
const x0: boolean = R.or(false, true); // => false
const x1: number | any[] = R.or(0, []); // => []
const x2: number | any[] = R.or(0)([]); // => []
const x3: string | null = R.or(null, ""); // => ''
const why = new Why(true);
why.or(true);
const x4: Why | boolean = R.or(why, false); // false
};
// Curry tests

View File

@@ -0,0 +1,8 @@
import * as R from 'ramda';
() => {
R.not(true); // => false
R.not(false); // => true
R.not(0); // => true
R.not(1); // => false
};

View File

@@ -0,0 +1,9 @@
import * as R from 'ramda';
() => {
const list = ['foo', 'bar', 'baz', 'quux'];
R.nth(1, list); // => 'bar'
R.nth(-1, list); // => 'quux'
R.nth(-99, list); // => undefined
R.nth(-99)(list); // => undefined
};

View File

@@ -0,0 +1,6 @@
import * as R from 'ramda';
() => {
R.nthArg(1)('a', 'b', 'c'); // => 'b'
R.nthArg(-1)('a', 'b', 'c'); // => 'c'
};

View File

@@ -0,0 +1,13 @@
import * as R from 'ramda';
() => {
const classyGreeting = (name: { last: string; first: string }) =>
`The name's ${name.last}, ${name.first} ${name.last}`;
const yellGreeting = R.o(R.toUpper, classyGreeting);
const str: string = yellGreeting({ first: 'James', last: 'Bond' });
const num: number = R.o(R.multiply(10), R.add(10))(-4);
const num2: number = R.o(R.multiply(10))(R.add(10))(-4);
const num3: number = R.o(R.multiply(10))(R.add(10), -4);
const num4: number = R.o(R.multiply(10), R.add(10), -4);
};

View File

@@ -0,0 +1,12 @@
import * as R from 'ramda';
() => {
const matchPhrases = (xs: string[]) =>
R.objOf('must', R.map((x: string) => R.objOf('match_phrase', x), xs));
const out: { must: Array<{ match_phrase: string }> } = matchPhrases([
'foo',
'bar',
'baz',
]);
};

View File

@@ -0,0 +1,6 @@
import * as R from 'ramda';
() => {
const b: number[][] = R.of([1]); // => [[1]]
const c: number[] = R.of(1);
};

View File

@@ -0,0 +1,6 @@
import * as R from 'ramda';
() => {
R.omit(['a', 'd'], { a: 1, b: 2, c: 3, d: 4 }); // => {b: 2, c: 3}
R.omit(['a', 'd'])({ a: 1, b: 2, c: 3, d: 4 }); // => {b: 2, c: 3}
};

View File

@@ -0,0 +1,9 @@
import * as R from 'ramda';
(() => {
const addOneOnce = R.once((x: number) => x + 1);
addOneOnce(10); // => 11
addOneOnce(addOneOnce(50)); // => 11
const str = R.once<string>(() => 'test')();
})();

View File

@@ -0,0 +1,23 @@
import * as R from 'ramda';
class Why {
val: boolean;
constructor(val: boolean) {
this.val = val;
}
or(x: boolean) {
return this.val && x;
}
}
() => {
const x0: boolean = R.or(false, true); // => false
const x1: number | any[] = R.or(0, []); // => []
const x2: number | any[] = R.or(0)([]); // => []
const x3: string | null = R.or(null, ''); // => ''
const why = new Why(true);
why.or(true);
const x4: Why | boolean = R.or(why, false); // false
};

View File

@@ -0,0 +1,35 @@
import * as R from 'ramda';
() => {
const headLens = R.lensIndex(0);
R.over(headLens, R.toUpper, ['a', 'b', 'c']); // => ['A', 'b', 'c']
};
() => {
const xLens = R.lens(R.prop('x'), R.assoc('x'));
R.over(xLens, R.negate, { x: 1, y: 2 }); // => {x: -1, y: 2}
R.over(xLens, R.negate)({ x: 1, y: 2 }); // => {x: -1, y: 2}
R.over(xLens)(R.negate, { x: 1, y: 2 }); // => {x: -1, y: 2}
};
() => {
const headLens = R.lensIndex(0);
R.over(headLens, R.toUpper, ['a', 'b', 'c']); // => ['A', 'b', 'c']
};
() => {
const xLens = R.lensProp('x');
R.over(xLens, R.negate, { x: 1, y: 2 }); // => {x: -1, y: 2}
};
() => {
const xyLens = R.lensPath(['x', 0, 'y']);
const testObj = { x: [{ y: 2, z: 3 }, { y: 4, z: 5 }] };
R.over(xyLens, R.negate, testObj); // => {x: [{y: -2, z: 3}, {y: 4, z: 5}]}
};
() => {
const headLens = R.lensIndex(0);
R.over(headLens, R.toUpper, ['foo', 'bar', 'baz']); // => ['FOO', 'bar', 'baz']
};

View File

@@ -0,0 +1,8 @@
import * as R from 'ramda';
() => {
R.pair('foo', 'bar'); // => ['foo', 'bar']
const p = R.pair('foo', 1); // => ['foo', 'bar']
const x: string = p[0];
const y: number = p[1];
};

View File

@@ -0,0 +1,23 @@
import * as R from 'ramda';
(() => {
function multiply(a: number, b: number): number {
return a * b;
}
const double = R.partial<number>(multiply, [2]);
double(2); // => 4
function greet(
salutation: string,
title: string,
firstName: string,
lastName: string,
) {
return `${salutation}, ${title} ${firstName} ${lastName}!`;
}
const sayHello = R.partial(greet, ['Hello']);
const sayHelloToMs = R.partial(sayHello, ['Ms.']);
sayHelloToMs('Jane', 'Jones'); // => 'Hello, Ms. Jane Jones!'
})();

View File

@@ -0,0 +1,15 @@
import * as R from 'ramda';
(() => {
function greet(
salutation: string,
title: string,
firstName: string,
lastName: string,
) {
return `${salutation}, ${title} ${firstName} ${lastName}!`;
}
const greetMsJaneJones = R.partialRight(greet, ['Ms.', 'Jane', 'Jones']);
greetMsJaneJones('Hello'); // => 'Hello, Ms. Jane Jones!'
})();

View File

@@ -0,0 +1,8 @@
import * as R from 'ramda';
() => {
R.partition(R.contains('s'), ['sss', 'ttt', 'foo', 'bars']);
R.partition(R.contains('s'))(['sss', 'ttt', 'foo', 'bars']);
R.partition((x: number) => x > 2, [1, 2, 3, 4]);
R.partition((x: number) => x > 2)([1, 2, 3, 4]);
};

View File

@@ -0,0 +1,12 @@
import * as R from 'ramda';
() => {
const testPath = ['x', 0, 'y'];
const testObj = { x: [{ y: 2, z: 3 }, { y: 4, z: 5 }] };
R.path(testPath, testObj); // => 2
R.path(testPath)(testObj); // => 2
R.path(['a', 'b'])({ c: { b: 2 } }); // => undefined
R.path(['a', 'b'], { c: { b: 2 } }); // => undefined
};

View File

@@ -0,0 +1,11 @@
import * as R from 'ramda';
() => {
const testPath = ['x', 0, 'y'];
const testObj = { x: [{ y: 2, z: 3 }, { y: 4, z: 5 }] };
R.pathEq(testPath, 2, testObj); // => true
R.pathEq(testPath, 2)(testObj); // => true
R.pathEq(testPath)(2)(testObj); // => true
R.pathEq(testPath)(2, testObj); // => true
};

View File

@@ -0,0 +1,16 @@
import * as R from 'ramda';
() => {
const orValue = 11;
const orValueStr = 'str';
const testPath = ['x', 0, 'y'];
const testObj = { x: [{ y: 2, z: 3 }, { y: 4, z: 5 }] };
const testObjMiss = { c: { b: 2 } };
R.pathOr<number>(orValue, testPath, testObj); // => 2
R.pathOr<number>(orValue, testPath)(testObj); // => 2
R.pathOr<number>(orValue)(testPath)(testObj); // => 2
R.pathOr<number>(orValue)(testPath, testObj); // => 2
R.pathOr<number>(orValue, testPath, testObjMiss); // => 11
R.pathOr<number | string>(orValueStr, testPath, testObjMiss); // => "str"
};

View File

@@ -0,0 +1,16 @@
import * as R from 'ramda';
() => {
const a: boolean = R.pathSatisfies((x: number) => x > 0, ['x'], {
x: 1,
y: 2,
}); // => true
const b: boolean = R.pathSatisfies((x: number) => x > 0, ['x'])({
x: 1,
y: 2,
}); // => true
const c: boolean = R.pathSatisfies((x: number) => x > 0)(['x'])({
x: 1,
y: 2,
}); // => true
};

View File

@@ -0,0 +1,8 @@
import * as R from 'ramda';
() => {
const a1 = R.pick(['a', 'd'], { a: 1, b: 2, c: 3, d: 4 }); // => {a: 1, d: 4}
const a2 = R.pick(['a', 'e', 'f'], { a: 1, b: 2, c: 3, d: 4 }); // => {a: 1}
const a3 = R.pick(['a', 'e', 'f'])({ a: 1, b: 2, c: 3, d: 4 }); // => {a: 1}
const a4 = R.pick(['a', 'e', 'f'], [1, 2, 3, 4]); // => {a: 1}
};

View File

@@ -0,0 +1,8 @@
import * as R from 'ramda';
() => {
R.pickAll(['a', 'd'], { a: 1, b: 2, c: 3, d: 4 }); // => {a: 1, d: 4}
R.pickAll(['a', 'd'])({ a: 1, b: 2, c: 3, d: 4 }); // => {a: 1, d: 4}
R.pickAll(['a', 'e', 'f'], { a: 1, b: 2, c: 3, d: 4 }); // => {a: 1, e: undefined, f: undefined}
R.pickAll(['a', 'e', 'f'])({ a: 1, b: 2, c: 3, d: 4 }); // => {a: 1, e: undefined, f: undefined}
};

View File

@@ -0,0 +1,32 @@
import * as R from 'ramda';
() => {
function isPositive(n: number) {
return n > 0;
}
const a1 = R.pickBy(isPositive, { a: 1, b: 2, c: -1, d: 0, e: 5 }); // => {a: 1, b: 2, e: 5}
function containsBackground(val: any) {
return val.bgcolor;
}
const colors = {
1: { color: 'read' },
2: { color: 'black', bgcolor: 'yellow' },
};
R.pickBy(containsBackground, colors); // => {2: {color: 'black', bgcolor: 'yellow'}}
function isUpperCase(val: number, key: string) {
return key.toUpperCase() === key;
}
R.pickBy(isUpperCase, { a: 1, b: 2, A: 3, B: 4 }); // => {A: 3, B: 4}
};
() => {
function isUpperCase(val: number, key: string) {
return key.toUpperCase() === key;
}
R.pickBy(isUpperCase, { a: 1, b: 2, A: 3, B: 4 }); // => {A: 3, B: 4}
};

View File

@@ -150,6 +150,27 @@
"test/multiply-tests.ts",
"test/nAry-tests.ts",
"test/negate-tests.ts",
"test/none-tests.ts"
"test/none-tests.ts",
"test/not-tests.ts",
"test/nth-tests.ts",
"test/nthArg-tests.ts",
"test/o-tests.ts",
"test/objOf-tests.ts",
"test/of-tests.ts",
"test/omit-tests.ts",
"test/once-tests.ts",
"test/or-tests.ts",
"test/over-tests.ts",
"test/pair-tests.ts",
"test/partial-tests.ts",
"test/partialRight-tests.ts",
"test/partition-tests.ts",
"test/path-tests.ts",
"test/pathEq-tests.ts",
"test/pathOr-tests.ts",
"test/pathSatisfies-tests.ts",
"test/pick-tests.ts",
"test/pickAll-tests.ts",
"test/pickBy-tests.ts"
]
}