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

Commit

Permalink
Move math stuff to ts (#35)
Browse files Browse the repository at this point in the history
* Move modulo to ts

* Move subtract to ts

* Move reduce to ts

* Move sum to ts

* Move negate to ts

* Move multiply to ts

* Move median to ts

* Migrate tests to ts

* Move mean to ts

* Move slice to ts

* Fix type errors
  • Loading branch information
adambrgmn authored Dec 15, 2021
1 parent 0c54c15 commit 519ae59
Show file tree
Hide file tree
Showing 36 changed files with 97 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-vans-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move reduce to ts
5 changes: 5 additions & 0 deletions .changeset/chatty-bags-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move negate to ts
5 changes: 5 additions & 0 deletions .changeset/cuddly-steaks-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move modulo to ts
5 changes: 5 additions & 0 deletions .changeset/eleven-days-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move median to ts
5 changes: 5 additions & 0 deletions .changeset/famous-deers-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

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

Move slice to ts
5 changes: 5 additions & 0 deletions .changeset/silent-ways-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move multiply to ts
5 changes: 5 additions & 0 deletions .changeset/smart-crabs-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': patch
---

Move mean to ts
5 changes: 5 additions & 0 deletions .changeset/warm-pugs-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frans': minor
---

Move subtract to ts
8 changes: 0 additions & 8 deletions src/__tests__/mean.js → src/__tests__/mean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,4 @@ describe('Core.mean', () => {
test('returns NaN for empty list', () => {
expect(mean([])).toBe(NaN);
});

test('handles array-like object', () => {
function getArgs() {
return arguments; // eslint-disable-line prefer-rest-params
}

expect(mean(getArgs(1, 2, 3))).toBe(2);
});
});
8 changes: 0 additions & 8 deletions src/__tests__/median.js → src/__tests__/median.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,4 @@ describe('Core.median', () => {
test('returns NaN for an empty list', () => {
expect(median([])).toBe(NaN);
});

test('handles array-like object', () => {
function getArgs() {
return arguments; // eslint-disable-line prefer-rest-params
}

expect(median(getArgs(1, 2, 3))).toBe(2);
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 5 additions & 4 deletions src/__tests__/reduce.js → src/__tests__/reduce.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { reduce } from '../reduce';

describe('Core.reduce', () => {
const add = (x, y) => x + y;
const mult = (x, y) => x * y;
const append = (arr, e) => arr.concat(e);
const add = (x: number, y: number) => x + y;
const mult = (x: number, y: number) => x * y;
const concat = <T>(arr: T[], e: T) => arr.concat(e);

test('folds simple fns over arrays with the supplied accumulator', () => {
expect(reduce(add, 0, [1, 2, 3, 4])).toBe(10);
expect(reduce(mult, 1, [1, 2, 3, 4])).toBe(24);
expect(reduce(append, [], [1, 2, 3, 4])).toEqual([1, 2, 3, 4]);

expect(reduce<number>(concat, [], [1, 2, 3, 4])).toEqual([1, 2, 3, 4]);
});

test('returns the accumulator for an empty array', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/slice.js → src/__tests__/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Core.slice', () => {
});

test('handles array-like object', () => {
const args = (function getArgs() {
const args = (function getArgs(..._: any[]) {
return arguments; // eslint-disable-line prefer-rest-params
})(1, 2, 3, 4, 5);

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/adjust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isArray } from './isArray';
export function adjust<T, R>(
i: number,
fn: (input: T) => R,
list: T[] | ArrayLike<T>,
list: ArrayLike<T>,
): (T | R)[] {
const arrLength = length(list);
if (i >= arrLength || i < -arrLength) {
Expand Down
4 changes: 3 additions & 1 deletion src/internal/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { append } from '../append';
import { prop } from '../prop';

export const values = <V = unknown>(obj: Record<string, V>): V[] => {
const props = keys(obj);
const props = keys(obj).filter(
(key): key is string => typeof key === 'string',
);
return reduce(
(acc: V[], key: string) => append(prop(key, obj), acc),
[],
Expand Down
2 changes: 1 addition & 1 deletion src/mean.js → src/mean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { sum } from './sum';
import { divide } from './divide';
import { length } from './length';

const mean = (list) => {
const mean = (list: number[]): number => {
const l = length(list);
const s = sum(list);
return divide(s, l);
Expand Down
6 changes: 2 additions & 4 deletions src/median.js → src/median.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { nth } from './nth';
import { slice } from './slice';
import { mean } from './mean';

const median = (list) => {
export function median(list: number[]): number {
const sorted = sort(subtract, list);
const l = length(sorted);
const half = divide(l, 2);
Expand All @@ -16,6 +16,4 @@ const median = (list) => {

const sub = slice(half - 1, half + 1, sorted);
return mean(sub);
};

export { median };
}
3 changes: 0 additions & 3 deletions src/modulo.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/modulo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function modulo(a: number, b: number) {
return a % b;
}
3 changes: 0 additions & 3 deletions src/multiply.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/multiply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function multiply(a: number, b: number) {
return a * b;
}
3 changes: 0 additions & 3 deletions src/negate.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/negate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function negate(n: number) {
return -n;
}
10 changes: 6 additions & 4 deletions src/reduce.js → src/reduce.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { length } from './length';

const reduce = (fn, init, arr) => {
export function reduce<I, T = I[]>(
fn: (acc: T, item: I) => T,
init: T,
arr: I[],
): T {
let idx = 0;
let accumulator = init;
const len = length(arr);
Expand All @@ -11,6 +15,4 @@ const reduce = (fn, init, arr) => {
}

return accumulator;
};

export { reduce };
}
8 changes: 0 additions & 8 deletions src/slice.js

This file was deleted.

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

export function slice(start: number, end: number, arr: string): string;
export function slice<T>(start: number, end: number, arr: ArrayLike<T>): T[];
export function slice<T>(
start: number,
end: number,
arr: ArrayLike<T> | string,
): T[] | string {
if (isString(arr)) {
return String.prototype.slice.call(arr, start, end);
}
return Array.prototype.slice.call(arr, start, end);
}
3 changes: 0 additions & 3 deletions src/subtract.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/subtract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function subtract(a: number, b: number) {
return a - b;
}
6 changes: 0 additions & 6 deletions src/sum.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { add } from './add';
import { reduce } from './reduce';

export function sum(list: number[]) {
return reduce(add, 0, list);
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"checkJs": false,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
Expand Down

0 comments on commit 519ae59

Please sign in to comment.