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

@libii/libii-utils

v0.0.4

Published

内部管理系统的公用工具函数

Readme

概述

给内部使用的库

不依赖第三方工具, 只依赖基本 API (浏览器环境 API, js API)

分为两个部分:

  1. node + browser 环境

  2. browser 环境

浏览器环境 API

objectToFormData

将对象转为表单数据

import { assert } from 'chai'

import { objectToFormData } from '#test/index.mjs'

assert.strictEqual(
  objectToFormData({ a: 1, b: 2 }).get('a'),
  '1',
  'objectToFormData 未成功转换',
)

assert.strictEqual(
  objectToFormData({ a: 1, b: 2 }).get('b'),
  '2',
  'objectToFormData 未成功转换',
)

// 复合数据
assert.strictEqual(
  objectToFormData({ a: { b: 1 } }).get('a[b]'),
  '1',
  'objectToFormData 未成功转换',
)

node + browser 环境

findKeyPath

在对象或数组中找到指定 key 的链路

返回的顺序为: [第一节点key, 第二节点key, ..., 当前指定key]

import { assert } from 'chai'

import { findKeyPath } from '#test/index.mjs'

// 默认比较 key, 遍历 children
assert.deepEqual(
  findKeyPath(
    {
      key: 1,
      children: [
        {
          key: 2,
          children: [{ key: 3 }],
        },
        {
          key: 4,
          children: [{ key: 5 }],
        },
      ],
    },
    5,
  ),
  [1, 4, 5],
)

// 改变比较的 key, 遍历的 key 名
assert.deepEqual(
  findKeyPath(
    {
      id: 1,
      list: [
        {
          id: 2,
          list: [{ id: 3 }],
        },
        {
          id: 4,
          list: [{ id: 5 }],
        },
      ],
    },
    3,
    {
      recurseKeyName: 'list',
      diffKeyName: 'id',
    },
  ),
  [1, 2, 3],
)

assert.deepEqual(
  findKeyPath(
    [
      {
        id: '1',
        list: [
          {
            id: '1-1',
            list: [{ id: '1-1-1' }],
          },
          {
            id: '1-2',
            list: [{ id: '1-2-1' }],
          },
        ],
      },
      {
        id: '2',
        list: [
          {
            id: '2-1',
            list: [{ id: '2-1-1' }],
          },
          {
            id: '2-2',
            list: [{ id: '2-2-1' }],
          },
        ],
      },
    ],
    '2-2-1',
    {
      recurseKeyName: 'list',
      diffKeyName: 'id',
    },
  ),
  ['2', '2-2', '2-2-1'],
)

getFileNameFromCD

通过 header 的 Content-Disposition 获取文件名

import { assert } from 'chai'

import { getFileNameFromCD } from '#test/index.mjs'

assert.strictEqual(
  getFileNameFromCD("attachment;filename*=utf-8''研发非人力费用管理.xlsx"),
  '研发非人力费用管理.xlsx',
)

assert.strictEqual(
  getFileNameFromCD(
    "attachment;filename*=utf-8''%E7%A0%94%E5%8F%91%E9%9D%9E%E4%BA%BA%E5%8A%9B%E8%B4%B9%E7%94%A8%E7%AE%A1%E7%90%86.xlsx",
  ),
  '研发非人力费用管理.xlsx',
)

assert.strictEqual(
  getFileNameFromCD('', {
    defaultFileName: '研发非人力费用管理.xlsx',
  }),
  '研发非人力费用管理.xlsx',
)

getType

得到变量的类型

import { assert } from 'chai'

import { getType } from '#test/index.mjs'

assert.strictEqual(getType([]), 'array')
assert.strictEqual(getType({}), 'object')
assert.strictEqual(getType(1), 'number')
assert.strictEqual(getType('yomua'), 'string')
assert.strictEqual(getType(true), 'boolean')
assert.strictEqual(getType(Symbol(1)), 'symbol')
assert.strictEqual(getType(null), 'null')
assert.strictEqual(getType(undefined), 'undefined')
assert.strictEqual(getType(BigInt(1)), 'bigInt')
assert.strictEqual(
  getType(function () {}),
  'function',
)

isType

判断变量的类型

import { assert } from 'chai'

import { isType } from '#test/index.mjs'

assert.strictEqual(isType([], 'array'), true)
assert.strictEqual(isType({}, 'object'), true)
assert.strictEqual(isType(1, 'number'), true)
assert.strictEqual(isType('yomua', 'string'), true)
assert.strictEqual(isType(true, 'boolean'), true)
assert.strictEqual(isType(Symbol(1), 'symbol'), true)
assert.strictEqual(isType(null, 'null'), true)
assert.strictEqual(isType(undefined, 'undefined'), true)
assert.strictEqual(isType(BigInt(1), 'bigInt'), true)
assert.strictEqual(
  isType(function () {}, 'function'),
  true,
)
assert.strictEqual(
  isType(async function () {}, 'asyncFunction'),
  true,
)

isNil

判断变量是否为 nullundefined

import { assert } from 'chai'

import { isNil } from '#test/index.mjs'

assert.strictEqual(isNil(null), true, 'isNil 未成功校验 null')
assert.strictEqual(isNil(undefined), true, 'isNil 未成功校验 undefined')
assert.strictEqual(isNil(), true, 'isNil 未成功校验 undefined')
assert.strictEqual(isNil({}), false, 'isNil 未成功校验 其他值')
assert.strictEqual(isNil(''), false, 'isNil 未成功校验 其他值')
assert.strictEqual(isNil(1), false, 'isNil 未成功校验 其他值')
assert.strictEqual(isNil(true), false, 'isNil 未成功校验 其他值')
assert.strictEqual(isNil(Symbol(1)), false, 'isNil 未成功校验 其他值')
assert.strictEqual(isNil(BigInt(1)), false, 'isNil 未成功校验 其他值')
assert.strictEqual(isNil([]), false, 'isNil 未成功校验 其他值')
assert.strictEqual(
  isNil(function () {}),
  false,
  'isNil 未成功校验 其他值',
)

parseQueryString

解析查询字符串

import { assert } from 'chai'

import { parseQueryString } from '#test/index.mjs'

assert.deepEqual(parseQueryString('?key=value1&key2=value2'), {
  key: 'value1',
  key2: 'value2',
})