Initial commit

This commit is contained in:
2026-05-27 14:28:02 +08:00
commit f74d8990cb
11 changed files with 842 additions and 0 deletions
+101
View File
@@ -0,0 +1,101 @@
# Windychen Utils
轻量级 JavaScript/TypeScript 工具库,提供对象、数组、存储等常用扩展方法。
## 安装
```bash
npm install windychen-untils
```
## 使用
### Node.js / Bun.js
```javascript
require('windychen-untils');
// Object 扩展
const obj = { a: { b: { c: 1 } } };
obj.getPro('a.b.c'); // 1
obj.setPro('x.y.z', 'hello');
// Array 扩展
[{ id: '1' }, { id: '2' }].findPro('id', '2');
```
### 浏览器
```html
<script src="dist/windychen-utils.js"></script>
```
### ES Module
```javascript
import from 'windychen-untils/dist/windychen-utils.esm.js';
```
## API 参考
### Object
| 方法 | 说明 | 示例 |
|------|------|------|
| `getPro(path, default?)` | 安全获取嵌套属性,支持特殊键名 | `obj.getPro('a.b.c')` / `obj.getPro("['x.y'].z")` |
| `setPro(path, value)` | 设置嵌套路径值,自动创建中间对象 | `obj.setPro('foo.bar', 1)` |
**路径格式支持:**
- 普通点分隔:`'a.b.c'`
- 特殊键名:`"['key.with.dot']"`
- 混合:`"['a.b'].c['d.e']"`
### Array
| 方法 | 说明 | 示例 |
|------|------|------|
| `findPro(fn \| key, val?)` | 增强查找 | `arr.findPro('id', '2')` |
| `filterPro(fn \| key, val?)` | 增强过滤 | `arr.filterPro('user.name', 'Alice')` |
| `findIndexPro(fn \| key, val?)` | 增强索引查找 | `arr.findIndexPro('status', 1)` |
| `findLastPro(fn \| key, val?)` | 从末尾查找 | `arr.findLastPro('type', 'b')` |
| **somePro(fn \| key, val?)** | 任一匹配 | `arr.somePro('active', true)` |
| **everyPro(fn \| key, val?)** | 全部匹配 | `arr.everyPro('valid', true)` |
**三种调用方式:**
```javascript
// 回调函数(同原生)
arr.findPro(item => item.id > 1);
// 单值精确匹配
['aa', 'bb'].findPro('bb');
// key-value 匹配(支持嵌套路径)
[{ user: { name: 'Bob' } }].findPro('user.name', 'Bob');
```
### Storage(浏览器环境)
自动选择存储后端:
- 支持 IndexedDB → 使用 `IDBStorage`
- 仅支持 localStorage → 使用 `ILocalStorage`(内存缓存)
```javascript
import storage from 'windychen-untils';
await storage.set('key', { data: 123 });
await storage.get('key', null); // { data: 123 }
```
## 构建
```bash
npm run build # 打包全部 (CJS + ESM + UMD)
npm run build:cjs # CommonJS
npm run build:esm # ES Module
npm run build:umd # UMD (浏览器)
npm test # 运行测试
```
## License
ISC