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

@lweb-utils/string

v1.0.2

Published

@lweb-utils/string

Downloads

5

Readme

@lweb-utils/string

字符串相关工具函数

npm i @lweb-utils/string --save

1. fixedZero

fixedZero(number)

生成两位数字格式的字符串,如0612

参数

number(number): 需要生成的数字。

返回值

(string): 生成的两位数字符串。

示例

const num1 = fixedZero(8);
// '08'
const num2 = fixedZero(16);
// '16'

2. numberWithCommas

numberWithCommas(number, decimal)

生成逗号分隔的三位数格式字符串。

参数

number(string): 纯数字或小数点组成的字符串。 decimal(number): 小数位数。

返回值

(string): 逗号分隔的数字字符串。

示例

// 使用默认小数位数
const result = numberWithCommas('1234567');
// '1,234,567.00'
const result = numberWithCommas('1234567.1234');
// '1,234,567.12'

// 指定小数位数
const result = numberWithCommas('1234567.1234', 3);
// '1,234,567.123'

3. percentStr

percentStr(number, decimal)

数字字符串转换成百分比格式。

参数

number(string|number): 需要转换的数字或字符串。 decimal(number): 转换成百分比后小数位数。

返回值

(string): 百分比格式的字符串。

示例

// 使用默认小数位数
percentStr(0.123);
// '12.30%'

// 指定小数位数
percentStr(0.123, 1);
// '12.3%'

4. randomStr

randomStr(length)

生成指定长度的随机字符串。

参数

length(number): 随机字符串的长度,默认为4。

返回值

(string): 随机字符串,包含大小写字母和数字。

示例

// 使用默认长度
const str = randomStr();
// '4Ex6'

// 使用指定长度
const str = randomStr(6);
// '5r8DdU'

5. toCapital

toCapital(string)

字符串转换成首字母大写的格式。

参数

string(string): 字符串。

返回值

(string): 首字母大写的字符串。

示例

const str = toCapital('abcd');
// 'Abcd'