utils.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { getDefaultFieldNames } from '../helpers/mixins/fieldNamesBehavior'
  2. const DEFAULT_FIELD_NAMES = getDefaultFieldNames()
  3. export function getRealIndex(value = 0, min = 0, max) {
  4. if (value <= min) return min
  5. if (value >= max) return max
  6. return value
  7. }
  8. export function getRealIndexes(indexes = [], cols = []) {
  9. return cols.reduce((acc, col, idx) => ([...acc, getRealIndex(indexes[idx], 0, col.length - 1)]), [])
  10. }
  11. export function getIndexFromValue(value, col = [], fieldNames = DEFAULT_FIELD_NAMES) {
  12. return getRealIndex(col.map((v) => v[fieldNames.value]).indexOf(value), 0, col.length - 1)
  13. }
  14. export function getIndexesFromValues(values = [], cols = [], fieldNames = DEFAULT_FIELD_NAMES) {
  15. return cols.reduce((acc, col, idx) => ([...acc, getIndexFromValue(values[idx], col, fieldNames)]), [])
  16. }
  17. export function getValueFromIndex(index, col = [], fieldNames = DEFAULT_FIELD_NAMES) {
  18. return col[getRealIndex(index, 0, col.length - 1)][fieldNames.value]
  19. }
  20. export function getValuesFromIndexes(indexes = [], cols = [], fieldNames = DEFAULT_FIELD_NAMES) {
  21. return cols.reduce((acc, col, idx) => ([...acc, getValueFromIndex(indexes[idx], col, fieldNames)]), [])
  22. }
  23. export function getRealValue(value = '', col = [], fieldNames = DEFAULT_FIELD_NAMES) {
  24. return col.length > 0 ? col[getIndexFromValue(value, col, fieldNames)][fieldNames.value] : ''
  25. }
  26. export function getRealValues(values = [], cols = [], fieldNames = DEFAULT_FIELD_NAMES) {
  27. return cols.length > 0 ? cols.reduce((acc, col, idx) => ([...acc, getRealValue(values[idx], col, fieldNames)]), []) : []
  28. }
  29. export function getLabelFromIndex(index, col = [], member) {
  30. return member ? col[index] && col[index][member] : col[index]
  31. }
  32. export function getLabelsFromIndexes(indexes, cols = [], member) {
  33. return cols.reduce((acc, col, idx) => ([...acc, getLabelFromIndex(indexes[idx], col, member)]), [])
  34. }
  35. export function isMultiPicker(data = []) {
  36. return !data ? false : Array.isArray(data[0])
  37. }
  38. export function getRealCol(data = [], fieldNames = DEFAULT_FIELD_NAMES) {
  39. return data.map((v) => {
  40. if (typeof v !== 'object') {
  41. return {
  42. [fieldNames.value]: v,
  43. [fieldNames.label]: v,
  44. }
  45. }
  46. return v
  47. })
  48. }
  49. export function getRealCols(data = [], fieldNames = DEFAULT_FIELD_NAMES) {
  50. const cols = isMultiPicker(data) ? data : [data]
  51. return cols.reduce((acc, col) => ([...acc, getRealCol(col, fieldNames)]), [])
  52. }