feat: array functions
This commit is contained in:
parent
90270fde4e
commit
0ec3976820
|
|
@ -1,8 +1,13 @@
|
|||
import { pick } from 'lodash-es'
|
||||
import { Menu } from 'd2/utils/menu.js'
|
||||
import { flattenObjectArray } from 'd2/utils/array.js'
|
||||
import routes from 'virtual:generated-pages'
|
||||
|
||||
|
||||
console.log('routes', routes)
|
||||
|
||||
console.log(flattenObjectArray(routes, 'children', (item, _) => pick(item, ['name', 'path', 'meta'])))
|
||||
|
||||
export const dashboardIndexMenu = new Menu('控制台')
|
||||
.url('/dashboard')
|
||||
.icon('icon-park-outline:dashboard')
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
import { get, omit, isArray } from 'lodash-es'
|
||||
|
||||
/**
|
||||
* Always return array
|
||||
* @param {*} value source value
|
||||
* @returns array
|
||||
*/
|
||||
export function safeArray (value) {
|
||||
return isArray(value) ? value : []
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the value passed in is a non empty array
|
||||
* @param {*} value source value
|
||||
* @returns boolean
|
||||
*/
|
||||
export function notEmptyArray (value) {
|
||||
return safeArray(value).length > 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an array with a nested structure to a one-dimensional array
|
||||
* @param {array} source array structure consisting of objects
|
||||
* @param {string} childrenKey fields of child elements
|
||||
* @param {function} iterator can decide how to take values when saving each item
|
||||
* @returns {array} flatten array
|
||||
* @example
|
||||
* from:
|
||||
* [
|
||||
* {
|
||||
* id: '1',
|
||||
* children: [
|
||||
* {
|
||||
* id: '1-1',
|
||||
* children: [
|
||||
* { id: '1-1-1' },
|
||||
* { id: '1-1-2' }
|
||||
* ]
|
||||
* },
|
||||
* { id: '1-2' }
|
||||
* ]
|
||||
* },
|
||||
* { id: '2' }
|
||||
* ]
|
||||
* to:
|
||||
* [
|
||||
* { id: '1' },
|
||||
* { id: '1-1' },
|
||||
* { id: '1-1-1' },
|
||||
* { id: '1-1-2' },
|
||||
* { id: '1-2' },
|
||||
* { id: '2' }
|
||||
* ]
|
||||
*/
|
||||
export function flattenObjectArray (
|
||||
source = [],
|
||||
childrenKey = 'children',
|
||||
iterator = (item, _) => omit(item, [childrenKey])
|
||||
) {
|
||||
const result = []
|
||||
safeArray(source).forEach((item, index) => {
|
||||
result.push(iterator(item, index))
|
||||
const children = get(item, childrenKey)
|
||||
if (notEmptyArray(children)) {
|
||||
result.push(...flattenObjectArray(children, childrenKey, iterator))
|
||||
}
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
import { nanoid } from 'nanoid'
|
||||
import { isArray, cloneDeep, omit } from 'lodash-es'
|
||||
import { warn } from 'd2/utils/error.js'
|
||||
|
||||
export const _k_id = '_id'
|
||||
export const _k_children = 'children'
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
import { camelCase, mapKeys } from 'lodash-es'
|
||||
|
||||
export const keyPrefix = (source, prefix) => mapKeys(source, (_, key) => camelCase(`${prefix}-${key}`))
|
||||
Loading…
Reference in New Issue