-
-
Notifications
You must be signed in to change notification settings - Fork 240
/
array.ts
42 lines (36 loc) · 1.25 KB
/
array.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { isArray, last } from '../../util/underscore'
import { isTruthy } from '../../render/syntax'
export default {
'join': (v: any[], arg: string) => v.join(arg === undefined ? ' ' : arg),
'last': (v: any) => isArray(v) ? last(v) : '',
'first': (v: any) => isArray(v) ? v[0] : '',
'map': map,
'reverse': (v: any[]) => [...v].reverse(),
'sort': <T>(v: T[], arg: (lhs: T, rhs: T) => number) => v.sort(arg),
'size': (v: string | any[]) => (v && v.length) || 0,
'concat': concat,
'slice': slice,
'uniq': uniq,
'where': where
}
function map<T1, T2> (arr: {[key: string]: T1}[], arg: string): T1[] {
return arr.map(v => v[arg])
}
function concat<T1, T2> (v: T1[], arg: T2[] | T2): (T1 | T2)[] {
return Array.prototype.concat.call(v, arg)
}
function slice<T> (v: T[], begin: number, length: number = 1): T[] {
begin = begin < 0 ? v.length + begin : begin
return v.slice(begin, begin + length)
}
function where<T> (arr: T[], property: string, value?: any): T[] {
return arr.filter(obj => value === undefined ? isTruthy(obj[property]) : obj[property] === value)
}
function uniq<T> (arr: T[]): T[] {
const u = {}
return (arr || []).filter(val => {
if (u.hasOwnProperty(String(val))) return false
u[String(val)] = true
return true
})
}