mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 04:40:39 -07:00
fix: sample filter randomness and count=1 case
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user