Skip to content

Commit

Permalink
added new utility functions (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrius Mozūraitis <andriusmozuraitis@thebankoflondon.com>
  • Loading branch information
digimuza and Andrius Mozūraitis authored Apr 27, 2024
1 parent 825d5cc commit 9e28dca
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-prime",
"version": "1.0.1",
"version": "1.0.2",
"description": "A utility library for JavaScript and Typescript.",
"main": "dist/commonjs/index.js",
"module": "dist/es/index.js",
Expand Down
11 changes: 11 additions & 0 deletions src/randomItem.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { randomItemInArray } from './randomItem';

describe('Random item test', () => {
test('Random item', () => {
expect(randomItemInArray([1, 2, 3, 4, 5], () => 0.1)).toEqual(1);
expect(randomItemInArray([1, 2, 3, 4, 5], () => 0.5123)).toEqual(3);
expect(randomItemInArray([1, 2, 3, 4, 5], () => 0.9123)).toEqual(5);
expect(randomItemInArray([1, 2, 3, 4, 5], () => 0.7642653)).toEqual(4);
return;
});
});
23 changes: 23 additions & 0 deletions src/randomItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@


/**
* Return random number between two numbers
*
* @param min - minimum
* @param max - maximum
* @param random - Random number generation function, by default function uses Math.random
*/
export function randomIntFromInterval(min: number, max: number, random: () => number = Math.random) { // min and max included
return Math.floor(random() * (max - min + 1) + min);
}

/**
* Picks random item in array
*
* @param arr - Array to pick
* @param random - Random number generation function, by default function uses Math.random
*/
export function randomItemInArray<T>(arr: T[], random: () => number = Math.random) {
const random_number = randomIntFromInterval(0, arr.length - 1, random)
return arr[random_number]
}
16 changes: 16 additions & 0 deletions src/slide.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { slide } from "./slide";

test('slugify', () => {
expect(slide([1,2,3], 12)).toEqual(1);
expect(slide([1,2,3], 11)).toEqual(3);
expect(slide([1,2,3], 10)).toEqual(2);
expect(slide([1,2,3], 9)).toEqual(1);
expect(slide([1,2,3], 8)).toEqual(3);


expect(slide([1,2,3], 1.23)).toEqual(2);
expect(slide([1,2,3], 0.23)).toEqual(1);
expect(slide([1,2,3], 0.73)).toEqual(1);
expect(slide([1,2,3], 0.99)).toEqual(1);
expect(slide([1,2,3], 1.01)).toEqual(2);
});
34 changes: 34 additions & 0 deletions src/slide.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { purry } from "./purry";

function _slide<T>(array: T[], count: number) {
if (array.length === 0) throw new TypeError("Can't accept empty arrays");
const size = array.length;
const divider = Math.floor(count) % size;
if (divider < 0) return array[size - divider];
return array[divider];
}


/**
* Slide through array with number
* @param items array to slide
* @example
* slide([1,2,3,4,5], 0) // 1
* slide([1,2,3,4,5], 1) // 2
* slide([1,2,3,4,5], 10) // 5
* slide([1,2,3,4,5], -1) // 5
* slide([1,2,3,4,5], -2) // 4
* slide([1,2,3,4,5], -2.34) // 4
* slide([1,2,3,4,5], -4.3) // 2
* @signature
* P.slugify(str);
* @example
* P.slugify("Super ball cup") // => super-ball-cup
* @category Array
*/
export function slide<T>(items: readonly T[], count: number): T;
export function slide<T>(count: number): (items: readonly T[]) => T;

export function slide() {
return purry(_slide, arguments);
}

0 comments on commit 9e28dca

Please sign in to comment.