-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray-filters.js
158 lines (146 loc) · 3.54 KB
/
array-filters.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { getPath } from '../parsers/path'
import vFor from '../directives/public/for'
import {
toArray,
toNumber,
isArray,
isObject,
isPlainObject
} from '../util/index'
const convertArray = vFor._postProcess
/**
* Limit filter for arrays
*
* @param {Number} n
* @param {Number} offset (Decimal expected)
*/
export function limitBy (arr, n, offset) {
offset = offset ? parseInt(offset, 10) : 0
n = toNumber(n)
return typeof n === 'number'
? arr.slice(offset, offset + n)
: arr
}
/**
* Filter filter for arrays
*
* @param {String} search
* @param {String} [delimiter]
* @param {String} ...dataKeys
*/
export function filterBy (arr, search, delimiter) {
arr = convertArray(arr)
if (search == null) {
return arr
}
if (typeof search === 'function') {
return arr.filter(search)
}
// cast to lowercase string
search = ('' + search).toLowerCase()
// allow optional `in` delimiter
// because why not
var n = delimiter === 'in' ? 3 : 2
// extract and flatten keys
var keys = Array.prototype.concat.apply([], toArray(arguments, n))
var res = []
var item, key, val, j
for (var i = 0, l = arr.length; i < l; i++) {
item = arr[i]
val = (item && item.$value) || item
j = keys.length
if (j) {
while (j--) {
key = keys[j]
if ((key === '$key' && contains(item.$key, search)) ||
contains(getPath(val, key), search)) {
res.push(item)
break
}
}
} else if (contains(item, search)) {
res.push(item)
}
}
return res
}
/**
* Filter filter for arrays
*
* @param {String|Array<String>|Function} ...sortKeys
* @param {Number} [order]
*/
export function orderBy (arr) {
let comparator = null
let sortKeys
arr = convertArray(arr)
// determine order (last argument)
let args = toArray(arguments, 1)
let order = args[args.length - 1]
if (typeof order === 'number') {
order = order < 0 ? -1 : 1
args = args.length > 1 ? args.slice(0, -1) : args
} else {
order = 1
}
// determine sortKeys & comparator
let firstArg = args[0]
if (!firstArg) {
return arr
} else if (typeof firstArg === 'function') {
// custom comparator
comparator = function (a, b) {
return firstArg(a, b) * order
}
} else {
// string keys. flatten first
sortKeys = Array.prototype.concat.apply([], args)
comparator = function (a, b, i) {
i = i || 0
return i >= sortKeys.length - 1
? baseCompare(a, b, i)
: baseCompare(a, b, i) || comparator(a, b, i + 1)
}
}
function baseCompare (a, b, sortKeyIndex) {
const sortKey = sortKeys[sortKeyIndex]
if (sortKey) {
if (sortKey !== '$key') {
if (isObject(a) && '$value' in a) a = a.$value
if (isObject(b) && '$value' in b) b = b.$value
}
a = isObject(a) ? getPath(a, sortKey) : a
b = isObject(b) ? getPath(b, sortKey) : b
}
return a === b ? 0 : a > b ? order : -order
}
// sort on a copy to avoid mutating original array
return arr.slice().sort(comparator)
}
/**
* String contain helper
*
* @param {*} val
* @param {String} search
*/
function contains (val, search) {
var i
if (isPlainObject(val)) {
var keys = Object.keys(val)
i = keys.length
while (i--) {
if (contains(val[keys[i]], search)) {
return true
}
}
} else if (isArray(val)) {
i = val.length
while (i--) {
if (contains(val[i], search)) {
return true
}
}
} else if (val != null) {
return val.toString().toLowerCase().indexOf(search) > -1
}
}