Skip to content

Commit

Permalink
feat: sort by key, see #227
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed May 15, 2020
1 parent d39f65c commit 4f17c94
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
16 changes: 13 additions & 3 deletions src/builtin/filters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ import { isArray, last as arrayLast } from '../../util/underscore'
import { toArray } from '../../util/collection'
import { isTruthy } from '../../render/boolean'
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 last = (v: any) => isArray(v) ? arrayLast(v) : ''
export const first = (v: any) => isArray(v) ? v[0] : ''
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 function map<T1, T2> (arr: {[key: string]: T1}[], arg: string): T1[] {
return toArray(arr).map(v => v[arg])
export function map<T1, T2> (this: FilterImpl, arr: Scope[], property: string) {
return toArray(arr).map(obj => this.context.getFromScope(obj, property.split('.')))
}

export function concat<T1, T2> (v: T1[], arg: T2[] | T2): (T1 | T2)[] {
Expand Down
31 changes: 26 additions & 5 deletions test/integration/builtin/filters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ describe('filters/array', function () {
const post = { category: '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 () {
it('should support reverse', () => test(
Expand Down Expand Up @@ -106,11 +113,25 @@ describe('filters/array', function () {
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 sort', function () {
return test('{% assign my_array = "zebra, octopus, giraffe, Sally Snake"' +
' | split: ", " %}' +
'{{ my_array | sort | join: ", " }}',
'Sally Snake, giraffe, octopus, zebra')
describe('sort', function () {
it('should support sort', function () {
return test('{% assign my_array = "zebra, octopus, giraffe, Sally Snake"' +
' | split: ", " %}' +
'{{ 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 () {
it('should uniq string list', function () {
Expand Down

0 comments on commit 4f17c94

Please sign in to comment.