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

digital-conversion

v1.0.0

Published

A lightweight collection of string and number utilities: reverse, palindrome check, capitalize, thousands separator, random integer and clamp.

Downloads

409

Readme

digital-conversion

npm version npm license node version

一個零依賴的輕量字串與數字工具組,提供反轉字串、迴文判斷、字首大寫、千分位格式化、隨機整數與數值限制等功能。

  • 零依賴、體積小(未壓縮約 4.4 kB)
  • 同時支援 CommonJS 與 ES Modules
  • 支援 Unicode(包含中文)
  • 完整的參數驗證與單元測試

安裝

npm install digital-conversion

需求:Node.js 18 以上。

快速開始

CommonJS

const {
  reverseString,
  isPalindrome,
  capitalizeWords,
  formatThousands,
  randomInt,
  clamp,
} = require('digital-conversion');

reverseString('hello');                          // 'olleh'
isPalindrome('A man, a plan, a canal: Panama');  // true
capitalizeWords('hello world');                  // 'Hello World'
formatThousands(1234567);                        // '1,234,567'
randomInt(1, 6);                                 // 1 到 6 之間的整數(含頭尾)
clamp(42, 0, 10);                                // 10

ES Modules

import { reverseString, formatThousands } from 'digital-conversion';

reverseString('你好');      // '好你'
formatThousands(-9876543); // '-9,876,543'

API

| 函式 | 說明 | 回傳 | | --- | --- | --- | | reverseString(text) | 反轉字串(支援 Unicode) | string | | isPalindrome(text) | 判斷是否為迴文,忽略大小寫、空白與標點 | boolean | | capitalizeWords(text) | 將每個單字字首大寫、其餘小寫 | string | | formatThousands(value) | 將數字加上千分位逗號 | string | | randomInt(min, max) | 產生含頭尾範圍的隨機整數 | number | | clamp(value, min, max) | 將數值限制在指定範圍內 | number |

reverseString(text)

反轉字串,使用展開運算子逐字元處理,因此不會破壞 Unicode 字元。

reverseString('hello');  // 'olleh'
reverseString('你好');    // '好你'
reverseString('');       // ''
  • text {string}:要反轉的字串
  • 回傳 {string}:反轉後的字串
  • 拋出 {TypeError}:當 text 不是字串

isPalindrome(text)

判斷是否為迴文。比較前會忽略大小寫、空白、標點符號,並保留英數字與中文字。

isPalindrome('racecar');                          // true
isPalindrome('A man, a plan, a canal: Panama');  // true
isPalindrome('hello');                            // false
isPalindrome('');                                 // true
  • text {string}:要檢查的字串
  • 回傳 {boolean}:是迴文回傳 true,否則 false
  • 拋出 {TypeError}:當 text 不是字串

capitalizeWords(text)

將每個以空白分隔的單字字首轉為大寫、其餘字母轉為小寫,原始空白與斷行會保留。

capitalizeWords('hello world');       // 'Hello World'
capitalizeWords('hELLO wORLD');       // 'Hello World'
capitalizeWords('  multiple   spaces  '); // '  Multiple   Spaces  '
  • text {string}:要轉換的字串
  • 回傳 {string}:轉換後的字串
  • 拋出 {TypeError}:當 text 不是字串

formatThousands(value)

將數字加上千分位逗號,支援負數與小數。

formatThousands(999);          // '999'
formatThousands(1000);         // '1,000'
formatThousands(1234567);      // '1,234,567'
formatThousands(-9876543.21);  // '-9,876,543.21'
  • value {number}:要格式化的有限數值
  • 回傳 {string}:加上千分位的字串
  • 拋出 {TypeError}:當 value 不是有限數字(例如 NaN、Infinity、字串)

randomInt(min, max)

產生介於 min 與 max 之間(含頭尾)的隨機整數。

randomInt(1, 6);    // 1、2、3、4、5 或 6
randomInt(5, 5);    // 5
  • min {number}:範圍下限(整數)
  • max {number}:範圍上限(整數)
  • 回傳 {number}:範圍內的隨機整數
  • 拋出 {TypeError}:當 min 或 max 不是整數
  • 拋出 {RangeError}:當 min 大於 max

clamp(value, min, max)

將數值限制在指定範圍內:小於下限回傳下限,大於上限回傳上限。

clamp(5, 0, 10);    // 5
clamp(-3, 0, 10);   // 0
clamp(42, 0, 10);   // 10
  • value {number}:要限制的有限數值
  • min {number}:範圍下限
  • max {number}:範圍上限
  • 回傳 {number}:限制後的數值
  • 拋出 {TypeError}:當參數不是有限數字
  • 拋出 {RangeError}:當 min 大於 max

錯誤處理

所有函式都會驗證參數型別,錯誤會以標準的 TypeError 或 RangeError 拋出,方便搭配 try/catch 使用:

const { formatThousands, randomInt } = require('digital-conversion');

try {
  formatThousands('1000');
} catch (error) {
  console.error(error.name);    // 'TypeError'
  console.error(error.message); // 'value must be a finite number'
}

try {
  randomInt(10, 1);
} catch (error) {
  console.error(error.name);    // 'RangeError'
  console.error(error.message); // 'min must be less than or equal to max'
}

開發

# 安裝專案(本套件無執行期依賴)
npm install

# 執行單元測試(使用 Node.js 內建 test runner)
npm test

專案結構:

digital-conversion/
├── index.js            # 套件原始碼
├── test/
│   └── index.test.js   # 單元測試
├── README.md
├── LICENSE
└── package.json

版本規則

本套件遵循 Semantic Versioning:

  • 修正錯誤:patch(例如 1.0.0 → 1.0.1)
  • 向下相容的新功能:minor(例如 1.0.0 → 1.1.0)
  • 不相容的變更:major(例如 1.0.0 → 2.0.0)

授權

ISC