Skip to content

Commit

Permalink
fix: sample filter randomness and count=1 case
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Jun 4, 2023
1 parent b4e1223 commit fcb930f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
6 changes: 4 additions & 2 deletions src/filters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
17 changes: 12 additions & 5 deletions test/integration/filters/array.spec.ts
Original file line number Diff line number Diff line change
@@ -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: ", " %}' +
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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 }}',
Expand Down

0 comments on commit fcb930f

Please sign in to comment.