mx-extension.js 3.7 KB

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