2021-11-03 08:23:55 -05:00
|
|
|
function findCommonElement <T> (array1: T[], array2: T[]) {
|
|
|
|
for (const a of array1) {
|
|
|
|
for (const b of array2) {
|
|
|
|
if (a === b) return a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-07-27 06:44:40 -05:00
|
|
|
// Avoid conflict with other toArray() functions
|
|
|
|
function arrayify <T> (element: T | T[]) {
|
|
|
|
if (Array.isArray(element)) return element
|
|
|
|
|
|
|
|
return [ element ]
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:36:03 -05:00
|
|
|
// Avoid conflict with other uniq() functions
|
|
|
|
function uniqify <T> (elements: T[]) {
|
|
|
|
return Array.from(new Set(elements))
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:02:27 -05:00
|
|
|
// Thanks: https://stackoverflow.com/a/12646864
|
|
|
|
function shuffle <T> (elements: T[]) {
|
|
|
|
const shuffled = [ ...elements ]
|
|
|
|
|
|
|
|
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
|
|
|
|
|
|
[ shuffled[i], shuffled[j] ] = [ shuffled[j], shuffled[i] ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return shuffled
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:23:55 -05:00
|
|
|
export {
|
2022-08-17 08:36:03 -05:00
|
|
|
uniqify,
|
2022-07-27 06:44:40 -05:00
|
|
|
findCommonElement,
|
2023-06-22 08:02:27 -05:00
|
|
|
shuffle,
|
2022-07-27 06:44:40 -05:00
|
|
|
arrayify
|
2021-11-03 08:23:55 -05:00
|
|
|
}
|