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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kaokei/utils

v1.0.4

Published

kaokei utils

Readme

@kaokei/utils

npm version npm downloads bundle JSDocs License

Example

以下是常量的标准定义形式,其中key,value,label是每个对象的必需属性,并且key和value的属性值必须唯一。

而且除了key,value,label属性以外可以增加任意其他属性,但是要求每个对象的属性名需要一致。

注意 特殊情况下,key属性可以不存在,此时和key相关的函数不能使用。主要是为了兼容有些场景不需要key属性,比如后端接口返回的枚举值没有key属性。

export const EXAMPLE_OPTIONS = [
  {
    key: 'BOY', // 唯一
    value: 1, // 必需且唯一
    label: '男生', // 必需
    tag: '高达', // 可选
    color: 'red', // 可选
  },
  {
    key: 'GIRL', // 唯一
    value: 2, // 必需且唯一
    label: '女生', // 必需
    tag: '小仙女', // 可选
    color: 'green', // 可选
  },
] as const

获取所有key的数组

const keys = getKeys(EXAMPLE_OPTIONS)
keys === ['BOY', 'GIRL']

获取所有value的数组

const values = getValues(EXAMPLE_OPTIONS)
values === [1, 2]

获取key-value的映射对象

const keyValueMap = getKeyValueMap(EXAMPLE_OPTIONS)
keyValueMap === { BOY: 1, GIRL: 2 }

获取key-label的映射对象

const keyLabelMap = getKeyLabelMap(EXAMPLE_OPTIONS)
keyLabelMap === { BOY: '男生', GIRL: '女生' }

获取value-key的映射对象

const valueKeyMap = getValueKeyMap(EXAMPLE_OPTIONS)
valueKeyMap === { 1: 'BOY', 2: 'GIRL' }

获取value-label的映射对象

const valueLabelMap = getValueLabelMap(EXAMPLE_OPTIONS)
valueLabelMap === { 1: '男生', 2: '女生' }

获取key-option的映射对象

const keyMap = getKeyOptionMap(EXAMPLE_OPTIONS)
keyMap === {
  BOY: {
    key: 'BOY',
    value: 1,
    label: '男生',
    tag: '高达',
    color: 'red'
  },
  GIRL: {
    key: 'GIRL',
    value: 2,
    label: '女生',
    tag: '小仙女',
    color: 'green'
  }
}

获取value-option的映射对象

const valueMap = getValueOptionMap(EXAMPLE_OPTIONS)
valueMap === {
  1: {
    key: 'BOY',
    value: 1,
    label: '男生',
    tag: '高达',
    color: 'red'
  },
  2: {
    key: 'GIRL',
    value: 2,
    label: '女生',
    tag: '小仙女',
    color: 'green'
  }
}

获取key-自定义属性的映射对象

// 获取key-tag的映射
const keyTagMap = getMapByKey(EXAMPLE_OPTIONS, 'tag')
keyTagMap === { BOY: '高达', GIRL: '小仙女' }

获取value-自定义属性的映射对象

// 获取value-color的映射
const valueColorMap = getMapByValue(EXAMPLE_OPTIONS, 'color')
valueColorMap === { 1: 'red', 2: 'green' }

License

MIT License © 2023-PRESENT kaokei