| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- var _ = require('../util')
- var Path = require('../parse/path')
- /**
- * Attempt to convert non-Array objects to array.
- * This is the default filter installed to every v-repeat
- * directive.
- *
- * @param {*} obj
- * @return {Array}
- * @private
- */
- exports._objToArray = function (obj) {
- if (_.isArray(obj)) {
- return obj
- }
- if (!_.isObject(obj)) {
- _.warn(
- 'Invalid value for v-repeat: ' + obj +
- '\nOnly Arrays and Objects are allowed.'
- )
- return
- }
- var res = []
- var val, data
- for (var key in obj) {
- res.push({
- key: key,
- value: obj[key]
- })
- }
- res._converted = true
- return res
- }
- /**
- * 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) {
- /* jshint eqeqeq: false */
- 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
- }
- }
|