mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: sort by key, see #227
This commit is contained in:
@@ -2,16 +2,26 @@ import { isArray, last as arrayLast } from '../../util/underscore'
|
|||||||
import { toArray } from '../../util/collection'
|
import { toArray } from '../../util/collection'
|
||||||
import { isTruthy } from '../../render/boolean'
|
import { isTruthy } from '../../render/boolean'
|
||||||
import { FilterImpl } from '../../template/filter/filter-impl'
|
import { FilterImpl } from '../../template/filter/filter-impl'
|
||||||
|
import { Scope } from '../../context/scope'
|
||||||
|
|
||||||
export const join = (v: any[], arg: string) => v.join(arg === undefined ? ' ' : arg)
|
export const join = (v: any[], arg: string) => v.join(arg === undefined ? ' ' : arg)
|
||||||
export const last = (v: any) => isArray(v) ? arrayLast(v) : ''
|
export const last = (v: any) => isArray(v) ? arrayLast(v) : ''
|
||||||
export const first = (v: any) => isArray(v) ? v[0] : ''
|
export const first = (v: any) => isArray(v) ? v[0] : ''
|
||||||
export const reverse = (v: any[]) => [...v].reverse()
|
export const reverse = (v: any[]) => [...v].reverse()
|
||||||
export const sort = <T>(v: T[], arg: (lhs: T, rhs: T) => number) => v.sort(arg)
|
|
||||||
|
export function sort<T> (this: FilterImpl, arr: T[], property?: string) {
|
||||||
|
const getValue = (obj: Scope) => property ? this.context.getFromScope(obj, property.split('.')) : obj
|
||||||
|
return toArray(arr).sort((lhs, rhs) => {
|
||||||
|
lhs = getValue(lhs)
|
||||||
|
rhs = getValue(rhs)
|
||||||
|
return lhs < rhs ? -1 : (lhs > rhs ? 1 : 0)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export const size = (v: string | any[]) => (v && v.length) || 0
|
export const size = (v: string | any[]) => (v && v.length) || 0
|
||||||
|
|
||||||
export function map<T1, T2> (arr: {[key: string]: T1}[], arg: string): T1[] {
|
export function map<T1, T2> (this: FilterImpl, arr: Scope[], property: string) {
|
||||||
return toArray(arr).map(v => v[arg])
|
return toArray(arr).map(obj => this.context.getFromScope(obj, property.split('.')))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function concat<T1, T2> (v: T1[], arg: T2[] | T2): (T1 | T2)[] {
|
export function concat<T1, T2> (v: T1[], arg: T2[] | T2): (T1 | T2)[] {
|
||||||
|
|||||||
@@ -42,6 +42,13 @@ describe('filters/array', function () {
|
|||||||
const post = { category: 'foo' }
|
const post = { category: 'foo' }
|
||||||
return test('{{post | map: "category"}}', { post }, 'foo')
|
return test('{{post | map: "category"}}', { post }, 'foo')
|
||||||
})
|
})
|
||||||
|
it('should support nested property', function () {
|
||||||
|
const tpl = '{{ arr | map: "name.first" | join }}'
|
||||||
|
const a = { name: { first: 'Alice' } }
|
||||||
|
const b = { name: { first: 'Bob' } }
|
||||||
|
const c = { name: { first: 'Carol' } }
|
||||||
|
return test(tpl, { arr: [a, b, c] }, 'Alice Bob Carol')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
describe('reverse', function () {
|
describe('reverse', function () {
|
||||||
it('should support reverse', () => test(
|
it('should support reverse', () => test(
|
||||||
@@ -106,11 +113,25 @@ describe('filters/array', function () {
|
|||||||
it('should slice substr by -2,2', () => test('{{ "abc" | slice: -2, 2 }}', 'bc'))
|
it('should slice substr by -2,2', () => test('{{ "abc" | slice: -2, 2 }}', 'bc'))
|
||||||
it('should support array', () => test('{{ "1,2,3,4" | split: "," | slice: 1,2 | join }}', '2 3'))
|
it('should support array', () => test('{{ "1,2,3,4" | split: "," | slice: 1,2 | join }}', '2 3'))
|
||||||
})
|
})
|
||||||
it('should support sort', function () {
|
describe('sort', function () {
|
||||||
return test('{% assign my_array = "zebra, octopus, giraffe, Sally Snake"' +
|
it('should support sort', function () {
|
||||||
' | split: ", " %}' +
|
return test('{% assign my_array = "zebra, octopus, giraffe, Sally Snake"' +
|
||||||
'{{ my_array | sort | join: ", " }}',
|
' | split: ", " %}' +
|
||||||
'Sally Snake, giraffe, octopus, zebra')
|
'{{ my_array | sort | join: ", " }}',
|
||||||
|
'Sally Snake, giraffe, octopus, zebra')
|
||||||
|
})
|
||||||
|
it('should support sort by key', function () {
|
||||||
|
const tpl = '{{ arr | sort: "name" | map: "name" | join }}'
|
||||||
|
const arr = [{ name: 'Bob' }, { name: 'Carol' }, { name: 'Alice' }]
|
||||||
|
return test(tpl, { arr }, 'Alice Bob Carol')
|
||||||
|
})
|
||||||
|
it('should support sort by nested property', function () {
|
||||||
|
const tpl = '{{ arr | sort: "name.first" | map: "name.first" | join }}'
|
||||||
|
const a = { name: { first: 'Alice' } }
|
||||||
|
const b = { name: { first: 'Bob' } }
|
||||||
|
const c = { name: { first: 'Carol' } }
|
||||||
|
return test(tpl, { arr: [b, c, a, c] }, 'Alice Bob Carol Carol')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
describe('uniq', function () {
|
describe('uniq', function () {
|
||||||
it('should uniq string list', function () {
|
it('should uniq string list', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user