|
|
@@ -1,7 +1,71 @@
|
|
|
var utils = require('./utils'),
|
|
|
get = utils.get,
|
|
|
slice = [].slice,
|
|
|
- QUOTE_RE = /^'.*'$/
|
|
|
+ QUOTE_RE = /^'.*'$/,
|
|
|
+ filters = module.exports = utils.hash()
|
|
|
+
|
|
|
+/**
|
|
|
+ * 'abc' => 'Abc'
|
|
|
+ */
|
|
|
+filters.capitalize = function (value) {
|
|
|
+ if (!value && value !== 0) return ''
|
|
|
+ value = value.toString()
|
|
|
+ return value.charAt(0).toUpperCase() + value.slice(1)
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 'abc' => 'ABC'
|
|
|
+ */
|
|
|
+filters.uppercase = function (value) {
|
|
|
+ return (value || value === 0)
|
|
|
+ ? value.toString().toUpperCase()
|
|
|
+ : ''
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 'AbC' => 'abc'
|
|
|
+ */
|
|
|
+filters.lowercase = function (value) {
|
|
|
+ return (value || value === 0)
|
|
|
+ ? value.toString().toLowerCase()
|
|
|
+ : ''
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 12345 => $12,345.00
|
|
|
+ */
|
|
|
+filters.currency = function (value, sign) {
|
|
|
+ if (!value && value !== 0) return ''
|
|
|
+ sign = sign || '$'
|
|
|
+ var s = Math.floor(value).toString(),
|
|
|
+ i = s.length % 3,
|
|
|
+ h = i > 0 ? (s.slice(0, i) + (s.length > 3 ? ',' : '')) : '',
|
|
|
+ f = '.' + value.toFixed(2).slice(-2)
|
|
|
+ return sign + h + s.slice(i).replace(/(\d{3})(?=\d)/g, '$1,') + f
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * args: an array of strings corresponding to
|
|
|
+ * the single, double, triple ... forms of the word to
|
|
|
+ * be pluralized. When the number to be pluralized
|
|
|
+ * exceeds the length of the args, it will use the last
|
|
|
+ * entry in the array.
|
|
|
+ *
|
|
|
+ * e.g. ['single', 'double', 'triple', 'multiple']
|
|
|
+ */
|
|
|
+filters.pluralize = function (value) {
|
|
|
+ var args = slice.call(arguments, 1)
|
|
|
+ return args.length > 1
|
|
|
+ ? (args[value - 1] || args[args.length - 1])
|
|
|
+ : (args[value - 1] || args[0] + 's')
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * A special filter that takes a handler function,
|
|
|
+ * wraps it so it only gets triggered on specific keypresses.
|
|
|
+ *
|
|
|
+ * v-on only
|
|
|
+ */
|
|
|
|
|
|
var keyCodes = {
|
|
|
enter : 13,
|
|
|
@@ -14,143 +78,89 @@ var keyCodes = {
|
|
|
esc : 27
|
|
|
}
|
|
|
|
|
|
-var filters = module.exports = {
|
|
|
-
|
|
|
- /**
|
|
|
- * 'abc' => 'Abc'
|
|
|
- */
|
|
|
- capitalize: function (value) {
|
|
|
- if (!value && value !== 0) return ''
|
|
|
- value = value.toString()
|
|
|
- return value.charAt(0).toUpperCase() + value.slice(1)
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 'abc' => 'ABC'
|
|
|
- */
|
|
|
- uppercase: function (value) {
|
|
|
- return (value || value === 0)
|
|
|
- ? value.toString().toUpperCase()
|
|
|
- : ''
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 'AbC' => 'abc'
|
|
|
- */
|
|
|
- lowercase: function (value) {
|
|
|
- return (value || value === 0)
|
|
|
- ? value.toString().toLowerCase()
|
|
|
- : ''
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 12345 => $12,345.00
|
|
|
- */
|
|
|
- currency: function (value, sign) {
|
|
|
- if (!value && value !== 0) return ''
|
|
|
- sign = sign || '$'
|
|
|
- var s = Math.floor(value).toString(),
|
|
|
- i = s.length % 3,
|
|
|
- h = i > 0 ? (s.slice(0, i) + (s.length > 3 ? ',' : '')) : '',
|
|
|
- f = '.' + value.toFixed(2).slice(-2)
|
|
|
- return sign + h + s.slice(i).replace(/(\d{3})(?=\d)/g, '$1,') + f
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * args: an array of strings corresponding to
|
|
|
- * the single, double, triple ... forms of the word to
|
|
|
- * be pluralized. When the number to be pluralized
|
|
|
- * exceeds the length of the args, it will use the last
|
|
|
- * entry in the array.
|
|
|
- *
|
|
|
- * e.g. ['single', 'double', 'triple', 'multiple']
|
|
|
- */
|
|
|
- pluralize: function (value) {
|
|
|
- var args = slice.call(arguments, 1)
|
|
|
- return args.length > 1
|
|
|
- ? (args[value - 1] || args[args.length - 1])
|
|
|
- : (args[value - 1] || args[0] + 's')
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * A special filter that takes a handler function,
|
|
|
- * wraps it so it only gets triggered on specific keypresses.
|
|
|
- */
|
|
|
- key: function (handler, key) {
|
|
|
- if (!handler) return
|
|
|
- var code = keyCodes[key]
|
|
|
- if (!code) {
|
|
|
- code = parseInt(key, 10)
|
|
|
- }
|
|
|
- return function (e) {
|
|
|
- if (e.keyCode === code) {
|
|
|
- handler.call(this, e)
|
|
|
- }
|
|
|
+filters.key = function (handler, key) {
|
|
|
+ if (!handler) return
|
|
|
+ var code = keyCodes[key]
|
|
|
+ if (!code) {
|
|
|
+ code = parseInt(key, 10)
|
|
|
+ }
|
|
|
+ return function (e) {
|
|
|
+ if (e.keyCode === code) {
|
|
|
+ handler.call(this, e)
|
|
|
}
|
|
|
- },
|
|
|
-
|
|
|
- filterBy: function (arr, searchKey, delimiter, dataKey) {
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- // allow optional `in` delimiter
|
|
|
- // because why not
|
|
|
- if (delimiter && delimiter !== 'in') {
|
|
|
- dataKey = delimiter
|
|
|
- }
|
|
|
+/**
|
|
|
+ * Filter filter for v-repeat
|
|
|
+ */
|
|
|
+filters.filterBy = function (arr, searchKey, delimiter, dataKey) {
|
|
|
|
|
|
- // get the search string
|
|
|
- var search = stripQuotes(searchKey) || this.$get(searchKey)
|
|
|
- if (!search) return arr
|
|
|
- search = search.toLowerCase()
|
|
|
+ // allow optional `in` delimiter
|
|
|
+ // because why not
|
|
|
+ if (delimiter && delimiter !== 'in') {
|
|
|
+ dataKey = delimiter
|
|
|
+ }
|
|
|
|
|
|
- // get the optional dataKey
|
|
|
- dataKey = dataKey && (stripQuotes(dataKey) || this.$get(dataKey))
|
|
|
+ // get the search string
|
|
|
+ var search = stripQuotes(searchKey) || this.$get(searchKey)
|
|
|
+ if (!search) return arr
|
|
|
+ search = search.toLowerCase()
|
|
|
|
|
|
- // convert object to array
|
|
|
- if (!Array.isArray(arr)) {
|
|
|
- arr = utils.objectToArray(arr)
|
|
|
- }
|
|
|
+ // get the optional dataKey
|
|
|
+ dataKey = dataKey && (stripQuotes(dataKey) || this.$get(dataKey))
|
|
|
|
|
|
- return arr.filter(function (item) {
|
|
|
- return dataKey
|
|
|
- ? contains(get(item, dataKey), search)
|
|
|
- : contains(item, search)
|
|
|
- })
|
|
|
+ // convert object to array
|
|
|
+ if (!Array.isArray(arr)) {
|
|
|
+ arr = utils.objectToArray(arr)
|
|
|
+ }
|
|
|
|
|
|
- },
|
|
|
+ return arr.filter(function (item) {
|
|
|
+ return dataKey
|
|
|
+ ? contains(get(item, dataKey), search)
|
|
|
+ : contains(item, search)
|
|
|
+ })
|
|
|
|
|
|
- orderBy: function (arr, sortKey, reverseKey) {
|
|
|
+}
|
|
|
|
|
|
- var key = stripQuotes(sortKey) || this.$get(sortKey)
|
|
|
- if (!key) return arr
|
|
|
+filters.filterBy.computed = true
|
|
|
|
|
|
- // convert object to array
|
|
|
- if (!Array.isArray(arr)) {
|
|
|
- arr = utils.objectToArray(arr)
|
|
|
- }
|
|
|
+/**
|
|
|
+ * Sort fitler for v-repeat
|
|
|
+ */
|
|
|
+filters.orderBy = function (arr, sortKey, reverseKey) {
|
|
|
|
|
|
- var order = 1
|
|
|
- if (reverseKey) {
|
|
|
- if (reverseKey === '-1') {
|
|
|
- order = -1
|
|
|
- } else if (reverseKey.charAt(0) === '!') {
|
|
|
- reverseKey = reverseKey.slice(1)
|
|
|
- order = this.$get(reverseKey) ? 1 : -1
|
|
|
- } else {
|
|
|
- order = this.$get(reverseKey) ? -1 : 1
|
|
|
- }
|
|
|
- }
|
|
|
+ var key = stripQuotes(sortKey) || this.$get(sortKey)
|
|
|
+ if (!key) return arr
|
|
|
|
|
|
- // sort on a copy to avoid mutating original array
|
|
|
- return arr.slice().sort(function (a, b) {
|
|
|
- a = get(a, key)
|
|
|
- b = get(b, key)
|
|
|
- return a === b ? 0 : a > b ? order : -order
|
|
|
- })
|
|
|
+ // convert object to array
|
|
|
+ if (!Array.isArray(arr)) {
|
|
|
+ arr = utils.objectToArray(arr)
|
|
|
+ }
|
|
|
|
|
|
+ var order = 1
|
|
|
+ if (reverseKey) {
|
|
|
+ if (reverseKey === '-1') {
|
|
|
+ order = -1
|
|
|
+ } else if (reverseKey.charAt(0) === '!') {
|
|
|
+ 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 = get(a, key)
|
|
|
+ b = get(b, key)
|
|
|
+ return a === b ? 0 : a > b ? order : -order
|
|
|
+ })
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+filters.orderBy.computed = true
|
|
|
+
|
|
|
// Array filter helpers -------------------------------------------------------
|
|
|
|
|
|
/**
|
|
|
@@ -177,8 +187,4 @@ function stripQuotes (str) {
|
|
|
if (QUOTE_RE.test(str)) {
|
|
|
return str.slice(1, -1)
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-// mark computed filters
|
|
|
-filters.filterBy.computed = true
|
|
|
-filters.orderBy.computed = true
|
|
|
+}
|