mx-extension.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. export default {
  2. install(Vue) {
  3. // Array ext.
  4. Array.prototype.first = function(predicate) {
  5. if (predicate == null || typeof predicate !== 'function') {
  6. return this.length ? this[0] : null
  7. }
  8. for (let i = 0; i < this.length; i++) {
  9. const item = this[i]
  10. if (predicate(item)) return item
  11. }
  12. }
  13. Array.prototype.last = function(predicate) {
  14. if (predicate == null || typeof predicate !== 'function') {
  15. return this.length ? this[this.length - 1] : null
  16. }
  17. for (let i = this.length - 1; i >= 0; i--) {
  18. const item = this[i]
  19. if (predicate(item)) return item
  20. }
  21. }
  22. Array.prototype.groupBy = function(propGetter, groupName = 'label', listName = 'options') {
  23. const results = []
  24. this.forEach(item => {
  25. const groupValue = propGetter(item)
  26. let exists = results.find(i => i[groupName] == groupValue)
  27. if (!exists) {
  28. exists = {}
  29. exists[groupName] = groupValue
  30. exists[listName] = []
  31. results.push(exists)
  32. }
  33. exists[listName].push(item)
  34. })
  35. return results
  36. }
  37. Array.prototype.remove = function(item) {
  38. // remove first match item in array
  39. const idx = this.findIndex(i => i == item)
  40. if (idx == -1) return
  41. this.splice(idx, 1)
  42. }
  43. Array.prototype.sum = function(propGetter = null) {
  44. let result = 0
  45. this.forEach(item => {
  46. if (propGetter) {
  47. result += propGetter(item) * 1
  48. } else {
  49. result += item * 1
  50. }
  51. })
  52. return result
  53. }
  54. Array.prototype.clear = function() {
  55. this.splice(0, this.length)
  56. }
  57. // String
  58. String.prototype.castObject = function() {
  59. // cast if it is json
  60. try {
  61. return JSON.parse(this)
  62. } catch (e) {
  63. console.log('extension `castObject` failed, will return raw value.', e)
  64. return this
  65. }
  66. }
  67. String.prototype.tailingFix = function(fix) {
  68. return this.endsWith(fix) ? this : this + fix
  69. }
  70. // Global func.
  71. Vue.prototype.deepClone = function(obj) {
  72. if (!obj) return obj
  73. return JSON.parse(JSON.stringify(obj))
  74. }
  75. // 动态表转objects
  76. Vue.prototype.reverseDynamicTableToObjects = function({ columns, rows }) {
  77. return rows.map(row => {
  78. const obj = {}
  79. columns.forEach((col, idx) => obj[col] = row[idx])
  80. return obj
  81. })
  82. }
  83. Vue.prototype.dynamicTableExchangeRowColumn = function({ columns, rows }) {
  84. let newColumns = []
  85. let newRows = []
  86. for (let rowIdx = 0; rowIdx < columns.length; rowIdx++) {
  87. const rowName = columns[rowIdx]
  88. const newRow = [rowName]
  89. for (let colIdx = 0; colIdx < rows.length; colIdx++) {
  90. const row = rows[colIdx]
  91. const val = rowIdx < row.length ? row[rowIdx] : ''
  92. newRow.push(val)
  93. }
  94. if (rowIdx == 0) {
  95. newColumns = newRow
  96. } else {
  97. newRows.push(newRow)
  98. }
  99. }
  100. return {
  101. columns: newColumns,
  102. rows: newRows
  103. }
  104. }
  105. Vue.prototype.convertStandardTree = function(root, labelKey = 'label', valueKey = 'value', childrenKey = 'children') {
  106. const convertor = (node) => ({
  107. label: node[labelKey],
  108. value: node[valueKey],
  109. children: node[childrenKey]?.map(convertor)
  110. })
  111. const arrRoot = Array.isArray(root) ? root : [root]
  112. return arrRoot.map(convertor)
  113. }
  114. }
  115. }