Skip to content

Commit

Permalink
refactor: del fromPairs
Browse files Browse the repository at this point in the history
  • Loading branch information
cole committed Jun 20, 2024
1 parent f1837b7 commit 19bc13b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 54 deletions.
10 changes: 4 additions & 6 deletions src/layout/components/fullscreen/screenfull.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fromPairs, head } from 'lodash-es'
import { head, reduce, set } from 'lodash-es'

const methodMap = [
['requestFullscreen', 'exitFullscreen', 'fullscreenElement', 'fullscreenEnabled', 'fullscreenchange', 'fullscreenerror',],
Expand All @@ -14,10 +14,8 @@ const methods = methodMap.find((list) => {
})

const defaultMethod = head(methodMap)
const result = (methods || []).map((method, index) => {
return [defaultMethod[index], method]
})

const native = fromPairs(result)
const native = reduce(methods || [], (result, method, key) => {
return set(result, defaultMethod[key], method)
}, {})

export default native
32 changes: 14 additions & 18 deletions src/packages/base-field/utils/valueEnum.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Badge, Space } from 'ant-design-vue'
import { compact, fromPairs, isArray, isNumber, isObject, isString, map } from 'lodash-es'
import { compact, isArray, isNumber, isObject, isString, map, reduce, set } from 'lodash-es'
import { isEmpty } from '@/utils'

export function valueEnumToOptions (valueEnum = {}) {
Expand All @@ -16,24 +16,20 @@ export function valueEnumToOptions (valueEnum = {}) {

export function optionsToValueEnum (options = [], fieldNames) {
const { value = 'value', label = 'label', children = 'children' } = fieldNames || {}
const traverseOptions = (values) => {
const result = []
if (isArray(values) && values.length !== 0) {
values.forEach((item) => {
const key = item[value], text = item[label]
const curChildren = item[children]
if (!(isEmpty(key) || isEmpty(text))) {
result.push([key, text])
}
if (isArray(curChildren) && curChildren.length !== 0) {
result.push(...traverseOptions(curChildren))
}
})
}
return result
const traverseOptions = (values = [], result) => {
return reduce(values, (_, option = {}) => {
const key = option[value], text = option[label]
if (!(isEmpty(key) || isEmpty(text))) {
set(result, key, text)
}
const curChildren = option[children]
if (isArray(curChildren) && curChildren.length !== 0) {
traverseOptions(curChildren, result)
}
return result
}, result)
}
const result = traverseOptions(options)
return fromPairs(result)
return traverseOptions(options, {})
}

export function valueEnumToText (text, valueEnum = {}) {
Expand Down
38 changes: 26 additions & 12 deletions src/packages/descriptions/__tests__/Descriptions.test.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import Descriptions from '../index'
import { Tooltip } from 'ant-design-vue'
import Descriptions from '../index'
import mountTest from '../../../../tests/shared/mountTest'

describe('Descriptions', () => {
it(`render`, async () => {
const wrapper = mount(Descriptions)
expect(wrapper.exists()).toBeTruthy()
})
mountTest(Descriptions)

const columns = [
{
title: () => 'label',
valueType: 'text',
dataIndex: 'text',
index: '1'
},
{
title: 'number',
valueType: 'number',
dataIndex: 'number',
index: '2'
},
]

const children = [
<Descriptions.Item label={'label'}/>,
<Descriptions.Item label={'label'}>
123
</Descriptions.Item>,
<Descriptions.Item valueType={'text'} label={'label'}/>
]
it(`props`, async () => {
const wrapper = mount(Descriptions, {
props: {
Expand All @@ -23,13 +43,7 @@ describe('Descriptions', () => {
},
slots: {
default: () => {
return [
<Descriptions.Item label={'label'}/>,
<Descriptions.Item label={'label'}>
123
</Descriptions.Item>,
<Descriptions.Item valueType={'text'} label={'label'}/>
]
return []
},
extra: () => {
return (<Tooltip>123</Tooltip>)
Expand Down
4 changes: 2 additions & 2 deletions src/packages/form/form/Dependency.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineComponent, unref } from 'vue'
import ColWrap from '../helpers/ColWrap'
import { useFormInstance } from '../base-form'
import { isFunction, set, transform } from 'lodash-es'
import { isFunction, reduce, set } from 'lodash-es'
import { cloneProxyToRaw } from '@/utils/props-util'

export default defineComponent({
Expand All @@ -23,7 +23,7 @@ export default defineComponent({
const { grid } = unref(formProps)
const { name: namePathList, colProps } = props

const slotScope = transform(namePathList, (result, namePath) => {
const slotScope = reduce(namePathList, (result, namePath) => {
if (namePath && getModelValue && isFunction(getModelValue)) {
const value = getModelValue(namePath)
return set(result, namePath, cloneProxyToRaw(value))
Expand Down
4 changes: 2 additions & 2 deletions src/packages/table/compatible/search/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { computed, defineComponent, unref } from 'vue'
import { Field } from '@/packages/form'
import BaseSearch from './BaseSearch'
import { namePathToString } from '@/packages/form/utils'
import { pick, set, transform } from 'lodash-es'
import { pick, reduce, set } from 'lodash-es'
import { isEmpty } from '@/utils'

function filterSearchColumns (columns) {
Expand All @@ -20,7 +20,7 @@ export default defineComponent({
},
setup (props, { attrs }) {
const defaultColumns = filterSearchColumns(props.columns)
const initialValues = transform(defaultColumns, (result, column) => {
const initialValues = reduce(defaultColumns, (result, column) => {
const namePath = column.key || column.dataIndex
if (namePath && !isEmpty(column.initialValue)) {
return set(result, namePath, column.initialValue)
Expand Down
18 changes: 9 additions & 9 deletions src/packages/table/components/column-setting/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SettingOutlined } from '@ant-design/icons-vue'
import TreeList from './TreeList'
import { useSharedContext } from '../../hooks/useSharedContext'
import { useLocaleReceiver } from '@/packages/locale-provider'
import { fromPairs, map } from 'lodash-es'
import { reduce, set } from 'lodash-es'
import classNames from '@/utils/classNames/bind'
import styles from './style/index.module.scss'

Expand All @@ -29,11 +29,11 @@ export default defineComponent({

function onCheckClick (evt) {
const { checked: targetChecked } = evt.target
const values = map(unref(columnsMap), (column, key) => {
const values = reduce(unref(columnsMap), (result, column, key) => {
const checked = column.disable ? column.checked : targetChecked
return [key, { ...column, checked: checked }]
})
setColumnsMap && setColumnsMap(fromPairs(values))
return set(result, key, { ...column, checked: checked })
}, {})
setColumnsMap && setColumnsMap(values)
}

function onClearClick () {
Expand Down Expand Up @@ -61,11 +61,11 @@ export default defineComponent({
} else {
keys.splice(dropIndex + 1, 0, target)
}
const values = keys.map((key, order) => {
const values = reduce(keys, (result, key, order) => {
const column = unref(columnsMap)[key] || {}
return [key, { ...column, order }]
})
setColumnsMap && setColumnsMap(fromPairs(values))
return set(result, key, { ...column, order })
}, {})
setColumnsMap && setColumnsMap(values)
}

return () => {
Expand Down
10 changes: 5 additions & 5 deletions src/packages/table/hooks/useTableColumns.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { computed, ref, unref, watch } from 'vue'
import useCustomRender from './useCustomRender'
import tryOnScopeDispose from '@/utils/hooks/tryOnScopeDispose'
import { fromPairs, isBoolean, isObject, map } from 'lodash-es'
import { isBoolean, isObject, map, reduce, set } from 'lodash-es'

function genColumnsMap (columns) {
const values = columns.map((column, index) => {
return reduce(columns, (result, column, index) => {
const checked = isBoolean(column.checked) ? column.checked : true
const disable = (column.filters || column.sorter) ? true : column.disable
return [column.key, { ...column, checked, disable, order: index }]
})
return fromPairs(values)
const value = { ...column, checked, disable, order: index }
return set(result, column.key, value)
}, {})
}

function useTableColumns (props) {
Expand Down

0 comments on commit 19bc13b

Please sign in to comment.