@soybeanjs/utils
v0.0.1
Published
A collection of useful utility functions for JavaScript and TypeScript projects of SoybeanJS.
Readme
@soybeanjs/utils
English | 简体中文
一个用于 JavaScript 和 TypeScript 项目的实用工具函数集合。
特性
- 🚀 轻量级且零依赖
- 📦 ESM 格式
- 🔧 完整的 TypeScript 类型支持
- 🎯 提供常用的工具函数
- 💾 浏览器存储封装
安装
# 使用 pnpm
pnpm add @soybeanjs/utils
# 使用 npm
npm install @soybeanjs/utils
# 使用 yarn
yarn add @soybeanjs/utils使用方法
字符串工具
提供多种字符串命名格式转换函数:
import { kebabCase, pascalCase, camelCase, snakeCase } from '@soybeanjs/utils';
// 转换为短横线命名
kebabCase('helloWorld'); // 'hello-world'
// 转换为帕斯卡命名
pascalCase('hello-world'); // 'HelloWorld'
// 转换为驼峰命名
camelCase('hello-world'); // 'helloWorld'
// 转换为蛇形命名
snakeCase('helloWorld'); // 'hello_world'对象工具
类型安全的对象操作函数:
import { keysOf, entriesOf, valuesOf } from '@soybeanjs/utils';
const obj = { name: 'John', age: 30 };
// 获取对象键(带类型推断)
const keys = keysOf(obj); // ('name' | 'age')[]
// 获取对象条目(带类型推断)
const entries = entriesOf(obj); // ['name' | 'age', string | number][]
// 获取对象值(带类型推断)
const values = valuesOf(obj); // (string | number)[]存储工具
浏览器存储的类型安全封装(localStorage 和 sessionStorage):
import { createStorage } from '@soybeanjs/utils';
// 定义存储数据结构
interface StorageData {
token: string;
userInfo: {
name: string;
age: number;
};
}
// 创建 localStorage 实例
const storage = createStorage<StorageData>('local', 'my-app-');
// 设置存储
storage.set('token', 'abc123');
storage.set('userInfo', { name: 'John', age: 30 });
// 获取存储
const token = storage.get('token'); // string | null
const userInfo = storage.get('userInfo'); // { name: string; age: number } | null
// 移除存储
storage.remove('token');
// 清空所有存储
storage.clear();API 文档
字符串工具
kebabCase(str: string): string- 转换为短横线命名格式pascalCase(str: string): string- 转换为帕斯卡命名格式camelCase(str: string): string- 转换为驼峰命名格式snakeCase(str: string): string- 转换为蛇形命名格式
对象工具
keysOf<T>(record: T): (keyof T)[]- 获取对象的键(类型安全)entriesOf<T>(record: T): [keyof T, T[keyof T]][]- 获取对象的条目(类型安全)valuesOf<T>(record: T): T[keyof T][]- 获取对象的值(类型安全)
存储工具
createStorage<T>(type: StorageType, storagePrefix: string)- 创建类型安全的存储实例type:'local'或'session'storagePrefix: 存储键前缀- 返回对象包含以下方法:
set<K>(key: K, value: T[K]): void- 设置存储get<K>(key: K): T[K] | null- 获取存储remove(key: keyof T): void- 移除存储clear(): void- 清空所有存储
开发
# 安装依赖
pnpm install
# 构建
pnpm build
# 代码检查
pnpm lint
# 类型检查
pnpm typecheck