Skip to content

Commit

Permalink
feat(maths): add median fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed Mar 12, 2024
1 parent 8bef434 commit ff6d1f3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-rice-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opentf/utils": minor
---

Added array, maths, types, object category utility fns.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ bun add @opentf/utils
## Usage

```ts
import { isNum, range, camelCase, sleep } from "@opentf/utils";
import { isNum, range, pascalCase, sleep } from "@opentf/utils";

isNum(NaN); //=> false

range(1, 5); //=> [1, 2, 3, 4, 5]

camelCase("i phone"); //=> 'iPhone'
pascalCase("pascal case"); //=> PascalCase

await sleep(1000); // It suspends the exection for 1 second.
```
Expand Down
13 changes: 13 additions & 0 deletions packages/utils/__tests__/maths/median.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { median } from '../../src';

describe('Number', () => {
test('median', () => {
expect(median()).toBe(NaN);
expect(median([])).toBe(NaN);
expect(median([1])).toBe(1);
expect(median([4, 1, 7])).toBe(1);
expect(median([4, 2, 8])).toBe(2);
expect(median([1, 4, 2, 5, 0])).toBe(2);
expect(median([10, 20, 40, 50])).toBe(30);
});
});
1 change: 1 addition & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"build": "tsup",
"test": "jest",
"test:w": "jest --watch",
"test:b": "bun test --watch",
"lint": "eslint src/** --fix",
"check-types": "tsc --noEmit",
"ci": "turbo run build test lint check-types"
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { default as odd } from './maths/odd';
export { default as sum } from './maths/sum';
export { default as prod } from './maths/prod';
export { default as mean } from './maths/mean';
export { default as median } from './maths/median';

// Types
export { default as isNum } from './types/isNum';
Expand Down
26 changes: 26 additions & 0 deletions packages/utils/src/maths/median.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sort from '../array/sort';
import even from './even';

/**
* Calculates the median value of the given array.
*
* @example
*
* median([1, 4, 2, 5, 0]) //=> 2
*
* median([10, 20, 40, 50]) //=> 30;
*/
export default function median(
arr: number[] = [],
cb?: (val: number, index: number) => number
) {
let a = cb ? arr.map(cb) : arr;
a = sort(arr);

if (even(a.length)) {
const i = a.length / 2;
return (arr[i] + arr[i - 1]) / 2;
}

return arr[Math.floor(a.length / 2)];
}

0 comments on commit ff6d1f3

Please sign in to comment.