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
+1
View File
@@ -17,6 +17,7 @@
### Features
* Add support for the Jekyll sample filter ([#612](https://github.com/harttle/liquidjs/issues/612)) ([ba8b842](https://github.com/harttle/liquidjs/commit/ba8b84245266589e43c0e70d99e12b981d349809))
* Add support for the Jekyll push filter ([#611](https://github.com/harttle/liquidjs/issues/611))
* introduce a matrix with latest Ubuntu and macOS to test the build on macOS as well ([82ba548](https://github.com/harttle/liquidjs/commit/82ba54845f4cd4e1e7660c1557e3cfaa22d68924)), closes [#615](https://github.com/harttle/liquidjs/issues/615)
* precise line/col for tokenization Error, [#613](https://github.com/harttle/liquidjs/issues/613) ([e347e60](https://github.com/harttle/liquidjs/commit/e347e603d76c039cec191d417deab34e7ef1f9a7))
+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)
}
+12 -5
View File
@@ -1,6 +1,8 @@
import { test, render } from '../../stub/render'
import { Liquid } from '../../../src/liquid'
describe('filters/array', function () {
const engine = new Liquid()
describe('index', function () {
it('should support index', function () {
const src = '{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' +
@@ -97,6 +99,10 @@ describe('filters/array', function () {
const scope = { val: ['hey'], arg: 'foo' }
await test('{{ val | push: arg | join: "," }}', scope, 'hey,foo')
})
it('should not change original array', async () => {
const scope = { val: ['hey'], arg: 'foo' }
await test('{{ val | push: arg | join: "," }} {{ val | push: arg | join: "," }}', scope, 'hey,foo hey,foo')
})
it('should support undefined left value', async () => {
const scope = { arg: 'foo' }
await test('{{ notDefined | push: arg | join: "," }}', scope, 'foo')
@@ -126,17 +132,18 @@ describe('filters/array', function () {
})
})
describe('sample', function () {
it('should return full array sample', () => test(
'{{ "hello,world" | split: "," | sample | size }}',
'2'
))
it('should return one item if count not specified', async () => {
const template = '{{ "hello,world" | split: "," | sample }}'
const result = await engine.parseAndRender(template)
expect(result).toMatch(/hello|world/)
})
it('should return full array sample even if excess count', () => test(
'{{ "hello,world" | split: "," | sample: 10 | size }}',
'2'
))
it('should return partial array sample', () => test(
'{{ "hello,world" | split: "," | sample: 1 | size }}',
'1'
'5'
))
it('should sample nil value', () => test(
'{{ nil | sample: 2 }}',