mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 22:10:41 -07:00
feat: Add support for the Jekyll sample filter (#612)
* Add support for the Jekyll sample filter See https://jekyllrb.com/docs/liquid/filters I am sorting the array randomly and then picking the first N items or all items if there is no sample limit. * Remove incorrect spaces before parens Typo and copy-paste meet
This commit is contained in:
@@ -88,3 +88,14 @@ export function uniq<T> (arr: T[]): T[] {
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
export function sample<T> (v: T[] | string, count: number | undefined = undefined): T[] | string {
|
||||
v = toValue(v)
|
||||
if (isNil(v)) return []
|
||||
if (!isArray(v)) {
|
||||
v = stringify(v)
|
||||
return [...v].sort(() => Math.random()).slice(0, count).join('')
|
||||
}
|
||||
|
||||
return [...v].sort(() => Math.random()).slice(0, count)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user