[ramda] split tests to separate modules: 'join' - 'mean' (#38570)

* move 'join' tests to separate file

* move 'juxt' tests to separate file

* move 'keys' tests to separate file

* move 'keysIn' tests to separate file

* move 'last' tests to separate file

* move 'lastIndexOf' tests to separate file

* move 'length' tests to separate file

* move 'lens' tests to separate file

* move 'lensIndex' tests to separate file

* move 'lensProp' tests to separate file

* move 'lt' tests to separate file

* move 'lte' tests to separate file

* move 'map' tests to separate file

* move 'mapAccum' tests to separate file

* move 'mapAccumRight' tests to separate file

* move 'mapObjIndexed' tests to separate file

* move 'mathMod' tests to separate file

* move 'max' tests to separate file

* move 'maxBy' tests to separate file

* move 'mean' tests to separate file
This commit is contained in:
Mike Deverell
2019-09-25 14:11:26 -07:00
committed by Michael Crane
parent f8aff22606
commit e7dd848801
22 changed files with 277 additions and 241 deletions
-240
View File
@@ -245,14 +245,6 @@ class F2 {
const followersForUser2: (userName: string) => Promise<string[]> = R.pipeWith(R.then)([db.getUserById, db.getFollowers]);
};
(() => {
const range = R.juxt([Math.min, Math.max]);
range(3, 4, 9, -3); // => [-3, 9]
const chopped = R.juxt([R.head, R.last]);
chopped([1, 2, 3]); // => [1, 3]
});
function square(x: number) {
return x * x;
}
@@ -365,39 +357,11 @@ R.times(i, 5);
R.reduceRight(flattenPairs, [], pairs); // => [ 'c', 3, 'b', 2, 'a', 1 ]
})();
(() => {
const values = {x: 1, y: 2, z: 3};
function prependKeyAndDouble(num: number, key: string, obj: any) {
return key + (num * 2);
}
R.mapObjIndexed(prependKeyAndDouble, values); // => { x: 'x2', y: 'y4', z: 'z6' }
});
(() => {
const testObject: {
[key: string]: Error
} = {
hello: new Error('hello'),
};
const errorMessages = R.mapObjIndexed(
function test(value, key) {
// value should be inferred.
return value.message + String(key);
}, testObject);
console.log(errorMessages);
});
(() => {
const b: number[][] = R.of([1]); // => [[1]]
const c: number[] = R.of(1);
});
(() => {
R.length([1, 2, 3]); // => 3
});
(() => {
function isOdd(n: number) {
return n % 2 === 1;
@@ -545,96 +509,13 @@ interface Obj {
)(list);
});
() => {
const spacer = R.join(" ");
spacer(["a", 2, 3.4]); // => 'a 2 3.4'
R.join("|", [1, 2, 3]); // => '1|2|3'
};
() => {
R.last(["fi", "fo", "fum"]); // => 'fum'
R.last("abc"); // => 'c'
R.last(""); // => ''
};
() => {
R.lastIndexOf(3, [-1, 3, 3, 0, 1, 2, 3, 4]); // => 6
R.lastIndexOf(10, [1, 2, 3, 4]); // => -1
};
() => {
R.length([]); // => 0
R.length([1, 2, 3]); // => 3
};
() => {
const headLens = R.lensIndex(0);
headLens([10, 20, 30, 40]); // => 10
headLens.set("mu", [10, 20, 30, 40]); // => ['mu', 20, 30, 40]
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']
};
() => {
function double(x: number) {
return x * 2;
}
R.map(double, [1, 2, 3]); // => [2, 4, 6]
// functor
const numberFunctor = {
map: <U>(fn: (c: number) => U) => {
const chars = "Ifmmp!Xpsme".split("");
return chars.map(char => fn(char.charCodeAt(0)));
}
};
R.map((x: number) => x - 1, numberFunctor); // => "Hello World"
};
() => {
interface A {
a: number;
b: number;
}
interface B {
a: string;
b: string;
}
R.map<A, A>(R.inc, {a: 1, b: 2});
R.map<A, B>(R.toString, {a: 1, b: 2});
R.map<A, A>(R.inc)({a: 1, b: 2});
R.map<A, B>(R.toString)({a: 1, b: 2});
};
() => {
const digits = ["1", "2", "3", "4"];
function append(a: string, b: string): [string, string] {
return [a + b, a + b];
}
R.mapAccum(append, "0", digits); // => ['01234', ['01', '012', '0123', '01234']]
R.mapAccum(append)("0", digits); // => ['01234', ['01', '012', '0123', '01234']]
R.mapAccum(append, "0")(digits); // => ['01234', ['01', '012', '0123', '01234']]
};
() => {
const digits = ["1", "2", "3", "4"];
function append(a: string, b: string): [string, string] {
return [a + b, a + b];
}
R.mapAccumRight(append, "0", digits); // => ['04321', ['04321', '0432', '043', '04']]
R.mapAccumRight(append)("0", digits); // => ['04321', ['04321', '0432', '043', '04']]
R.mapAccumRight(append, "0")(digits); // => ['04321', ['04321', '0432', '043', '04']]
};
() => {
const sampleList = ['a', 'b', 'c', 'd', 'e', 'f'];
@@ -1025,18 +906,6 @@ type Pair = KeyValuePair<string, number>;
* Object category
*/
() => {
const objKeys = R.keys({a: 1, b: 2, c: 3}); // $ExpectType ("a" | "b" | "c")[]
const numberKeys = R.keys(1); // $ExpectType string[]
const arrayKeys = R.keys([]); // List of array members
const stringKeys = R.keys('foo'); // $ExpectType string[]
};
() => {
const f = new F();
R.keysIn(f); // => ['x', 'y']
};
() => {
interface Person { firstName: string; lastName: string; }
const failedFetch = (id: string): Promise<Person> => Promise.reject('bad ID');
@@ -1091,53 +960,6 @@ type Pair = KeyValuePair<string, number>;
R.over(xyLens, R.negate, testObj); // => {x: [{y: -2, z: 3}, {y: 4, z: 5}]}
};
() => {
R.keys({a: 1, b: 2, c: 3}); // => ['a', 'b', 'c']
};
() => {
const f = new F();
R.keysIn(f); // => ['x', 'y']
};
() => {
const headLens = R.lens(
function get(arr: number[]) {
return arr[0];
},
function set(val: number, arr: number[]) {
return [val].concat(arr.slice(1));
}
);
headLens([10, 20, 30, 40]); // => 10
headLens.set("mu", [10, 20, 30, 40]); // => ['mu', 20, 30, 40]
const phraseLens = R.lens(
function get(obj: any) {
return obj.phrase;
},
function set(val: string, obj: any) {
const out = R.clone(obj);
out.phrase = val;
return out;
}
);
const obj1 = {phrase: "Absolute filth . . . and I LOVED it!"};
const obj2 = {phrase: "What's all this, then?"};
phraseLens(obj1); // => 'Absolute filth . . . and I LOVED it!'
phraseLens(obj2); // => "What's all this, then?"
phraseLens.set("Ooh Betty", obj1); // => { phrase: 'Ooh Betty'}
};
() => {
const phraseLens = R.lensProp("phrase");
const obj1 = {phrase: "Absolute filth . . . and I LOVED it!"};
const obj2 = {phrase: "What's all this, then?"};
phraseLens(obj1); // => 'Absolute filth . . . and I LOVED it!'
phraseLens(obj2); // => "What's all this, then?"
phraseLens.set("Ooh Betty", obj1); // => { phrase: 'Ooh Betty'}
};
() => {
R.merge({name: "fred", age: 10}, {age: 40});
// => { 'name': 'fred', 'age': 40 }
@@ -1495,60 +1317,6 @@ type Pair = KeyValuePair<string, number>;
R.startsWith([1])([1, 2, 3]); // => true
};
() => {
R.lt(2, 6); // => true
R.lt(2, 0); // => false
R.lt(2, 2); // => false
R.lt(5)(10); // => true
};
() => {
R.lte(2, 6); // => true
R.lte(2, 0); // => false
R.lte(2, 2); // => true
R.lte(2)(10); // => true
};
() => {
R.mathMod(-17, 5); // => 3
R.mathMod(17, 5); // => 2
R.mathMod(17, -5); // => NaN
R.mathMod(17, 0); // => NaN
R.mathMod(17.2, 5); // => NaN
R.mathMod(17, 5.3); // => NaN
const seventeenMod = R.mathMod(17);
seventeenMod(3); // => 2
};
() => {
const x: number = R.max(7, 3); // => 7
const y: string = R.max("a", "z"); // => 'z'
};
() => {
function cmp(obj: { x: R.Ord }) {
return obj.x;
}
const a = {x: 1};
const b = {x: 2};
const c = {x: 3};
const d = {x: "a"};
const e = {x: "z"};
const f = {x: new Date(0)};
const g = {x: new Date(60 * 1000)};
R.maxBy(cmp, a, c); // => {x: 3}
R.maxBy(cmp)(a, c); // => {x: 3}
R.maxBy(cmp)(a)(b);
R.maxBy(cmp)(f)(g);
};
() => {
const a: number = R.mean([2, 7, 9]); // => 6
const b: number = R.mean([]); // => NaN
};
() => {
const a: number = R.median([7, 2, 10, 9]); // => 8
const b: number = R.median([]); // => NaN
@@ -1659,14 +1427,6 @@ type Pair = KeyValuePair<string, number>;
* Logic category
*/
() => {
// Flatten all arrays in the list but leave other values alone.
const flattenArrays = R.map(R.ifElse(Array.isArray, R.flatten, R.identity));
flattenArrays([[0], [[10], [8]], 1234, {}]); // => [[0], [10, 8], 1234, {}]
flattenArrays([[[10], 123], [8, [10]], "hello"]); // => [[10, 123], [8, 10], "hello"]
};
() => {
R.not(true); // => false
R.not(false); // => true
+7
View File
@@ -0,0 +1,7 @@
import * as R from 'ramda';
() => {
const spacer = R.join(' ');
spacer(['a', 2, 3.4]); // => 'a 2 3.4'
R.join('|', [1, 2, 3]); // => '1|2|3'
};
+9
View File
@@ -0,0 +1,9 @@
import * as R from 'ramda';
() => {
const range = R.juxt([Math.min, Math.max]);
range(3, 4, 9, -3); // => [-3, 9]
const chopped = R.juxt([R.head, R.last]);
chopped([1, 2, 3]); // => [1, 3]
};
+8
View File
@@ -0,0 +1,8 @@
import * as R from 'ramda';
() => {
const objKeys = R.keys({a: 1, b: 2, c: 3}); // $ExpectType ("a" | "b" | "c")[]
const numberKeys = R.keys(1); // $ExpectType string[]
const arrayKeys = R.keys([]); // List of array members
const stringKeys = R.keys('foo'); // $ExpectType string[]
};
+12
View File
@@ -0,0 +1,12 @@
import * as R from 'ramda';
class F {
[k: string]: string;
x = 'X';
y = 'Y';
}
() => {
const f = new F();
R.keysIn(f); // => ['x', 'y']
};
+7
View File
@@ -0,0 +1,7 @@
import * as R from 'ramda';
() => {
R.last(['fi', 'fo', 'fum']); // => 'fum'
R.last('abc'); // => 'c'
R.last(''); // => ''
};
+6
View File
@@ -0,0 +1,6 @@
import * as R from 'ramda';
() => {
R.lastIndexOf(3, [-1, 3, 3, 0, 1, 2, 3, 4]); // => 6
R.lastIndexOf(10, [1, 2, 3, 4]); // => -1
};
+6
View File
@@ -0,0 +1,6 @@
import * as R from 'ramda';
() => {
R.length([]); // => 0
R.length([1, 2, 3]); // => 3
};
+30
View File
@@ -0,0 +1,30 @@
import * as R from 'ramda';
() => {
const headLens = R.lens(
function get(arr: number[]) {
return arr[0];
},
function set(val: number, arr: number[]) {
return [val].concat(arr.slice(1));
},
);
headLens([10, 20, 30, 40]); // => 10
headLens.set('mu', [10, 20, 30, 40]); // => ['mu', 20, 30, 40]
const phraseLens = R.lens(
function get(obj: any) {
return obj.phrase;
},
function set(val: string, obj: any) {
const out = R.clone(obj);
out.phrase = val;
return out;
},
);
const obj1 = { phrase: 'Absolute filth . . . and I LOVED it!' };
const obj2 = { phrase: "What's all this, then?" };
phraseLens(obj1); // => 'Absolute filth . . . and I LOVED it!'
phraseLens(obj2); // => "What's all this, then?"
phraseLens.set('Ooh Betty', obj1); // => { phrase: 'Ooh Betty'}
};
+7
View File
@@ -0,0 +1,7 @@
import * as R from 'ramda';
() => {
const headLens = R.lensIndex(0);
headLens([10, 20, 30, 40]); // => 10
headLens.set('mu', [10, 20, 30, 40]); // => ['mu', 20, 30, 40]
};
+10
View File
@@ -0,0 +1,10 @@
import * as R from 'ramda';
() => {
const phraseLens = R.lensProp('phrase');
const obj1 = { phrase: 'Absolute filth . . . and I LOVED it!' };
const obj2 = { phrase: "What's all this, then?" };
phraseLens(obj1); // => 'Absolute filth . . . and I LOVED it!'
phraseLens(obj2); // => "What's all this, then?"
phraseLens.set('Ooh Betty', obj1); // => { phrase: 'Ooh Betty'}
};
+8
View File
@@ -0,0 +1,8 @@
import * as R from 'ramda';
() => {
R.lt(2, 6); // => true
R.lt(2, 0); // => false
R.lt(2, 2); // => false
R.lt(5)(10); // => true
};
+8
View File
@@ -0,0 +1,8 @@
import * as R from 'ramda';
() => {
R.lte(2, 6); // => true
R.lte(2, 0); // => false
R.lte(2, 2); // => true
R.lte(2)(10); // => true
};
+44
View File
@@ -0,0 +1,44 @@
import * as R from 'ramda';
() => {
// Flatten all arrays in the list but leave other values alone.
const flattenArrays = R.map(R.ifElse(Array.isArray, R.flatten, R.identity));
flattenArrays([[0], [[10], [8]], 1234, {}]); // => [[0], [10, 8], 1234, {}]
flattenArrays([[[10], 123], [8, [10]], 'hello']); // => [[10, 123], [8, 10], "hello"]
};
() => {
function double(x: number) {
return x * 2;
}
R.map(double, [1, 2, 3]); // => [2, 4, 6]
// functor
const numberFunctor = {
map: <U>(fn: (c: number) => U) => {
const chars = 'Ifmmp!Xpsme'.split('');
return chars.map(char => fn(char.charCodeAt(0)));
},
};
R.map((x: number) => x - 1, numberFunctor); // => "Hello World"
};
() => {
interface A {
a: number;
b: number;
}
interface B {
a: string;
b: string;
}
R.map<A, A>(R.inc, { a: 1, b: 2 });
R.map<A, B>(R.toString, { a: 1, b: 2 });
R.map<A, A>(R.inc)({ a: 1, b: 2 });
R.map<A, B>(R.toString)({ a: 1, b: 2 });
};
+13
View File
@@ -0,0 +1,13 @@
import * as R from 'ramda';
() => {
const digits = ['1', '2', '3', '4'];
function append(a: string, b: string): [string, string] {
return [a + b, a + b];
}
R.mapAccum(append, '0', digits); // => ['01234', ['01', '012', '0123', '01234']]
R.mapAccum(append)('0', digits); // => ['01234', ['01', '012', '0123', '01234']]
R.mapAccum(append, '0')(digits); // => ['01234', ['01', '012', '0123', '01234']]
};
+13
View File
@@ -0,0 +1,13 @@
import * as R from 'ramda';
() => {
const digits = ['1', '2', '3', '4'];
function append(a: string, b: string): [string, string] {
return [a + b, a + b];
}
R.mapAccumRight(append, '0', digits); // => ['04321', ['04321', '0432', '043', '04']]
R.mapAccumRight(append)('0', digits); // => ['04321', ['04321', '0432', '043', '04']]
R.mapAccumRight(append, '0')(digits); // => ['04321', ['04321', '0432', '043', '04']]
};
+24
View File
@@ -0,0 +1,24 @@
import * as R from 'ramda';
() => {
const values = { x: 1, y: 2, z: 3 };
function prependKeyAndDouble(num: number, key: string, obj: any) {
return key + num * 2;
}
R.mapObjIndexed(prependKeyAndDouble, values); // => { x: 'x2', y: 'y4', z: 'z6' }
};
() => {
const testObject: {
[key: string]: Error;
} = {
hello: new Error('hello'),
};
const errorMessages = R.mapObjIndexed(function test(value, key) {
// value should be inferred.
return value.message + String(key);
}, testObject);
console.log(errorMessages);
};
+13
View File
@@ -0,0 +1,13 @@
import * as R from 'ramda';
() => {
R.mathMod(-17, 5); // => 3
R.mathMod(17, 5); // => 2
R.mathMod(17, -5); // => NaN
R.mathMod(17, 0); // => NaN
R.mathMod(17.2, 5); // => NaN
R.mathMod(17, 5.3); // => NaN
const seventeenMod = R.mathMod(17);
seventeenMod(3); // => 2
};
+6
View File
@@ -0,0 +1,6 @@
import * as R from 'ramda';
() => {
const x: number = R.max(7, 3); // => 7
const y: string = R.max('a', 'z'); // => 'z'
};
+19
View File
@@ -0,0 +1,19 @@
import * as R from 'ramda';
() => {
function cmp(obj: { x: R.Ord }) {
return obj.x;
}
const a = { x: 1 };
const b = { x: 2 };
const c = { x: 3 };
const d = { x: 'a' };
const e = { x: 'z' };
const f = { x: new Date(0) };
const g = { x: new Date(60 * 1000) };
R.maxBy(cmp, a, c); // => {x: 3}
R.maxBy(cmp)(a, c); // => {x: 3}
R.maxBy(cmp)(a)(b);
R.maxBy(cmp)(f)(g);
};
+6
View File
@@ -0,0 +1,6 @@
import * as R from 'ramda';
() => {
const a: number = R.mean([2, 7, 9]); // => 6
const b: number = R.mean([]); // => NaN
};
+21 -1
View File
@@ -112,6 +112,26 @@
"test/is-tests.ts",
"test/isArrayLike-tests.ts",
"test/isEmpty-tests.ts",
"test/isNaN-tests.ts"
"test/isNaN-tests.ts",
"test/join-tests.ts",
"test/juxt-tests.ts",
"test/keys-tests.ts",
"test/keysIn-tests.ts",
"test/last-tests.ts",
"test/lastIndexOf-tests.ts",
"test/length-tests.ts",
"test/lens-tests.ts",
"test/lensIndex-tests.ts",
"test/lensProp-tests.ts",
"test/lt-tests.ts",
"test/lte-tests.ts",
"test/map-tests.ts",
"test/mapAccum-tests.ts",
"test/mapAccumRight-tests.ts",
"test/mapObjIndexed-tests.ts",
"test/mathMod-tests.ts",
"test/max-tests.ts",
"test/maxBy-tests.ts",
"test/mean-tests.ts"
]
}