综合 lg.contextmenu、lg.data、lg.table-copy、lg.validator
六哥开源前后端脚手架:https://gitee.com/lgx1992/lg-soar
具体使用请参考 对应项目:
import {
toObject,
toSet,
toMap,
grouping,
toBoolean,
toNumber,
toPromise,
toOptions,
toBooleanArray,
toNumberArray,
toStringArray,
toPromiseArray,
toAge, splice
} from "lg.data";
const list = [];
for (let i = 0; i < 11; i++) {
list.push({
value: i,
label: String(i + 1)
})
}
console.log('toObject:转对象')
console.log(toObject(list, 'value'))
console.log(toObject(list, x => x.value))
console.log(toObject(list, 'value', 'label'))
console.log(toObject(list, x => x.value, x => x.label))
console.log('toSet:转Set')
console.log(toSet(list))
console.log(toSet(list, 'value'))
console.log(toSet(list, x => x.value))
console.log('toMap:转Map')
console.log(toMap(list, 'value'))
console.log(toMap(list, x => x.value))
console.log(toMap(list, 'value', 'label'))
console.log(toMap(list, x => x.value, x => x.label))
console.log('grouping:分组')
console.log(grouping(list, 'value'))
console.log(grouping(list, x => x.value % 2))
console.log(grouping(list, 'value', 'label'))
console.log(grouping(list, x => x.value % 2, x => x.label))
console.log('toOptions:转选项数组(用于下拉选择)')
console.log(toOptions('a,b,c'))
console.log(toOptions(['a', 'b', 'c']))
console.log(toOptions('a:1,b:2,c:3'))
console.log(toOptions({ a: 1, b: 2, c: 3 }))
console.log(toOptions(list))
console.log('toBoolean:转布尔值')
console.log(toBoolean(true))
console.log(toBoolean(1))
console.log(toBoolean(''))
console.log(toBoolean('false'))
console.log('toBooleanArray:转布尔值数组')
console.log(toBooleanArray([false, true]))
console.log(toBooleanArray(['false', 'true']))
console.log(toBooleanArray('false,true'))
console.log('toNumber:转数值')
console.log(toNumber('1.25'))
console.log(toNumber(1.25))
console.log('toNumberArray:转数值数组')
console.log(toNumberArray([1, 2.25]))
console.log(toNumberArray(['1', '2.25']))
console.log(toNumberArray('1,2.25'))
console.log('toPromise:转Promise')
console.log(toPromise(Promise.resolve(1.25)))
console.log(toPromise(1.25))
console.log('toPromiseArray:转 Promise 数组')
console.log(toPromiseArray(['https://gitee.com/lgx1992/lg-soar', Promise.resolve('https://gitee.com/lgx1992/lg-soar')]))
console.log('toStringArray:转字符串数组')
console.log(toStringArray([1, 2.25]))
console.log(toStringArray(['1', '2.25']))
console.log(toStringArray('1,2.25'))
console.log('toAge:日期转年龄')
console.log(toAge(new Date(1992, 3, 5)))
console.log(toAge(new Date(1992, 3, 5).getTime()))
console.log(toAge(new Date(1992, 3, 5), new Date(2025, 3, 10)))
console.log('splice:数组删除')
const ls1 = [1, 2, 3, 2, 2];
splice(ls1, 2)
console.log(ls1)
const ls2 = [1, 2, 3, 2, 2];
splice(ls2, (x: number) => x === 2)
console.log(ls2)
const ls3 = [1, 2, 3, 2, 2];
splice(ls3, (x: number, i: number) => i === 2)
console.log(ls3)