forked from remeda/remeda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Andrius Mozūraitis <andriusmozuraitis@thebankoflondon.com>
- Loading branch information
Showing
5 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |