-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray-filters.js
87 lines (81 loc) · 1.85 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
var _ = require('../util')
var Path = require('../parsers/path')
/**
* Filter filter for v-repeat
*
* @param {String} searchKey
* @param {String} [delimiter]
* @param {String} dataKey
*/
exports.filterBy = function (arr, searchKey, delimiter, dataKey) {
// allow optional `in` delimiter
// because why not
if (delimiter && delimiter !== 'in') {
dataKey = delimiter
}
// get the search string
var search =
_.stripQuotes(searchKey) ||
this.$get(searchKey)
if (!search) {
return arr
}
search = ('' + search).toLowerCase()
// get the optional dataKey
dataKey =
dataKey &&
(_.stripQuotes(dataKey) || this.$get(dataKey))
return arr.filter(function (item) {
return dataKey
? contains(Path.get(item, dataKey), search)
: contains(item, search)
})
}
/**
* Filter filter for v-repeat
*
* @param {String} sortKey
* @param {String} reverseKey
*/
exports.orderBy = function (arr, sortKey, reverseKey) {
var key =
_.stripQuotes(sortKey) ||
this.$get(sortKey)
if (!key) {
return arr
}
var order = 1
if (reverseKey) {
if (reverseKey === '-1') {
order = -1
} else if (reverseKey.charCodeAt(0) === 0x21) { // !
reverseKey = reverseKey.slice(1)
order = this.$get(reverseKey) ? 1 : -1
} else {
order = this.$get(reverseKey) ? -1 : 1
}
}
// sort on a copy to avoid mutating original array
return arr.slice().sort(function (a, b) {
a = Path.get(a, key)
b = Path.get(b, key)
return a === b ? 0 : a > b ? order : -order
})
}
/**
* String contain helper
*
* @param {*} val
* @param {String} search
*/
function contains (val, search) {
if (_.isObject(val)) {
for (var key in val) {
if (contains(val[key], search)) {
return true
}
}
} else if (val != null) {
return val.toString().toLowerCase().indexOf(search) > -1
}
}