array-filters.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var _ = require('../util')
  2. var Path = require('../parse/path')
  3. /**
  4. * Attempt to convert non-Array objects to array.
  5. * This is the default filter installed to every v-repeat
  6. * directive.
  7. *
  8. * @param {*} obj
  9. * @return {Array}
  10. * @private
  11. */
  12. exports._objToArray = function (obj) {
  13. if (_.isArray(obj)) {
  14. return obj
  15. }
  16. if (!_.isObject(obj)) {
  17. _.warn(
  18. 'Invalid value for v-repeat: ' + obj +
  19. '\nOnly Arrays and Objects are allowed.'
  20. )
  21. return
  22. }
  23. var res = []
  24. var val, data
  25. for (var key in obj) {
  26. res.push({
  27. key: key,
  28. value: obj[key]
  29. })
  30. }
  31. res._converted = true
  32. return res
  33. }
  34. /**
  35. * Filter filter for v-repeat
  36. *
  37. * @param {String} searchKey
  38. * @param {String} [delimiter]
  39. * @param {String} dataKey
  40. */
  41. exports.filterBy = function (arr, searchKey, delimiter, dataKey) {
  42. // allow optional `in` delimiter
  43. // because why not
  44. if (delimiter && delimiter !== 'in') {
  45. dataKey = delimiter
  46. }
  47. // get the search string
  48. var search =
  49. _.stripQuotes(searchKey) ||
  50. this.$get(searchKey)
  51. if (!search) {
  52. return arr
  53. }
  54. search = search.toLowerCase()
  55. // get the optional dataKey
  56. dataKey =
  57. dataKey &&
  58. (stripQuotes(dataKey) || this.$get(dataKey))
  59. return arr.filter(function (item) {
  60. return dataKey
  61. ? contains(Path.get(item, dataKey), search)
  62. : contains(item, search)
  63. })
  64. }
  65. /**
  66. * Filter filter for v-repeat
  67. *
  68. * @param {String} sortKey
  69. * @param {String} reverseKey
  70. */
  71. exports.orderBy = function (arr, sortKey, reverseKey) {
  72. var key =
  73. _.stripQuotes(sortKey) ||
  74. this.$get(sortKey)
  75. if (!key) {
  76. return arr
  77. }
  78. var order = 1
  79. if (reverseKey) {
  80. if (reverseKey === '-1') {
  81. order = -1
  82. } else if (reverseKey.charCodeAt(0) === 0x21) { // !
  83. reverseKey = reverseKey.slice(1)
  84. order = this.$get(reverseKey) ? 1 : -1
  85. } else {
  86. order = this.$get(reverseKey) ? -1 : 1
  87. }
  88. }
  89. // sort on a copy to avoid mutating original array
  90. return arr.slice().sort(function (a, b) {
  91. a = Path.get(a, key)
  92. b = Path.get(b, key)
  93. return a === b ? 0 : a > b ? order : -order
  94. })
  95. }
  96. /**
  97. * String contain helper
  98. *
  99. * @param {*} val
  100. * @param {String} search
  101. */
  102. function contains (val, search) {
  103. /* jshint eqeqeq: false */
  104. if (_.isObject(val)) {
  105. for (var key in val) {
  106. if (contains(val[key], search)) {
  107. return true
  108. }
  109. }
  110. } else if (val != null) {
  111. return val.toString().toLowerCase().indexOf(search) > -1
  112. }
  113. }