npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

lg.data

v0.0.6

Published

数据转换(数据类型转换、结构转换、数组数据处理、树形数据处理)

Downloads

25

Readme

数据转换(数据类型转换、结构转换、数组数据处理、树形数据处理)

六哥开源前后端脚手架

  • https://gitee.com/lgx1992/lg-soar

六哥npm工具库

import {
    toObject,
    toSet,
    toMap,
    toBoolean,
    toNumber,
    toPromise,
    toOptions,
    toBooleanArray,
    toNumberArray,
    toStringArray,
    toPromiseArray,
    toAge,
    splice,
    grouping,
    sum,
    forEach,
    map,
    filter,
    find,
    sort,
    toTreeData,
    treeFlat,
    findLink,
    findLinkR,
    queryTree
} from "lg.data";

const list = [];

for (let i = 0; i < 11; i++) {
    list.push({
        value: i,
        label: String(i + 1),
        pid: -1
    })
}

for (let i = 11; i < 22; i++) {
    list.push({
        value: i,
        label: String(i + 1),
        pid: i % 5
    })
}
for (let i = 22; i < 33; i++) {
    list.push({
        value: i,
        label: String(i + 1),
        pid: 11
    })
}

console.log('toObject:转对象')
console.log(toObject(list, 'value')) // 使用字段定义key
console.log(toObject(list, (x: any) => x.value)) // 使用函数定义key
console.log(toObject(list, 'value', 'label')) // 使用字段定义key 和 value
console.log(toObject(list, (x: any) => x.value, x => x.label)) // 使用函数定义key 和 value

console.log('toSet:转Set')
console.log(toSet(list))
console.log(toSet(list, 'value')) // 使用字段定义value
console.log(toSet(list, x => x.value)) // 使用函数定义value

console.log('toMap:转Map')
console.log(toMap(list, 'value')) // 使用字段定义key
console.log(toMap(list, x => x.value)) // 使用函数定义key
console.log(toMap(list, 'value', 'label')) // 使用字段定义key 和 value
console.log(toMap(list, x => x.value, x => x.label)) // 使用函数定义key 和 value

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, (_: number, i: number) => i === 2)
console.log(ls3)

console.log('grouping:分组')
console.log(grouping(list, 'value')) // 使用字段分组
console.log(grouping(list, (x: any) => String(x.value % 2))) // 使用函数分组
console.log(grouping(list, 'value', 'label')) // 使用字段分组,并使用字段映射值
console.log(grouping(list, (x: any) => String(x.value % 2), x => x.label)) // 使用函数分组,并使用函数映射值

function add(a: string, b: string | number) {
    return String(Number(a) + Number(b))
}
console.log('sum:求和')
console.log(sum([0, '1', 2, '3'])) // 基础求和
console.log(sum(list, 'value')) // 单字段求和
console.log(sum(list, ['value', 'pid'])) // 多字段求和
console.log(sum([0, '1', 2, '3'], add)) // 自定义求和函数
console.log(sum(list, 'value', add)) // 自定义求和函数
console.log(sum(list, ['value', 'pid'], add)) // 自定义求和函数

console.log('toTreeData:数组转树')
const tree = toTreeData(list, 'value', 'pid', 'children', -1);
console.log(tree)

console.log('treeFlat:树转数组')
console.log(treeFlat(tree))

console.log('map:转换')
console.log(map(tree, (x) => {
    return {
        id: x.value,
        name: x.label,
    }
}))

console.log('filter:筛选')
console.log(filter(tree, (x) => x.value % 2 === 0))

console.log('find:查找')
console.log(find(tree, (x) => x.value === 22))

console.log('findLink:查找层级链')
console.log(findLink(tree, (x) => x.value === 22))

console.log('findLinkR:查找层级链')
console.log(findLinkR(tree, (x) => x.value === 22))

console.log('queryTree:查寻树上下节点')
console.log(queryTree(tree, (x) => x.value === 11))

console.log('sort:排序')
let cl1 = JSON.parse(JSON.stringify(tree));
sort(cl1, (a, b) => a.value - b.value)
console.log(cl1)
cl1 = JSON.parse(JSON.stringify(tree));
sort(cl1, '-value')
console.log(cl1)
cl1 = JSON.parse(JSON.stringify(tree));
sort(cl1, ['-value', 'label'])
console.log(cl1)

console.log('forEach:遍历')
let i = 1;
forEach(tree, (x) => {
    console.log(i++, x)
})