Skip to content

Commit

Permalink
feat: adds random and range fns
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Mar 16, 2021
1 parent f9aceea commit 784decf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/__tests__/num.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,27 @@ describe(`num`, () => {
test('bsubtract', () => {
expect(num.bsubtract(2, 2)).toBe(0);
})
test(`urandomint/brandomint/urandomfloat/brandomfloat`, () => {
const min = 2;
const max = 6;
const check = (value: number) => value >= min && value <= max;
const testfn = (value: number) => expect(check(value)).toBeTruthy();

testfn(num.urandomint(min)(max));
testfn(num.brandomint(min, max));
testfn(num.urandomfloor(min)(max));
testfn(num.brandomfloor(min, max));
});
test(`brangeint/urangeint/urangeint2`, () => {
const expected = [0,1,2,3,4,5];
const first = num.brangeint(0, 5);
const second = num.urangeint(0)(5);
const third = num.urangeint2(5)(0);

const testfn = (arg: any) => expect(expect.arrayContaining(arg)).toEqual(expected);

testfn(first);
testfn(second);
testfn(third);
});
});
61 changes: 60 additions & 1 deletion src/num.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FnBinary, FnUnary, PrimitiveMethodMapper } from './applicative';
import { check, FnBinary, FnUnary, PrimitiveMethodMapper } from './applicative';

const map: PrimitiveMethodMapper<number> = key => (...args) => value => (Number.prototype[key] as any).apply(value, args);

Expand Down Expand Up @@ -81,6 +81,9 @@ export const uremainder: NumberUnaryFn = a => b => b % a;
export const uroot: NumberUnaryFn = a => b => b ** (1 / a);
export const usubtract: NumberUnaryFn = a => b => b - a;

export const urandomint: NumberUnaryFn = min => max => Math.floor(Math.random() * (max - min + 1)) + min;
export const urandomfloor: NumberUnaryFn = min => max => (Math.random() * (max - min + 1)) + min;

//#endregion

//#region binary functions
Expand All @@ -97,4 +100,60 @@ export const bremainder: NumberBinaryFn = (a, b) => a % b;
export const broot: NumberBinaryFn = (a, b) => a ** (1 / b);
export const bsubtract: NumberBinaryFn = (a, b) => a - b;

export const brandomint: NumberBinaryFn = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
export const brandomfloor: NumberBinaryFn = (min, max) => (Math.random() * (max - min + 1)) + min;

//#endregion

//#region ranges

export type NumberRangeUnaryFn = FnUnary<number, FnUnary<number, number[]>>
export type NumberRangeBinaryFn = FnBinary<number, number, number[]>

/**
* Returns an array for the given numeric range. Each number in range is integer.
* It throws if `min` is greater than `max`.
* Note: the range is always inclusive, so if you set the min to `0` and max to `5`, you
* will get all integer numbers between `0` and `5 `included.
*
* @since 2.13.0
* @example
*
* ```ts
* import { num } from 'tiinvo';
*
* num.brangeint(0, 5) // [0, 1, 2, 3, 4, 5];
* ```
*
* @param min
* @param max
* @returns {number[]}
*/
export const brangeint: NumberRangeBinaryFn = (min, max) => {
const count = check(min < max, 'urangeint min must be less than max')(max - min);
const range: number[] = [];
while (range.length <= count) range.push(min + range.length);
return range;
}

/**
* The unary version for `num.brangeint`.
* First function accepts the `min`, returned function accepts `max`.
*
* @since 2.13.0
* @param min
* @returns {(arg: number) => number[]}
*/
export const urangeint: NumberRangeUnaryFn = min => max => brangeint(min, max);

/**
* The reversed unary version for `num.brangeint`.
* First function accepts the `max`, returned function accepts `min`.
*
* @since 2.13.0
* @param min
* @returns {(arg: number) => number[]}
*/
export const urangeint2: NumberRangeUnaryFn = max => min => brangeint(min, max);

//#endregion

0 comments on commit 784decf

Please sign in to comment.