mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-06-28 22:30:01 +00:00
[ramda] split tests to separate modules: 'filter' - 'head' (#38347)
* move 'filter' tests to separate file * move 'find' tests to separate file * move 'findIndex' tests to separate file * move 'findLast' tests to separate file * move 'findLastIndex' tests to separate file * move 'flatten' tests to separate file * move 'flip' tests to separate file * move 'forEach' tests to separate file * move 'fromPairs' tests to separate file * move 'groupBy' tests to separate file * move 'groupWith' tests to separate file * move 'gt' tests to separate file * move 'gte' tests to separate file * move 'has' tests to separate file * move 'hasIn' tests to separate file * move 'hasPath' tests to separate file * move 'head' tests to separate file
This commit is contained in:
committed by
Mine Starks
parent
2e66d19c1f
commit
118fe79578
@@ -279,19 +279,6 @@ function addAll() {
|
||||
// Basic example
|
||||
R.useWith(addAll, [double, square]);
|
||||
|
||||
(() => {
|
||||
function printXPlusFive(x: number) {
|
||||
console.log(x + 5);
|
||||
}
|
||||
|
||||
R.forEach(printXPlusFive, [1, 2, 3]);
|
||||
})();
|
||||
|
||||
// (() => {
|
||||
// function printXPlusFive(x, i) { console.log(i + 5); }
|
||||
// R.forEach.idx(printXPlusFive, [{name: 1}, {name: 2}, {name: 3}]);
|
||||
// })();
|
||||
|
||||
function i(x: number) {
|
||||
return x;
|
||||
}
|
||||
@@ -424,13 +411,6 @@ R.times(i, 5);
|
||||
});
|
||||
|
||||
(() => {
|
||||
function isEven(n: number) {
|
||||
return n % 2 === 0;
|
||||
}
|
||||
|
||||
R.filter(isEven, [1, 2, 3, 4]); // => [2, 4]
|
||||
R.filter(isEven, { a: 0, b: 1 }); // => { a: 0 }
|
||||
|
||||
function isOdd(n: number) {
|
||||
return n % 2 === 1;
|
||||
}
|
||||
@@ -455,18 +435,6 @@ R.times(i, 5);
|
||||
const b = R.unfold(f); // => [-10, -20, -30, -40, -50]
|
||||
const c = b(10);
|
||||
});
|
||||
/*****************************************************************
|
||||
* Function category
|
||||
*/
|
||||
() => {
|
||||
function mergeThree(a: number, b: number, c: number): number[] {
|
||||
return (new Array<number>()).concat(a, b, c);
|
||||
}
|
||||
|
||||
mergeThree(1, 2, 3); // => [1, 2, 3]
|
||||
const flipped = R.flip(mergeThree);
|
||||
flipped(1, 2, 3); // => [2, 1, 3]
|
||||
};
|
||||
|
||||
/*********************
|
||||
* List category
|
||||
@@ -475,11 +443,6 @@ R.times(i, 5);
|
||||
function isEven(n: number) {
|
||||
return n % 2 === 0;
|
||||
}
|
||||
|
||||
const filterEven = R.filter(isEven);
|
||||
const objA: R.Dictionary<number> = filterEven({ a: 0, b: 1 }); // => { a: 0 }
|
||||
const listA: number[] = filterEven([0, 1]); // => [0]
|
||||
|
||||
const rejectEven = R.reject(isEven);
|
||||
const objB: R.Dictionary<number> = rejectEven({ a: 0, b: 1 }); // => { b: 1 }
|
||||
const listB: number[] = rejectEven([0, 1]); // => [1]
|
||||
@@ -507,45 +470,6 @@ R.times(i, 5);
|
||||
)([0, 1]); // => [1]
|
||||
};
|
||||
|
||||
() => {
|
||||
const compact = R.filter(Boolean);
|
||||
const objA: R.Dictionary<number> = compact({ a: 0, b: 1 }); // => { b: 1 }
|
||||
const listA: number[] = compact([0, 1]); // => [1]
|
||||
|
||||
const omitEmptyString = R.filter((val: string) => val !== '');
|
||||
const objB: R.Dictionary<string> = omitEmptyString({ a: '', b: 'foo' }); // => { b: 'foo' }
|
||||
const listB: string[] = omitEmptyString(['', 'foo']); // => ['foo']
|
||||
|
||||
const objC = omitEmptyString({ some: 42 }); // $ExpectError
|
||||
};
|
||||
|
||||
() => {
|
||||
const xs = [{a: 1}, {a: 2}, {a: 3}];
|
||||
R.find(R.propEq("a", 2))(xs); // => {a: 2}
|
||||
R.find(R.propEq("a", 4))(xs); // => undefined
|
||||
};
|
||||
|
||||
() => {
|
||||
const xs = [{a: 1}, {a: 2}, {a: 3}];
|
||||
R.findIndex(R.propEq("a", 2))(xs); // => 1
|
||||
R.findIndex(R.propEq("a", 4))(xs); // => -1
|
||||
|
||||
R.findIndex((x: number) => x === 1, [1, 2, 3]);
|
||||
};
|
||||
|
||||
() => {
|
||||
const xs = [{a: 1, b: 0}, {a: 1, b: 1}];
|
||||
R.findLast(R.propEq("a", 1))(xs); // => {a: 1, b: 1}
|
||||
R.findLast(R.propEq("a", 4))(xs); // => undefined
|
||||
};
|
||||
|
||||
() => {
|
||||
const xs = [{a: 1, b: 0}, {a: 1, b: 1}];
|
||||
R.findLastIndex(R.propEq("a", 1))(xs); // => 1
|
||||
R.findLastIndex(R.propEq("a", 4))(xs); // => -1
|
||||
R.findLastIndex((x: number) => x === 1, [1, 2, 3]);
|
||||
};
|
||||
|
||||
() => {
|
||||
const testPath = ["x", 0, "y"];
|
||||
const testObj = {x: [{y: 2, z: 3}, {y: 4, z: 5}]};
|
||||
@@ -554,20 +478,6 @@ R.times(i, 5);
|
||||
R.pathEq(testPath, 2)(testObj); // => true
|
||||
R.pathEq(testPath)(2)(testObj); // => true
|
||||
R.pathEq(testPath)(2, testObj); // => true
|
||||
|
||||
const user1 = {address: {zipCode: 90210}};
|
||||
const user2 = {address: {zipCode: 55555}};
|
||||
const user3 = {name: "Bob"};
|
||||
const users = [user1, user2, user3];
|
||||
const isFamous = R.pathEq(["address", "zipCode"], 90210);
|
||||
R.filter(isFamous, users); // => [ user1 ]
|
||||
};
|
||||
|
||||
() => {
|
||||
const coll = [{ type: 'BUY' }, { type: 'SELL' }, { type: 'BUY' }];
|
||||
const typeIs = R.propEq('type');
|
||||
const isBuy = typeIs('BUY');
|
||||
R.filter(isBuy, coll); // [{ type: 'BUY' }, { type: 'BUY' }]
|
||||
};
|
||||
|
||||
() => {
|
||||
@@ -605,37 +515,6 @@ interface Obj {
|
||||
R.propEq("a", 4, xs); // => false
|
||||
};
|
||||
|
||||
() => {
|
||||
R.flatten([[1, 2], [3, 4], [5, 6]]); // => [1, 2, 3, 4, 5, 6]
|
||||
R.flatten([1, 2, 3, 4, 5, 6]); // => [1, 2, 3, 4, 5, 6]
|
||||
};
|
||||
|
||||
() => {
|
||||
function printXPlusFive(x: number) {
|
||||
console.log(x + 5);
|
||||
}
|
||||
|
||||
R.forEach(printXPlusFive, [1, 2, 3]); // => [1, 2, 3]
|
||||
R.forEach(printXPlusFive)([1, 2, 3]); // => [1, 2, 3]
|
||||
// -> 6
|
||||
// -> 7
|
||||
// -> 8
|
||||
};
|
||||
|
||||
() => {
|
||||
const byGrade = R.groupBy((student: { score: number; name: string }) => {
|
||||
const score = student.score;
|
||||
return score < 65 ? "F" :
|
||||
score < 70 ? "D" :
|
||||
score < 80 ? "C" :
|
||||
score < 90 ? "B" : "A";
|
||||
});
|
||||
const students = [{name: "Abby", score: 84},
|
||||
{name: "Eddy", score: 58},
|
||||
{name: "Jack", score: 69}];
|
||||
byGrade(students);
|
||||
};
|
||||
|
||||
() => {
|
||||
interface MyObject {
|
||||
id: string;
|
||||
@@ -666,28 +545,6 @@ interface Obj {
|
||||
};
|
||||
};
|
||||
|
||||
() => {
|
||||
R.groupWith(R.equals)([0, 1, 1, 2, 3, 5, 8, 13, 21]);
|
||||
|
||||
R.groupWith(R.equals, [0, 1, 1, 2, 3, 5, 8, 13, 21]);
|
||||
// [[0], [1, 1], [2, 3, 5, 8, 13, 21]]
|
||||
|
||||
R.groupWith((a: number, b: number) => a % 2 === b % 2, [0, 1, 1, 2, 3, 5, 8, 13, 21]);
|
||||
// [[0], [1, 1], [2], [3, 5], [8], [13, 21]]
|
||||
|
||||
const isVowel = (a: string) => R.contains(a, "aeiou") ? a : "";
|
||||
R.groupWith(R.eqBy<string>(isVowel), "aestiou");
|
||||
// ['ae', 'st', 'iou']
|
||||
};
|
||||
|
||||
() => {
|
||||
R.head(["fi", "fo", "fum"]); // => 'fi'
|
||||
R.head([10, "ten"]); // => 10
|
||||
R.head(["10", 10]); // => '10'
|
||||
R.head("abc"); // => 'a'
|
||||
R.head(""); // => ''
|
||||
};
|
||||
|
||||
(() => {
|
||||
interface Book {
|
||||
id: string;
|
||||
@@ -1229,47 +1086,6 @@ type Pair = KeyValuePair<string, number>;
|
||||
* Object category
|
||||
*/
|
||||
|
||||
() => {
|
||||
const hasName = R.has("name");
|
||||
const a1: boolean = hasName({name: "alice"}); // => true
|
||||
const a2: boolean = hasName({name: "bob"}); // => true
|
||||
const a3: boolean = hasName({}); // => false
|
||||
|
||||
const point = {x: 0, y: 0};
|
||||
const pointHas = R.flip(R.has)(point);
|
||||
const b1: boolean = pointHas("x"); // => true
|
||||
const b2: boolean = pointHas("y"); // => true
|
||||
const b3: boolean = pointHas("z"); // => false
|
||||
};
|
||||
|
||||
class Rectangle {
|
||||
constructor(public width: number, public height: number) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
area(): number {
|
||||
return this.width * this.height;
|
||||
}
|
||||
}
|
||||
|
||||
() => {
|
||||
const square = new Rectangle(2, 2);
|
||||
R.hasIn("width", square); // => true
|
||||
R.hasIn("area", square); // => true
|
||||
R.flip(R.hasIn)(square)("area"); // => true
|
||||
};
|
||||
|
||||
() => {
|
||||
R.hasPath(['a', 'b'], {a: {b: 2}}); // true
|
||||
R.hasPath(['a', 'b'], {a: {b: undefined}}); // => true
|
||||
R.hasPath(['a', 'b'], {a: {c: 2}}); // => false
|
||||
R.hasPath(['a', 'b'], {}); // => false
|
||||
|
||||
const hasPathCurried = R.hasPath(['a', 'b']);
|
||||
hasPathCurried({a: {b: 2}}); // true
|
||||
};
|
||||
|
||||
() => {
|
||||
const raceResultsByFirstName = {
|
||||
first : "alice",
|
||||
@@ -1410,9 +1226,6 @@ class Rectangle {
|
||||
() => {
|
||||
R.merge({name: "fred", age: 10}, {age: 40});
|
||||
// => { 'name': 'fred', 'age': 40 }
|
||||
|
||||
const resetToDefault = R.flip(R.merge)({x: 0});
|
||||
resetToDefault({x: 5, y: 2}); // => {x: 0, y: 2}
|
||||
};
|
||||
|
||||
() => {
|
||||
@@ -1542,10 +1355,6 @@ class Rectangle {
|
||||
R.omit(["a", "d"])({a: 1, b: 2, c: 3, d: 4}); // => {b: 2, c: 3}
|
||||
};
|
||||
|
||||
() => {
|
||||
R.fromPairs([["a", 1], ["b", 2], ["c", 3]]); // => {a: 1, b: 2, c: 3}
|
||||
};
|
||||
|
||||
() => {
|
||||
R.pair("foo", "bar"); // => ['foo', 'bar']
|
||||
const p = R.pair("foo", 1); // => ['foo', 'bar']
|
||||
@@ -1668,10 +1477,6 @@ class Rectangle {
|
||||
};
|
||||
R.where(spec2, {x: 2, y: 7}); // => false
|
||||
R.where(spec2, {x: 3, y: 8}); // => true
|
||||
|
||||
const xs = [{x: 2, y: 1}, {x: 10, y: 2}, {x: 8, y: 3}, {x: 10, y: 4}];
|
||||
R.filter(R.where({x: 10}), xs); // ==> [{x: 10, y: 2}, {x: 10, y: 4}]
|
||||
R.filter(R.where({x: 10}))(xs); // ==> [{x: 10, y: 2}, {x: 10, y: 4}]
|
||||
};
|
||||
|
||||
() => {
|
||||
@@ -1793,27 +1598,6 @@ class Rectangle {
|
||||
R.startsWith([1])([1, 2, 3]); // => true
|
||||
};
|
||||
|
||||
() => {
|
||||
const half = R.flip(R.divide)(2);
|
||||
half(42); // => 21
|
||||
};
|
||||
|
||||
() => {
|
||||
R.gt(2, 6); // => false
|
||||
R.gt(2, 0); // => true
|
||||
R.gt(2, 2); // => false
|
||||
R.flip(R.gt)(2)(10); // => true
|
||||
R.gt(2)(10); // => false
|
||||
};
|
||||
|
||||
() => {
|
||||
R.gte(2, 6); // => false
|
||||
R.gte(2, 0); // => true
|
||||
R.gte(2, 2); // => false
|
||||
R.flip(R.gte)(2)(10); // => true
|
||||
R.gte(2)(10); // => false
|
||||
};
|
||||
|
||||
() => {
|
||||
R.isNaN(NaN); // => true
|
||||
R.isNaN(undefined); // => false
|
||||
@@ -1825,14 +1609,12 @@ class Rectangle {
|
||||
R.lt(2, 0); // => false
|
||||
R.lt(2, 2); // => false
|
||||
R.lt(5)(10); // => true
|
||||
R.flip(R.lt)(5)(10); // => false // right-sectioned currying
|
||||
};
|
||||
|
||||
() => {
|
||||
R.lte(2, 6); // => true
|
||||
R.lte(2, 0); // => false
|
||||
R.lte(2, 2); // => true
|
||||
R.flip(R.lte)(2)(1); // => true
|
||||
R.lte(2)(10); // => true
|
||||
};
|
||||
|
||||
@@ -1844,27 +1626,10 @@ class Rectangle {
|
||||
R.mathMod(17.2, 5); // => NaN
|
||||
R.mathMod(17, 5.3); // => NaN
|
||||
|
||||
const clock = R.flip(R.mathMod)(12);
|
||||
clock(15); // => 3
|
||||
clock(24); // => 0
|
||||
|
||||
const seventeenMod = R.mathMod(17);
|
||||
seventeenMod(3); // => 2
|
||||
};
|
||||
|
||||
() => {
|
||||
const hasName = R.has("name");
|
||||
hasName({name: "alice"}); // => true
|
||||
hasName({name: "bob"}); // => true
|
||||
hasName({}); // => false
|
||||
|
||||
const point = {x: 0, y: 0};
|
||||
const pointHas = R.flip(R.has)(point);
|
||||
pointHas("x"); // => true
|
||||
pointHas("y"); // => true
|
||||
pointHas("z"); // => false
|
||||
};
|
||||
|
||||
() => {
|
||||
const x: number = R.max(7, 3); // => 7
|
||||
const y: string = R.max("a", "z"); // => 'z'
|
||||
@@ -1927,10 +1692,6 @@ class Rectangle {
|
||||
// JS behavior:
|
||||
R.modulo(-17, 3); // => -2
|
||||
R.modulo(17, -3); // => 2
|
||||
|
||||
const isOdd = R.flip(R.modulo)(2);
|
||||
isOdd(42); // => 0
|
||||
isOdd(21); // => 1
|
||||
};
|
||||
|
||||
() => {
|
||||
@@ -1952,9 +1713,6 @@ class Rectangle {
|
||||
() => {
|
||||
R.subtract(10, 8); // => 2
|
||||
|
||||
const minus5 = R.flip(R.subtract)(5);
|
||||
minus5(17); // => 12
|
||||
|
||||
const complementaryAngle = R.subtract(90);
|
||||
complementaryAngle(30); // => 60
|
||||
complementaryAngle(72); // => 18
|
||||
|
||||
45
types/ramda/test/filter-tests.ts
Normal file
45
types/ramda/test/filter-tests.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
function isEven(n: number) {
|
||||
return n % 2 === 0;
|
||||
}
|
||||
|
||||
const filterEven = R.filter(isEven);
|
||||
const objA: R.Dictionary<number> = filterEven({ a: 0, b: 1 }); // => { a: 0 }
|
||||
const listA: number[] = filterEven([0, 1]); // => [0]
|
||||
};
|
||||
|
||||
() => {
|
||||
const compact = R.filter(Boolean);
|
||||
const objA: R.Dictionary<number> = compact({ a: 0, b: 1 }); // => { b: 1 }
|
||||
const listA: number[] = compact([0, 1]); // => [1]
|
||||
|
||||
const omitEmptyString = R.filter((val: string) => val !== '');
|
||||
const objB: R.Dictionary<string> = omitEmptyString({ a: '', b: 'foo' }); // => { b: 'foo' }
|
||||
const listB: string[] = omitEmptyString(['', 'foo']); // => ['foo']
|
||||
|
||||
const objC = omitEmptyString({ some: 42 }); // $ExpectError
|
||||
};
|
||||
|
||||
() => {
|
||||
const user1 = { address: { zipCode: 90210 } };
|
||||
const user2 = { address: { zipCode: 55555 } };
|
||||
const user3 = { name: 'Bob' };
|
||||
const users = [user1, user2, user3];
|
||||
const isFamous = R.pathEq(['address', 'zipCode'], 90210);
|
||||
R.filter(isFamous, users); // => [ user1 ]
|
||||
};
|
||||
|
||||
() => {
|
||||
const coll = [{ type: 'BUY' }, { type: 'SELL' }, { type: 'BUY' }];
|
||||
const typeIs = R.propEq('type');
|
||||
const isBuy = typeIs('BUY');
|
||||
R.filter(isBuy, coll); // [{ type: 'BUY' }, { type: 'BUY' }]
|
||||
};
|
||||
|
||||
() => {
|
||||
const xs = [{ x: 2, y: 1 }, { x: 10, y: 2 }, { x: 8, y: 3 }, { x: 10, y: 4 }];
|
||||
R.filter(R.where({ x: 10 }), xs); // ==> [{x: 10, y: 2}, {x: 10, y: 4}]
|
||||
R.filter(R.where({ x: 10 }))(xs); // ==> [{x: 10, y: 2}, {x: 10, y: 4}]
|
||||
};
|
||||
7
types/ramda/test/find-tests.ts
Normal file
7
types/ramda/test/find-tests.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
const xs = [{ a: 1 }, { a: 2 }, { a: 3 }];
|
||||
R.find(R.propEq('a', 2))(xs); // => {a: 2}
|
||||
R.find(R.propEq('a', 4))(xs); // => undefined
|
||||
};
|
||||
9
types/ramda/test/findIndex-tests.ts
Normal file
9
types/ramda/test/findIndex-tests.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
const xs = [{ a: 1 }, { a: 2 }, { a: 3 }];
|
||||
R.findIndex(R.propEq('a', 2))(xs); // => 1
|
||||
R.findIndex(R.propEq('a', 4))(xs); // => -1
|
||||
|
||||
R.findIndex((x: number) => x === 1, [1, 2, 3]);
|
||||
};
|
||||
7
types/ramda/test/findLast-tests.ts
Normal file
7
types/ramda/test/findLast-tests.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
const xs = [{ a: 1, b: 0 }, { a: 1, b: 1 }];
|
||||
R.findLast(R.propEq('a', 1))(xs); // => {a: 1, b: 1}
|
||||
R.findLast(R.propEq('a', 4))(xs); // => undefined
|
||||
};
|
||||
8
types/ramda/test/findLastIndex-tests.ts
Normal file
8
types/ramda/test/findLastIndex-tests.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
const xs = [{ a: 1, b: 0 }, { a: 1, b: 1 }];
|
||||
R.findLastIndex(R.propEq('a', 1))(xs); // => 1
|
||||
R.findLastIndex(R.propEq('a', 4))(xs); // => -1
|
||||
R.findLastIndex((x: number) => x === 1, [1, 2, 3]);
|
||||
};
|
||||
6
types/ramda/test/flatten-tests.ts
Normal file
6
types/ramda/test/flatten-tests.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
R.flatten([[1, 2], [3, 4], [5, 6]]); // => [1, 2, 3, 4, 5, 6]
|
||||
R.flatten([1, 2, 3, 4, 5, 6]); // => [1, 2, 3, 4, 5, 6]
|
||||
};
|
||||
86
types/ramda/test/flip-tests.ts
Normal file
86
types/ramda/test/flip-tests.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
function mergeThree(a: number, b: number, c: number): number[] {
|
||||
return new Array<number>().concat(a, b, c);
|
||||
}
|
||||
|
||||
mergeThree(1, 2, 3); // => [1, 2, 3]
|
||||
const flipped = R.flip(mergeThree);
|
||||
flipped(1, 2, 3); // => [2, 1, 3]
|
||||
};
|
||||
|
||||
() => {
|
||||
const point = { x: 0, y: 0 };
|
||||
const pointHas = R.flip(R.has)(point);
|
||||
const b1: boolean = pointHas('x'); // => true
|
||||
const b2: boolean = pointHas('y'); // => true
|
||||
const b3: boolean = pointHas('z'); // => false
|
||||
};
|
||||
|
||||
() => {
|
||||
class Rectangle {
|
||||
constructor(public width: number, public height: number) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
area(): number {
|
||||
return this.width * this.height;
|
||||
}
|
||||
}
|
||||
|
||||
const square = new Rectangle(2, 2);
|
||||
R.flip(R.hasIn)(square)('area'); // => true
|
||||
};
|
||||
|
||||
() => {
|
||||
const resetToDefault = R.flip(R.merge)({ x: 0 });
|
||||
resetToDefault({ x: 5, y: 2 }); // => {x: 0, y: 2}
|
||||
};
|
||||
|
||||
() => {
|
||||
const half = R.flip(R.divide)(2);
|
||||
half(42); // => 21
|
||||
};
|
||||
|
||||
() => {
|
||||
R.flip(R.gt)(2)(10); // => true
|
||||
};
|
||||
|
||||
() => {
|
||||
R.flip(R.gte)(2)(10); // => true
|
||||
};
|
||||
|
||||
() => {
|
||||
R.flip(R.lt)(5)(10); // => false // right-sectioned currying
|
||||
};
|
||||
|
||||
() => {
|
||||
R.flip(R.lte)(2)(1); // => true
|
||||
};
|
||||
|
||||
() => {
|
||||
const clock = R.flip(R.mathMod)(12);
|
||||
clock(15); // => 3
|
||||
clock(24); // => 0
|
||||
};
|
||||
|
||||
() => {
|
||||
const point = { x: 0, y: 0 };
|
||||
const pointHas = R.flip(R.has)(point);
|
||||
pointHas('x'); // => true
|
||||
pointHas('y'); // => true
|
||||
pointHas('z'); // => false
|
||||
};
|
||||
|
||||
() => {
|
||||
const isOdd = R.flip(R.modulo)(2);
|
||||
isOdd(42); // => 0
|
||||
isOdd(21); // => 1
|
||||
};
|
||||
|
||||
() => {
|
||||
const minus5 = R.flip(R.subtract)(5);
|
||||
minus5(17); // => 12
|
||||
};
|
||||
26
types/ramda/test/forEach-tests.ts
Normal file
26
types/ramda/test/forEach-tests.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
(() => {
|
||||
function printXPlusFive(x: number) {
|
||||
console.log(x + 5);
|
||||
}
|
||||
|
||||
R.forEach(printXPlusFive, [1, 2, 3]);
|
||||
})();
|
||||
|
||||
// (() => {
|
||||
// function printXPlusFive(x, i) { console.log(i + 5); }
|
||||
// R.forEach.idx(printXPlusFive, [{name: 1}, {name: 2}, {name: 3}]);
|
||||
// })();
|
||||
|
||||
() => {
|
||||
function printXPlusFive(x: number) {
|
||||
console.log(x + 5);
|
||||
}
|
||||
|
||||
R.forEach(printXPlusFive, [1, 2, 3]); // => [1, 2, 3]
|
||||
R.forEach(printXPlusFive)([1, 2, 3]); // => [1, 2, 3]
|
||||
// -> 6
|
||||
// -> 7
|
||||
// -> 8
|
||||
};
|
||||
5
types/ramda/test/fromPairs-tests.ts
Normal file
5
types/ramda/test/fromPairs-tests.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); // => {a: 1, b: 2, c: 3}
|
||||
};
|
||||
22
types/ramda/test/groupBy-tests.ts
Normal file
22
types/ramda/test/groupBy-tests.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
const byGrade = R.groupBy((student: { score: number; name: string }) => {
|
||||
const score = student.score;
|
||||
return score < 65
|
||||
? 'F'
|
||||
: score < 70
|
||||
? 'D'
|
||||
: score < 80
|
||||
? 'C'
|
||||
: score < 90
|
||||
? 'B'
|
||||
: 'A';
|
||||
});
|
||||
const students = [
|
||||
{ name: 'Abby', score: 84 },
|
||||
{ name: 'Eddy', score: 58 },
|
||||
{ name: 'Jack', score: 69 },
|
||||
];
|
||||
byGrade(students);
|
||||
};
|
||||
25
types/ramda/test/groupWith-tests.ts
Normal file
25
types/ramda/test/groupWith-tests.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
R.groupWith(R.equals)([0, 1, 1, 2, 3, 5, 8, 13, 21]);
|
||||
|
||||
R.groupWith(R.equals, [0, 1, 1, 2, 3, 5, 8, 13, 21]);
|
||||
// [[0], [1, 1], [2, 3, 5, 8, 13, 21]]
|
||||
|
||||
R.groupWith((a: number, b: number) => a % 2 === b % 2, [
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
5,
|
||||
8,
|
||||
13,
|
||||
21,
|
||||
]);
|
||||
// [[0], [1, 1], [2], [3, 5], [8], [13, 21]]
|
||||
|
||||
const isVowel = (a: string) => (R.contains(a, 'aeiou') ? a : '');
|
||||
R.groupWith(R.eqBy<string>(isVowel), 'aestiou');
|
||||
// ['ae', 'st', 'iou']
|
||||
};
|
||||
8
types/ramda/test/gt-tests.ts
Normal file
8
types/ramda/test/gt-tests.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
R.gt(2, 6); // => false
|
||||
R.gt(2, 0); // => true
|
||||
R.gt(2, 2); // => false
|
||||
R.gt(2)(10); // => false
|
||||
};
|
||||
8
types/ramda/test/gte-tests.ts
Normal file
8
types/ramda/test/gte-tests.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
R.gte(2, 6); // => false
|
||||
R.gte(2, 0); // => true
|
||||
R.gte(2, 2); // => false
|
||||
R.gte(2)(10); // => false
|
||||
};
|
||||
8
types/ramda/test/has-tests.ts
Normal file
8
types/ramda/test/has-tests.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
const hasName = R.has('name');
|
||||
const a1: boolean = hasName({ name: 'alice' }); // => true
|
||||
const a2: boolean = hasName({ name: 'bob' }); // => true
|
||||
const a3: boolean = hasName({}); // => false
|
||||
};
|
||||
18
types/ramda/test/hasIn-tests.ts
Normal file
18
types/ramda/test/hasIn-tests.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
class Rectangle {
|
||||
constructor(public width: number, public height: number) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
area(): number {
|
||||
return this.width * this.height;
|
||||
}
|
||||
}
|
||||
|
||||
() => {
|
||||
const square = new Rectangle(2, 2);
|
||||
R.hasIn('width', square); // => true
|
||||
R.hasIn('area', square); // => true
|
||||
};
|
||||
11
types/ramda/test/hasPath-tests.ts
Normal file
11
types/ramda/test/hasPath-tests.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
R.hasPath(['a', 'b'], { a: { b: 2 } }); // true
|
||||
R.hasPath(['a', 'b'], { a: { b: undefined } }); // => true
|
||||
R.hasPath(['a', 'b'], { a: { c: 2 } }); // => false
|
||||
R.hasPath(['a', 'b'], {}); // => false
|
||||
|
||||
const hasPathCurried = R.hasPath(['a', 'b']);
|
||||
hasPathCurried({ a: { b: 2 } }); // true
|
||||
};
|
||||
9
types/ramda/test/head-tests.ts
Normal file
9
types/ramda/test/head-tests.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import * as R from 'ramda';
|
||||
|
||||
() => {
|
||||
R.head(['fi', 'fo', 'fum']); // => 'fi'
|
||||
R.head([10, 'ten']); // => 10
|
||||
R.head(['10', 10]); // => '10'
|
||||
R.head('abc'); // => 'a'
|
||||
R.head(''); // => ''
|
||||
};
|
||||
@@ -75,6 +75,23 @@
|
||||
"test/eqBy-tests.ts",
|
||||
"test/eqProps-tests.ts",
|
||||
"test/equals-tests.ts",
|
||||
"test/evolve-tests.ts"
|
||||
"test/evolve-tests.ts",
|
||||
"test/filter-tests.ts",
|
||||
"test/find-tests.ts",
|
||||
"test/findIndex-tests.ts",
|
||||
"test/findLast-tests.ts",
|
||||
"test/findLastIndex-tests.ts",
|
||||
"test/flatten-tests.ts",
|
||||
"test/flip-tests.ts",
|
||||
"test/forEach-tests.ts",
|
||||
"test/fromPairs-tests.ts",
|
||||
"test/groupBy-tests.ts",
|
||||
"test/groupWith-tests.ts",
|
||||
"test/gt-tests.ts",
|
||||
"test/gte-tests.ts",
|
||||
"test/has-tests.ts",
|
||||
"test/hasIn-tests.ts",
|
||||
"test/hasPath-tests.ts",
|
||||
"test/head-tests.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user