mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: size, first, last support arraylike objects, #781
This commit is contained in:
@@ -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'
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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: ", " %}' +
|
||||
@@ -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(
|
||||
@@ -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(
|
||||
@@ -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'))
|
||||
|
||||
Reference in New Issue
Block a user