fix: sample filter randomness and count=1 case

This commit is contained in:
Harttle
2023-06-04 13:34:16 +08:00
parent b4e12232be
commit fcb930f0dd
3 changed files with 17 additions and 7 deletions
+4 -2
View File
@@ -89,9 +89,11 @@ export function uniq<T> (arr: T[]): T[] {
})
}
export function sample<T> (v: T[] | string, count: number | undefined = undefined): (T | string)[] {
export function sample<T> (v: T[] | string, count = 1): T | string | (T | string)[] {
v = toValue(v)
if (isNil(v)) return []
if (!isArray(v)) v = stringify(v)
return [...v].sort(() => Math.random()).slice(0, count)
const shuffled = [...v].sort(() => Math.random() - 0.5)
if (count === 1) return shuffled[0]
return shuffled.slice(0, count)
}