Skip to content

Commit dd4c33a

Browse files
committed
filters
1 parent 8f3d06e commit dd4c33a

File tree

4 files changed

+52
-56
lines changed

4 files changed

+52
-56
lines changed

src/filters/array-filters.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
var _ = require('../util')
21
import { getPath } from '../parsers/path'
3-
var toArray = require('../directives/public/for')._postProcess
2+
import { toArray, isArray, isObject, isPlainObject } from '../util'
3+
import vFor from '../directives/public/for'
4+
const convertArray = vFor._postProcess
45

56
/**
67
* Limit filter for arrays
@@ -9,7 +10,7 @@ var toArray = require('../directives/public/for')._postProcess
910
* @param {Number} offset (Decimal expected)
1011
*/
1112

12-
exports.limitBy = function (arr, n, offset) {
13+
export function limitBy (arr, n, offset) {
1314
offset = offset ? parseInt(offset, 10) : 0
1415
return typeof n === 'number'
1516
? arr.slice(offset, offset + n)
@@ -24,8 +25,8 @@ exports.limitBy = function (arr, n, offset) {
2425
* @param {String} ...dataKeys
2526
*/
2627

27-
exports.filterBy = function (arr, search, delimiter) {
28-
arr = toArray(arr)
28+
export function filterBy (arr, search, delimiter) {
29+
arr = convertArray(arr)
2930
if (search == null) {
3031
return arr
3132
}
@@ -38,7 +39,7 @@ exports.filterBy = function (arr, search, delimiter) {
3839
// because why not
3940
var n = delimiter === 'in' ? 3 : 2
4041
// extract and flatten keys
41-
var keys = _.toArray(arguments, n).reduce(function (prev, cur) {
42+
var keys = toArray(arguments, n).reduce(function (prev, cur) {
4243
return prev.concat(cur)
4344
}, [])
4445
var res = []
@@ -70,20 +71,20 @@ exports.filterBy = function (arr, search, delimiter) {
7071
* @param {String} reverse
7172
*/
7273

73-
exports.orderBy = function (arr, sortKey, reverse) {
74-
arr = toArray(arr)
74+
export function orderBy (arr, sortKey, reverse) {
75+
arr = convertArray(arr)
7576
if (!sortKey) {
7677
return arr
7778
}
7879
var order = (reverse && reverse < 0) ? -1 : 1
7980
// sort on a copy to avoid mutating original array
8081
return arr.slice().sort(function (a, b) {
8182
if (sortKey !== '$key') {
82-
if (_.isObject(a) && '$value' in a) a = a.$value
83-
if (_.isObject(b) && '$value' in b) b = b.$value
83+
if (isObject(a) && '$value' in a) a = a.$value
84+
if (isObject(b) && '$value' in b) b = b.$value
8485
}
85-
a = _.isObject(a) ? getPath(a, sortKey) : a
86-
b = _.isObject(b) ? getPath(b, sortKey) : b
86+
a = isObject(a) ? getPath(a, sortKey) : a
87+
b = isObject(b) ? getPath(b, sortKey) : b
8788
return a === b ? 0 : a > b ? order : -order
8889
})
8990
}
@@ -97,15 +98,15 @@ exports.orderBy = function (arr, sortKey, reverse) {
9798

9899
function contains (val, search) {
99100
var i
100-
if (_.isPlainObject(val)) {
101+
if (isPlainObject(val)) {
101102
var keys = Object.keys(val)
102103
i = keys.length
103104
while (i--) {
104105
if (contains(val[keys[i]], search)) {
105106
return true
106107
}
107108
}
108-
} else if (_.isArray(val)) {
109+
} else if (isArray(val)) {
109110
i = val.length
110111
while (i--) {
111112
if (contains(val[i], search)) {

src/filters/index.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
var _ = require('../util')
1+
import { toArray, debounce as _debounce } from '../util'
2+
export * from './array-filters'
23

34
/**
45
* Stringify value.
56
*
67
* @param {Number} indent
78
*/
89

9-
exports.json = {
10+
export const json = {
1011
read: function (value, indent) {
1112
return typeof value === 'string'
1213
? value
@@ -25,7 +26,7 @@ exports.json = {
2526
* 'abc' => 'Abc'
2627
*/
2728

28-
exports.capitalize = function (value) {
29+
export function capitalize (value) {
2930
if (!value && value !== 0) return ''
3031
value = value.toString()
3132
return value.charAt(0).toUpperCase() + value.slice(1)
@@ -35,7 +36,7 @@ exports.capitalize = function (value) {
3536
* 'abc' => 'ABC'
3637
*/
3738

38-
exports.uppercase = function (value) {
39+
export function uppercase (value) {
3940
return (value || value === 0)
4041
? value.toString().toUpperCase()
4142
: ''
@@ -45,7 +46,7 @@ exports.uppercase = function (value) {
4546
* 'AbC' => 'abc'
4647
*/
4748

48-
exports.lowercase = function (value) {
49+
export function lowercase (value) {
4950
return (value || value === 0)
5051
? value.toString().toLowerCase()
5152
: ''
@@ -58,7 +59,7 @@ exports.lowercase = function (value) {
5859
*/
5960

6061
var digitsRE = /(\d{3})(?=\d)/g
61-
exports.currency = function (value, currency) {
62+
export function currency (value, currency) {
6263
value = parseFloat(value)
6364
if (!isFinite(value) || (!value && value !== 0)) return ''
6465
currency = currency != null ? currency : '$'
@@ -88,8 +89,8 @@ exports.currency = function (value, currency) {
8889
* e.g. ['single', 'double', 'triple', 'multiple']
8990
*/
9091

91-
exports.pluralize = function (value) {
92-
var args = _.toArray(arguments, 1)
92+
export function pluralize (value) {
93+
var args = toArray(arguments, 1)
9394
return args.length > 1
9495
? (args[value % 10 - 1] || args[args.length - 1])
9596
: (args[0] + (value === 1 ? '' : 's'))
@@ -103,16 +104,10 @@ exports.pluralize = function (value) {
103104
* @return {Function}
104105
*/
105106

106-
exports.debounce = function (handler, delay) {
107+
export function debounce (handler, delay) {
107108
if (!handler) return
108109
if (!delay) {
109110
delay = 300
110111
}
111-
return _.debounce(handler, delay)
112+
return _debounce(handler, delay)
112113
}
113-
114-
/**
115-
* Install special array filters
116-
*/
117-
118-
_.extend(exports, require('./array-filters'))

src/index.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
import Vue from './instance/vue'
2+
import directives from './directives/public'
3+
import elementDirectives from './directives/element'
4+
import * as filters from './filters'
25
import { inBrowser } from './util'
36

7+
Vue.version = '1.0.8'
8+
9+
/**
10+
* Vue and every constructor that extends Vue has an
11+
* associated options object, which can be accessed during
12+
* compilation steps as `this.constructor.options`.
13+
*
14+
* These can be seen as the default options of every
15+
* Vue instance.
16+
*/
17+
18+
Vue.options = {
19+
directives,
20+
elementDirectives,
21+
filters,
22+
transitions: {},
23+
components: {},
24+
partials: {},
25+
replace: true
26+
}
27+
28+
export default Vue
29+
430
// devtools global hook
531
/* istanbul ignore if */
632
if (process.env.NODE_ENV !== 'production') {
733
if (inBrowser && window.__VUE_DEVTOOLS_GLOBAL_HOOK__) {
834
window.__VUE_DEVTOOLS_GLOBAL_HOOK__.emit('init', Vue)
935
}
1036
}
11-
12-
Vue.version = '1.0.8'
13-
export default Vue

src/instance/vue.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import directives from '../directives/public'
2-
import elementDirectives from '../directives/element'
3-
import filters from '../filters'
4-
51
import initMixin from './internal/init'
62
import stateMixin from './internal/state'
73
import eventsMixin from './internal/events'
@@ -44,25 +40,6 @@ class Vue {
4440
}
4541
}
4642

47-
/**
48-
* Vue and every constructor that extends Vue has an
49-
* associated options object, which can be accessed during
50-
* compilation steps as `this.constructor.options`.
51-
*
52-
* These can be seen as the default options of every
53-
* Vue instance.
54-
*/
55-
56-
Vue.options = {
57-
directives,
58-
elementDirectives,
59-
filters,
60-
transitions: {},
61-
components: {},
62-
partials: {},
63-
replace: true
64-
}
65-
6643
// install internals
6744
initMixin(Vue)
6845
stateMixin(Vue)

0 commit comments

Comments
 (0)