Chisels - JS/TS公用事业和类型
收集可重复使用的JS/TS公用事业和类型的意见.
- 可树动摇的ES模块: 只导入您需要的东西
- 完全测试:全面覆盖的严格单位测试
- 无依赖性: 无运行时间依赖性
- 类型脚本支持: 在 TypeScript 中以完整类型定义写入
- 轻巧: 小捆绑大小
- 现代: 用现代JS/TS语法编写
- 命名参数: 函数, 最好是命名参数
安装
# Using npm
npm install @corentinth/chisels
# Using yarn
yarn add @corentinth/chisels
# Using pnpm
pnpm add @corentinth/chisels
使用量
导入示例 safely 函数 :
// Using ES6 import
import { safely } from '@corentinth/chisels';
const [result, error] = safely(mayThrowError);
console.log({ result, error });
API 密码
safelySync
函数 - 见来源
安全执行一个函数,并返回一个有结果和任何错误的拖拉.
const [result, error] = safelySync(myFunction);
if (error) {
console.error(error);
}
console.log(result);
safely
函数 - 见来源
安全地执行 aync 函数或承诺,并返回一拖拉和任何错误.
const [result, error] = await safely(myFunction);
if (error) {
console.error(error);
}
console.log(result);
formatBytes
函数 - 见来源
将若干字节格式化为可读字符串.
const formatted = formatBytes({ bytes: 4194304 });
console.log(formatted); // 4 MiB
castError
函数 - 见来源
将未知值刻入错误.
try {
// ...
} catch (rawError) {
const error = castError(rawError);
// Do something with a proper Error instance
}
joinUrlPaths
函数 - 见来源
加入 URL 部件并修剪斜线 .
const url = joinUrlPaths('/part1/', '/part2/', 'part3', 'part4/');
console.log(url); // 'part1/part2/part3/part4'
buildUrl
函数 - 见来源
围绕 URL 构建器的功能包来从基址和可选路径构建 URL 字符串,查询参数和散列.
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
函数 - 见来源
将参数注入一组函数。 可用于寄存器、服务等的编目.
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
函数 - 见来源
此函数具有返回值并返回缓存第一次调用结果的新函数的函数. 基本上是一种毫无争议的回忆.
const getCwd = memoizeOnce(() => process.cwd());
// process.cwd() is only called once
console.log(getCwd());
console.log(getCwd());
PartialBy
类型别名 - 见来源
使 T 的一些属性可选
type User = {
id: number;
name: string;
email: string;
};
type PartialUser = PartialBy<User, 'email' | 'name'>;
const user: PartialUser = { id: 1 };
Expand
类型别名 - 见来源
为更好的 IDE 支持而平整对象类型
Dictionary
类型别名 - 见来源
记录 < 字符串, T > 别名
const dictionary: Dictionary<number> = {
a: 1,
b: 2,
};
DeepPartial
类型别名 - 见来源
使 T 的所有属性递归
Subtract
类型别名 - 见来源
不包括在 U 中的 T 属性
type User = {
id: number;
name: string;
email: string;
};
type WithId = {
id: number;
}
type UserWithoutId = Subtract<User, WithId>;
许可证
这个项目根据麻省理工学院的许可证获得许可。 见 自由主义 文档以获取更多信息.
信用和鸣谢
这个项目由 . . 科伦丁·托马斯塞特。 。 。 .