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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shencom/utils-array

v1.1.0

Published

数组相关工具方法

Downloads

41

Readme

@shencom/utils-array

数组相关工具方法

Install

pnpm add @shencom/utils

# or

pnpm add @shencom/utils-array

Basic Usage

import { ArrayToObject, ToArray } from '@shencom/utils';
// import { ArrayToObject, ToArray } from '@shencom/utils-array';

Methods

ToArray

  • 说明: 转数组
  • 类型: (val?: Nullable<Arrayable<T>>): Array<T>
  • 参数:
    • val - 值
  • 示例:
    ToArray('1'); // ['1']
    ToArray([1]); // [1]
    ToArray(null); // []
    ToArray(undefined); // []
    ToArray(true); // [true]
    ToArray(false); // [false]
    ToArray(''); // ['']

ArrayToObject

  • 说明: 数组转对象

  • 类型: (list: T[], key: string): Record<string, T[]>

  • 参数:

    • list - 数据列表
    • key - 指定使用字段作为 key
  • 示例:

    arr.ArrayToObject([], ''); // {}
    
    const data = [
      { id: 1, b: 11 },
      { id: 4, b: 44, c: 444 },
    ];
    arr.ArrayToObject(data, 'id');
    /*
      {
        '1': [{ id: 1, b: 11 }],
        '4': [{ id: 4, b: 44, c: 444 }],
      }
    */
    
    arr.ArrayToObject(data, 'cc');
    /*
      {
        '444': [{ id: 4, b: 44, c: 444 }],
        undefined: [{ id: 1, b: 11 }],
      };
    */

ArrayFlatten

  • 说明: 扁平化数组
  • 类型: (array?: Nullable<Arrayable<T | Array<T>>>, depth?: number): Array<T>
  • 参数:
    • array - 多维数组
    • depth - 最大递归深度,默认:1
  • 示例:
    ArrayFlatten([]); // []
    ArrayFlatten([1]); // [1]
    ArrayFlatten([1, [2]]); // [1, 2]
    ArrayFlatten([1, [2], [3]]); // [1, 2, 3]
    ArrayFlatten([1, [2, [3]]]); // [1, 2, [3]]
    ArrayFlatten([1, [2, [3]]], 2); // [1, 2, 3]

ArrayToTree

  • 说明: 数组转树
  • 类型: (arr: T, options?: ArrayToTreeMaps<C>): Tree<T>
  • 参数:
    • arr - 数组数据
    • depth - 最大递归深度,默认:1
  • 示例:
    const arr1 = [
      { id: '1', pid: '0' },
      { id: '1-1', pid: '1' },
      { id: '1-2', pid: '1' },
      { id: '1-1-1', pid: '1-1' },
    ];
    const tree1 = [
      {
        id: '1',
        pid: '0',
        children: [
          {
            id: '1-1',
            pid: '1',
            children: [{ id: '1-1-1', pid: '1-1' }],
          },
          {
            id: '1-2',
            pid: '1',
          },
        ],
      },
    ];
    const res = arr.ArrayToTree(arr1);

ArrayKeyMap

  • 说明: 对数组中每一项的进行映射
  • 类型: (array: T, maps: P): Unfurl<Mutable<Res>>[]
  • 参数:
    • array - 数组
    • maps - 映射关系
  • 示例:
    const arr = [
      { a: '1', b: true, c: 3 },
      { a: '2', b: false, c: 4 },
    ];
    const res = ArrayKeyMap(arr, { aa: 'a', bb: 'b' } as const);
    // res = [
    //   { aa: '1', bb: true, c: 3 },
    //   { aa: '2', bb: false, c: 4 },
    // ];