From 9e28dca76d09e868284c3eb4f2398635e81490f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20Moz=C5=ABraitis?= Date: Sat, 27 Apr 2024 15:51:13 +0300 Subject: [PATCH] added new utility functions (#8) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Andrius MozĊĞraitis --- package.json | 2 +- src/randomItem.test.ts | 11 +++++++++++ src/randomItem.ts | 23 +++++++++++++++++++++++ src/slide.test.ts | 16 ++++++++++++++++ src/slide.ts | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/randomItem.test.ts create mode 100644 src/randomItem.ts create mode 100644 src/slide.test.ts create mode 100644 src/slide.ts diff --git a/package.json b/package.json index 6cccff6d0..ff7ea8422 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/randomItem.test.ts b/src/randomItem.test.ts new file mode 100644 index 000000000..0d040a78f --- /dev/null +++ b/src/randomItem.test.ts @@ -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; + }); +}); diff --git a/src/randomItem.ts b/src/randomItem.ts new file mode 100644 index 000000000..a4ff7cd1d --- /dev/null +++ b/src/randomItem.ts @@ -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(arr: T[], random: () => number = Math.random) { + const random_number = randomIntFromInterval(0, arr.length - 1, random) + return arr[random_number] +} \ No newline at end of file diff --git a/src/slide.test.ts b/src/slide.test.ts new file mode 100644 index 000000000..49705da5e --- /dev/null +++ b/src/slide.test.ts @@ -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); +}); diff --git a/src/slide.ts b/src/slide.ts new file mode 100644 index 000000000..0469bdb16 --- /dev/null +++ b/src/slide.ts @@ -0,0 +1,34 @@ +import { purry } from "./purry"; + +function _slide(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(items: readonly T[], count: number): T; +export function slide(count: number): (items: readonly T[]) => T; + +export function slide() { + return purry(_slide, arguments); +} \ No newline at end of file