@corentinth/chisels

中文文档
2.1.0发布于 2025/10/27MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 5772

Collection of utilities for JavaScript and TypeScript, lightweight and tree-shakable.

原始使用文档
文档目录18 节
  1. Installation
  2. Usage
  3. API
  4. safelySync
  5. safely
  6. formatBytes
  7. castError
  8. joinUrlPaths
  9. buildUrl
  10. injectArguments
  11. memoizeOnce
  12. PartialBy
  13. Expand
  14. Dictionary
  15. DeepPartial
  16. Subtract
  17. License
  18. Credits and Acknowledgements

Chisels - JS/TS utilities and types

Opinionated collection of reusable JS/TS utilities and types.

  • Tree-shakable ES modules: Import only what you need
  • Fully tested: Rigorous unit tests with comprehensive coverage
  • Dependency free: No runtime dependencies
  • TypeScript support: Written in TypeScript with full type definitions
  • Lightweight: Small bundle size
  • Modern: Written with modern JS/TS syntax
  • Named arguments: Functions with preferably named arguments

Installation

# Using npm
npm install @corentinth/chisels

# Using yarn
yarn add @corentinth/chisels

# Using pnpm
pnpm add @corentinth/chisels

Usage

Example importing the safely function:

// Using ES6 import
import { safely } from '@corentinth/chisels';

const [result, error] = safely(mayThrowError);

console.log({ result, error });

API

safelySync

Function - See source

Safely executes a function and return a tuple with the result and an error if any.

const [result, error] = safelySync(myFunction);

if (error) {
 console.error(error);
}

console.log(result);

safely

Function - See source

Safely executes an async function or promise and return a tuple with the result and an error if any.

const [result, error] = await safely(myFunction);

if (error) {
 console.error(error);
}

console.log(result);

formatBytes

Function - See source

Formats a number of bytes into a human-readable string.

const formatted = formatBytes({ bytes: 4194304 });

console.log(formatted); // 4 MiB

castError

Function - See source

Casts an unknown value to an Error.

 try {
     // ...
 } catch (rawError) {
   const error = castError(rawError);

   // Do something with a proper Error instance
 }

joinUrlPaths

Function - See source

Join URL parts and trim slashes.

const url = joinUrlPaths('/part1/', '/part2/', 'part3', 'part4/');

console.log(url); // 'part1/part2/part3/part4'

buildUrl

Function - See source

Functional wrapper around URL constructor to build an URL string from a base URL and optional path, query params and hash.

const url = buildUrl({ baseUrl: 'https://example.com', path: 'foo', queryParams: { a: '1', b: '2' }, hash: 'hash' });

console.log(url); // 'https://example.com/foo?a=1&b=2#hash'

injectArguments

Function - See source

Injects arguments into a set of functions. Useful for DI of repositories, services, etc.

 const functions = {
  getUser: ({ userId, db }) => db.users.find({ id: userId }),
  removeUser: ({ userId, db }) => db.users.remove({ id: userId }),
 };

 const { getUser, removeUser } = injectArguments(functions, { db });

 getUser({ userId: 1 });
 removeUser({ userId: 1 });

memoizeOnce

Function - See source

This function takes a function that returns a value and returns a new function that caches the result of the first call. Basically a argument-less memoization.

const getCwd = memoizeOnce(() => process.cwd());

// process.cwd() is only called once
console.log(getCwd());
console.log(getCwd());

PartialBy

Type alias - See source

Make some properties of T optional

type User = {
  id: number;
  name: string;
  email: string;
};

type PartialUser = PartialBy<User, 'email' | 'name'>;

const user: PartialUser = { id: 1 };

Expand

Type alias - See source

Flatten an object type for better IDE support

Dictionary

Type alias - See source

Record<string, T> alias

const dictionary: Dictionary<number> = {
  a: 1,
  b: 2,
};

DeepPartial

Type alias - See source

Make all properties of T optional recursively

Subtract

Type alias - See source

Exclude properties of T that are in U

type User = {
  id: number;
  name: string;
  email: string;
};

type WithId = {
  id: number;
}

type UserWithoutId = Subtract<User, WithId>;

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Credits and Acknowledgements

This project is crafted with ❤️ by Corentin Thomasset.

版本与发布记录

@corentinth/chisels 共发布 14 个版本,首次发布于 2024/09/08;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.0latest2025/10/271 年前
2.0.22025/09/131.2 年前
2.0.02025/09/131.2 年前
1.3.12025/04/301.5 年前
1.3.02025/04/131.6 年前
1.2.22025/03/291.6 年前

关于 @corentinth/chisels 的常见问题

@corentinth/chisels 是什么?

Collection of utilities for JavaScript and TypeScript, lightweight and tree-shakable.

如何安装 @corentinth/chisels?

在项目目录运行 npm install @corentinth/chisels,或使用 pnpm add / yarn add / bun add。

@corentinth/chisels 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@corentinth/chisels 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@corentinth/chisels 可以商用吗?

@corentinth/chisels 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@corentinth/chisels 的下载量有多少?

根据 npm 官方下载统计,@corentinth/chisels 在 2026/10/11 – 2026/11/09 这 30 天内被下载 5,772 次,日均约 192 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@corentinth/chisels 还在维护吗?

最新版本 2.1.0 发布于 2025/10/27(1 年前);至今共发布 14 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/09/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。