Skip to content

Commit

Permalink
feat: size, first, last support arraylike objects, #781
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Dec 28, 2024
1 parent bb08cfa commit 35a8442
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/filters/array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toArray, argumentsToValue, toValue, stringify, caseInsensitiveCompare, isArray, isNil, last as arrayLast } from '../util'
import { toArray, argumentsToValue, toValue, stringify, caseInsensitiveCompare, isArray, isNil, last as arrayLast, isArrayLike } from '../util'
import { arrayIncludes, equals, evalToken, isTruthy } from '../render'
import { Value, FilterImpl } from '../template'
import { Tokenizer } from '../parser'
Expand All @@ -12,8 +12,8 @@ export const join = argumentsToValue(function (this: FilterImpl, v: any[], arg:
this.context.memoryLimit.use(complexity)
return array.join(sep)
})
export const last = argumentsToValue((v: any) => isArray(v) ? arrayLast(v) : '')
export const first = argumentsToValue((v: any) => isArray(v) ? v[0] : '')
export const last = argumentsToValue((v: any) => isArrayLike(v) ? arrayLast(v) : '')
export const first = argumentsToValue((v: any) => isArrayLike(v) ? v[0] : '')
export const reverse = argumentsToValue(function (this: FilterImpl, v: any[]) {
const array = toArray(v)
this.context.memoryLimit.use(array.length)
Expand Down
4 changes: 4 additions & 0 deletions src/util/underscore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export function isArray (value: any): value is any[] {
return toString.call(value) === '[object Array]'
}

export function isArrayLike (value: any): value is any[] {
return value && isNumber(value.length)
}

export function isIterable (value: any): value is Iterable<any> {
return isObject(value) && Symbol.iterator in value
}
Expand Down
12 changes: 3 additions & 9 deletions test/integration/filters/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ describe('filters/array', function () {
return expect(render(src)).rejects.toThrow('expected ":" after filter name, line:1, col:83')
})
})
describe('last', () => {
it('should support last', function () {
const src = '{{ arr | last }}'
const scope = { arr: ['zebra', 'octopus', 'giraffe', 'tiger'] }
return test(src, scope, 'tiger')
})
})
describe('split', () => {
it('should support split', function () {
const src = '{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}' +
Expand Down Expand Up @@ -263,6 +256,7 @@ describe('filters/array', function () {
it('should return 0 for false', () => test('{{ false | size }}', '0'))
it('should return 0 for nil', () => test('{{ nil | size }}', '0'))
it('should return 0 for undefined', () => test('{{ foo | size }}', '0'))
it('should work for string', () => test('{{ "foo" | size }}', {}, '3'))
})
describe('first', function () {
it('should support first', () => test(
Expand All @@ -273,7 +267,7 @@ describe('filters/array', function () {
it('should return empty for nil', () => test('{{nil | first}}', ''))
it('should return empty for undefined', () => test('{{foo | first}}', ''))
it('should return empty for false', () => test('{{false | first}}', ''))
it('should return empty for string', () => test('{{"zebra" | first}}', ''))
it('should work for string', () => test('{{ "foo" | first }}', 'f'))
})
describe('last', function () {
it('should support last', () => test(
Expand All @@ -284,7 +278,7 @@ describe('filters/array', function () {
it('should return empty for nil', () => test('{{nil | last}}', ''))
it('should return empty for undefined', () => test('{{foo | last}}', ''))
it('should return empty for false', () => test('{{false | last}}', ''))
it('should return empty for string', () => test('{{"zebra" | last}}', ''))
it('should work for string', () => test('{{ "foo" | last }}', {}, 'o'))
})
describe('slice', function () {
it('should slice first char by 0', () => test('{{ "Liquid" | slice: 0 }}', 'L'))
Expand Down

0 comments on commit 35a8442

Please sign in to comment.