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

@teaghy/g-utils

v0.1.1

Published

> 此项目仅示范性封装自身常用的工具函数.

Downloads

143

Readme

g-tools

此项目仅示范性封装自身常用的工具函数.

安装

yarn 安装方式

yarn add @teaghy/g-utils

npm 安装方式

npm install @teaghy/g-utils

使用方式

import { arrayToTree, getDiffTree, treeToList } from '@teaghy/g-utils';
const arr = [
  {
    pid: 1,
    id: 2,
    name: '0-1-1',
  },
  {
    id: 0,
    name: '0-0',
  },
  {
    pid: 0,
    id: 3,
    name: '0-2',
  },
  {
    pid: 0,
    id: 1,
    name: '0-1',
  },
];
const treeData = arrayToTree(arr);
// console.log(treeData);

const list = treeToList(treeData);
// console.log(list);

 const oldTree = [{
    id: '1',
    value: 'a',
    children: [
      { id: '2', value: 'b', children: [] },
      {
        id: '3',
        value: 'c',
        children: [
          { id: '4', value: 'd', children: [] },
          { id: '5', value: 'e', children: [] },
          { id: '6', value: 'f', children: [] },
        ],
      },
    ],
  }];

  const newTree = [{
    id: '1',
    value: 'a',
    children: [
      { id: '7', value: 'g', children: [] },
      {
        id: '3',
        value: 'cc',
        children: [
          { id: '5', value: 'e', children: [] },
          { id: '4', value: 'd', children: [] },
        ],
      },
      { id: '6', value: 'f', children: [] },
    ],
  }];
  const result = getDiffTree(newTree, oldTree);

函数文档

| 方法 | 描述 | 参数 | 返回值 | |:----------- |:------------- |:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |:---------------------------------------- | | arrayToTree | 平行数据结构转树形结构 | (list, options) 接收两个参数: 1. 平行数据结构数据 2. options: ReplaceFiledOptionsType 替换数据中的 id,pid,children 字段为 每个数据 中对应的字段 默认为 { id = 'id', pid = 'pid', children = 'children' } | Array | | treeToList | 树形结构转换平行数据结构 | (tree, options) 接收两个参数: 1. 树形结构数据 2. options: ReplaceFiledOptionsType 替换数据中的 id,pid,children 字段为 每个数据 中对应的字段 默认为 { id = 'id', pid = 'pid', children = 'children' } | Array | | getDiffTree | 获取两个json对象的区别 | getDiffTree(newData: TreeNodeType[], oldData: TreeNodeType[], option: CompareDiffOptionType = {})1. 新树2.旧树3.CompareDiffOptionType {  indexEffect?: boolean; // 是否检测索引变化  key?: string; // 对象对比的唯一键(获取的属性的路径a.b.c)  childrenKey: string // 子集属性的键compareMethod?: (...arg: any) => boolean; // 比较的方法(默认比较除children外的整个对象) } | 返回全部的差异对象{updates, deletes, adds, moves} |