From f4ed409885a5a2bfeb5187b8c4c695ad9ebe4065 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sat, 19 Feb 2022 17:35:30 +0100 Subject: [PATCH] Update index.ts --- src/index.ts | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7313d28..62dc7eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -157,8 +157,29 @@ export function getRandom(min: number, max: number, decimals: number=0): number return (Math.random() * (maxRandom - minRandom + 1) + minRandom).toFixed(decimals); } + /** - * Returns the index of an Array of Files from its name. If there are multiple files with the same name, the last one will be returned. + * Returns a list of non-repeating random numbers. + * + * @param {number} long - Length. + * @param {number} list - Initial list. + * @return {number} + * @example + * + * getRandom(6) + * // [ 1, 3, 0, 5, 4, 2 ] + * + * + */ +export function randomUniqList(long: number, list=[]: Array): Array { + return list.length >= long ? list : randomUniqList(long, Array.from( + new Set( + list + .concat(Math.floor(Math.random() * long))))) +} + +/** + * Returns the index of an list of Files from its name. If there are multiple files with the same name, the last one will be returned. * @param {string} name - Name file. * @param {Array} list - List of files. * @return number @@ -171,7 +192,7 @@ export function getIndexOfFileList(name: string, list: Array): number { } /** - * Returns a copy of the array by removing one position by index. + * Returns a copy of the list by removing one position by index. * @param {number} index * @param {Array} list * @return {Array} list @@ -185,7 +206,7 @@ export function deleteArrayElementByIndex(index: number, list: Array): Arra } /** - * Returns a FileLists from an array containing Files. + * Returns a FileLists from an list containing Files. * @param {Array} filesList * @return {FileList} */ @@ -197,7 +218,7 @@ export function arrayFilesToFileList(filesList): Filelist { } /** - * Returns a copy of the Array by swapping 2 indices. + * Returns a copy of the list by swapping 2 indices. * @param {number} firstIndex * @param {number} secondIndex * @param {Array} list @@ -224,7 +245,7 @@ export function arraySwapIndex(firstIndex: number, secondIndex: number, list: Ar * arrayMoveIndex(2, 0, ['a', 'b', 'c', 'd']) * // => ['c', 'a', 'b', 'd'] */ -function arrayMoveIndex(indexFrom: number, indexTo: number, list: Array): Array { +export function arrayMoveIndex(indexFrom: number, indexTo: number, list: Array): Array { // Save value to move const moveValue = list[indexFrom]; // Deletes value to be moved