Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Migrate more modules to ts (#34)
Browse files Browse the repository at this point in the history
* Move anyPass to ts

* Add js to ts git shortcut

* Move append to ts

* Move concat to ts

* Move assoc to ts

* Supress ts warning

* Remove assocPath function

* Move both to ts

* Move clamp to ts

* Move comparator to ts

* Move complement to ts

* Fix ts warning
  • Loading branch information
adambrgmn authored Sep 9, 2020
1 parent bb2c942 commit 0c54c15
Show file tree
Hide file tree
Showing 39 changed files with 133 additions and 165 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-poets-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move anyPass to ts
5 changes: 5 additions & 0 deletions .changeset/forty-experts-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move complement to ts
5 changes: 5 additions & 0 deletions .changeset/serious-crews-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move concat to ts
5 changes: 5 additions & 0 deletions .changeset/serious-stingrays-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move assoc to ts
5 changes: 5 additions & 0 deletions .changeset/seven-shirts-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': major
---

Remove assocPath function
5 changes: 5 additions & 0 deletions .changeset/spicy-masks-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move both to ts
5 changes: 5 additions & 0 deletions .changeset/tough-chefs-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move append to ts
5 changes: 5 additions & 0 deletions .changeset/tricky-kings-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move comparator to ts
5 changes: 5 additions & 0 deletions .changeset/young-buses-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move clamp to ts
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"type-check": "tsc --noEmit",
"validate": "yarn run lint && yarn run test --coverage && yarn run build",
"release": "yarn build && yarn changeset publish",
"create-module": "node scripts/create-module.js"
"create-module": "node scripts/create-module.js",
"mv:ts": "sh -c 'git mv src/${0}.js src/${0}.ts && git mv src/__tests__/${0}.js src/__tests__/${0}.ts'"
},
"dependencies": {},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/anyPass.js → src/__tests__/anyPass.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { anyPass } from '../anyPass';

describe('Core.anyPass', () => {
const odd = (n) => n % 2 !== 0;
const lt20 = (n) => n < 20;
const gt5 = (n) => n > 5;
const odd = (n: number) => n % 2 !== 0;
const lt20 = (n: number) => n < 20;
const gt5 = (n: number) => n > 5;

test('reports whether all predicates are satisfied by a given value', () => {
const ok = (x) => anyPass([odd, lt20, gt5], x);
const ok = (x: number) => anyPass([odd, lt20, gt5], x);

expect(ok(7)).toBeTruthy();
expect(ok(9)).toBeTruthy();
Expand Down
6 changes: 5 additions & 1 deletion src/__tests__/append.js → src/__tests__/append.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { append } from '../append';
describe('Core.append', () => {
test('adds the element to the end of the list', () => {
expect(append('z', ['x', 'y'])).toEqual(['x', 'y', 'z']);
expect(append(['a', 'z'], ['x', 'y'])).toEqual(['x', 'y', ['a', 'z']]);
expect(append(['a', 'z'], ['x', 'y'] as (string | string[])[])).toEqual([
'x',
'y',
['a', 'z'],
]);
});

test('works on empty list', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/assoc.js → src/__tests__/assoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Core.assoc', () => {

test('is the equivalent of clone and set if the property is not on the original', () => {
const obj1 = { a: 1, b: { c: 2, d: 3 }, e: 4, f: 5 };
const obj2 = assoc('z', { x: 42 }, obj1);
const obj2 = assoc('z', { x: 42 }, (obj1 as unknown) as any);
expect(obj2).toEqual({ a: 1, b: { c: 2, d: 3 }, e: 4, f: 5, z: { x: 42 } });

expect(obj2.a).toBe(obj1.a);
Expand Down
51 changes: 0 additions & 51 deletions src/__tests__/assocPath.js

This file was deleted.

15 changes: 8 additions & 7 deletions src/__tests__/both.js → src/__tests__/both.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { both } from '../both';

describe('Core.both', () => {
test('combines two boolean-returning functions into one', () => {
const even = (x) => x % 2 === 0;
const gt10 = (x) => x > 10;
const even = (x: any) => x % 2 === 0;
const gt10 = (x: any) => x > 10;
const f = both(even, gt10);

expect(f(8)).toBeFalsy();
Expand All @@ -12,8 +12,8 @@ describe('Core.both', () => {
});

test('accepts functions that take multiple parameters', () => {
const between = (a, b, c) => a < b && b < c;
const total20 = (a, b, c) => a + b + c === 20;
const between = (a: any, b: any, c: any) => a < b && b < c;
const total20 = (a: any, b: any, c: any) => a + b + c === 20;
const f = both(between, total20);

expect(f(4, 5, 11)).toBeTruthy();
Expand All @@ -23,13 +23,14 @@ describe('Core.both', () => {
});

test('does not evaluate the second expression if the first one is false', () => {
let effect = 'not evaluated';
let effect = jest.fn();
const F = () => false;
const Z = () => {
effect = 'Z got evaluated';
effect();
return true;
};

both(F, Z)();
expect(effect).toBe('not evaluated');
expect(effect).not.toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion src/__tests__/clamp.js → src/__tests__/clamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { clamp } from '../clamp';

describe('Core.clamp', () => {
test('restrict number by min and max values', () => {
const min0Max10 = (n) => clamp(0, 10, n);
const min0Max10 = (n: number) => clamp(0, 10, n);
expect(min0Max10(0)).toBe(0);
expect(min0Max10(0)).toBe(0);
expect(min0Max10(-1)).toBe(0);
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/comparator.js → src/__tests__/comparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { comparator } from '../comparator';

describe('Core.comparator', () => {
test('builds a comparator function for sorting out of a simple predicate that reports whether the first param is smaller', () => {
const compOne = comparator((a, b) => a < b);
const compTwo = comparator((a, b) => a > b);
const compOne = comparator((a: number, b: number) => a < b);
const compTwo = comparator((a: number, b: number) => a > b);

expect([3, 1, 8, 1, 2, 5].sort(compOne)).toEqual([1, 1, 2, 3, 5, 8]);
expect([3, 1, 8, 1, 2, 5].sort(compTwo)).toEqual([8, 5, 3, 2, 1, 1]);
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/complement.js → src/__tests__/complement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { complement } from '../complement';

describe('Core.complement', () => {
test('accepts a boolean returning function and reverses its otput', () => {
const isNil = (x) => x == null;
const isNil = (x: any) => x == null;
const isNotNil = complement(isNil);

expect(isNotNil(null)).not.toBe(isNil(null));
});

test('creates boolean-returning function that reverses another', () => {
const even = (x) => x % 2 === 0;
const even = (x: number) => x % 2 === 0;
const f = complement(even);

expect(f(8)).toBeFalsy();
expect(f(13)).toBeTruthy();
});

test('accepts a function that take multiple parameters', () => {
const between = (a, b, c) => a < b && b < c;
const between = (a: number, b: number, c: number) => a < b && b < c;
const f = complement(between);

expect(f(4, 5, 11)).toBeFalsy();
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/concat.js → src/__tests__/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ describe('Core.concat', () => {
});

test('throws if attempting to combine an array with a non-array', () => {
// @ts-expect-error
expect(() => concat([1], 2)).toThrow(TypeError);
});

test('throws if not an array or String', () => {
// @ts-expect-error
expect(() => concat({}, {})).toThrow(TypeError);
});
});
2 changes: 1 addition & 1 deletion src/adjust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function adjust<T, R>(
const arr = slice(0, Infinity, list);

const idx = i < 0 ? arrLength + i : i;
const newArr = concat([], arr) as (T | R)[];
const newArr = concat([], arr as (T | R)[]);
newArr[idx] = fn(arr[idx]);

return newArr;
Expand Down
2 changes: 1 addition & 1 deletion src/allPass.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type PredicateFn<T> = (...args: T[]) => boolean;
import { PredicateFn } from './internal/types';

export function allPass<T>(predicateList: PredicateFn<T>[], ...args: T[]) {
const { length } = predicateList;
Expand Down
16 changes: 0 additions & 16 deletions src/anyPass.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/anyPass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { PredicateFn } from './internal/types';

export function anyPass<T>(predicateList: PredicateFn<T>[], ...args: T[]) {
const { length } = predicateList;
let i = 0;
let ret = false;

while (!ret) {
if (i >= length) break;

ret = predicateList[i](...args);
i += 1;
}

return ret;
}
5 changes: 0 additions & 5 deletions src/append.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/append.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { concat } from './concat';

export function append<T>(el: T, list: T[]): T[] {
return concat(list, [el]);
}
3 changes: 0 additions & 3 deletions src/assoc.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/assoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function assoc<O extends Record<string, unknown>>(
prop: keyof O,
val: unknown,
obj: O,
): O {
return { ...obj, [prop]: val };
}
38 changes: 0 additions & 38 deletions src/assocPath.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/both.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/both.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PredicateFn } from './internal/types';

export function both<T>(a: PredicateFn<T>, b: PredicateFn<T>) {
return (...args: T[]) => a(...args) && b(...args);
}
12 changes: 0 additions & 12 deletions src/clamp.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/clamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function clamp(min: number, max: number, n: number): number {
if (min > max) {
throw new Error(
`min (${min}) must be greater than max (${max}) in clamp(min, max, val)`,
);
}

return Math.max(min, Math.min(max, n));
}
Loading

0 comments on commit 0c54c15

Please sign in to comment.