wux-weapp
Version: 
一套组件化、可复用、易扩展的微信小程序 UI 组件库
66 lines (52 loc) • 2.51 kB
JavaScript
import { getDefaultFieldNames } from '../helpers/mixins/fieldNamesBehavior'
const DEFAULT_FIELD_NAMES = getDefaultFieldNames()
export function getRealIndex(value = 0, min = 0, max) {
    if (value <= min) return min
    if (value >= max) return max
    return value
}
export function getRealIndexes(indexes = [], cols = []) {
    return cols.reduce((acc, col, idx) => ([...acc, getRealIndex(indexes[idx], 0, col.length - 1)]), [])
}
export function getIndexFromValue(value, col = [], fieldNames = DEFAULT_FIELD_NAMES) {
    return getRealIndex(col.map((v) => v[fieldNames.value]).indexOf(value), 0, col.length - 1)
}
export function getIndexesFromValues(values = [], cols = [], fieldNames = DEFAULT_FIELD_NAMES) {
    return cols.reduce((acc, col, idx) => ([...acc, getIndexFromValue(values[idx], col, fieldNames)]), [])
}
export function getValueFromIndex(index, col = [], fieldNames = DEFAULT_FIELD_NAMES) {
    return col[getRealIndex(index, 0, col.length - 1)][fieldNames.value]
}
export function getValuesFromIndexes(indexes = [], cols = [], fieldNames = DEFAULT_FIELD_NAMES) {
    return cols.reduce((acc, col, idx) => ([...acc, getValueFromIndex(indexes[idx], col, fieldNames)]), [])
}
export function getRealValue(value = '', col = [], fieldNames = DEFAULT_FIELD_NAMES) {
    return col.length > 0 ? col[getIndexFromValue(value, col, fieldNames)][fieldNames.value] : ''
}
export function getRealValues(values = [], cols = [], fieldNames = DEFAULT_FIELD_NAMES) {
    return cols.length > 0 ? cols.reduce((acc, col, idx) => ([...acc, getRealValue(values[idx], col, fieldNames)]), []) : []
}
export function getLabelFromIndex(index, col = [], member) {
    return member ? col[index] && col[index][member] : col[index]
}
export function getLabelsFromIndexes(indexes, cols = [], member) {
    return cols.reduce((acc, col, idx) => ([...acc, getLabelFromIndex(indexes[idx], col, member)]), [])
}
export function isMultiPicker(data = []) {
    return !data ? false : Array.isArray(data[0])
}
export function getRealCol(data = [], fieldNames = DEFAULT_FIELD_NAMES) {
    return data.map((v) => {
        if (typeof v !== 'object') {
            return {
                [fieldNames.value]: v,
                [fieldNames.label]: v,
            }
        }
        return v
    })
}
export function getRealCols(data = [], fieldNames = DEFAULT_FIELD_NAMES) {
    const cols = isMultiPicker(data) ? data : [data]
    return cols.reduce((acc, col) => ([...acc, getRealCol(col, fieldNames)]), [])
}