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

name-case-transform

v1.0.6

Published

name case transform tool

Downloads

17

Readme

风格转换工具

name case transform tool

文档:中文 | English

  • 支持 Pascal、小驼峰、连字符、下划线命名风格互转。

  • 支持字符串和 JSON(key)。

API

caseTransform((dataSource: [String | Object]), (targetCase: String), [(sourceCase: String)]);
  • dataSource: 想要转换的数据。

  • targetCase: 目标命名风格。

    • -: 连字符命名风格,比如:my-name.
    • _: 下划线命名风格, 比如: my_name.
    • A: Pascal 命名风格, eg: MyName.
    • a: 小驼峰命名风格, eg: myName.
  • 数据源命名风格: 非必须。(程序会自动推断源数据风格,但是判断可能不及预期(多种风格混合)。这个时候,可以指定数据源命名风格。)

    • -: 连字符命名风格,比如:my-name.
    • _: 下划线命名风格, 比如: my_name.
    • A: Pascal 命名风格, eg: MyName.
    • a: 小驼峰命名风格, eg: myName.

用法

引入

import caseTransform from "name-case-transform";

转换

字符串:

let res = caseTransform("my_name", "A"); // MyName;

// 指定源数据命名风格
let res = caseTransform("my_name", "A", "_"); // MyName;

JSON:

let data = {
  res_code: 4000,
  res_list: [
    {
      my_name: "admin1",
      password: "12323",
    },
    {
      my_name: "admin2",
      password: "123123",
    },
  ],
};

let res = caseTransform(data, "a");

// 指定源数据命名风格
let res = caseTransform(data, "a", "_");

结果:

{
  resCode:4000,
  resList:[
    {
      myName:"admin1",
      password:"12323"
    },
    {
      myName:"admin2",
      password:"123123"
    }
  ]
}

Q-A

  1. 如果几种风格混合在一起,如何自动推断类型?(按照下面先后顺序采用)
  • 字符串或者 JSON(key)值,包含_, 推断为下划线风格("_")。
  • 字符串或者 JSON(key)值,包含-, 推断为连字符风格("-")。
  • 字符串或者 JSON(key)值,第一个字母为大写,推断为 Pascal 风格。
  • 其他情况,推断为小驼峰风格。

其他

兼容性问题

因为程序中多是使用正则,所以在不支持正则的环境将会报错!!!