1
0
mirror of https://github.com/phuoc-ng/csslayout.git synced 2025-10-23 02:36:11 +02:00
Files
csslayout/client/helpers/randomIterms.ts
Phuoc Nguyen 354f8da849 Update header
2021-04-01 15:13:01 +07:00

22 lines
607 B
TypeScript

/**
* A collection of popular layouts and patterns made with CSS (https://csslayout.io)
* (c) 2019 - 2021 Nguyen Huu Phuoc <https://twitter.com/nghuuphuoc>
*/
type Tuple<T> = [number, T[]];
function randomItems<T>(arr: T[], count: number): T[] {
const result = arr.concat().reduce(
(p, _, __, arr) => {
const [a, b] = p;
return (a < count)
? [a + 1, b.concat(arr.splice(Math.random() * arr.length | 0, 1))]
: p;
},
[0, []] as Tuple<T>
);
return (result as Tuple<T>)[1];
};
export default randomItems;