You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP →

cparse

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cparse - npm Package Compare versions

Comparing version

to
2.0.3

@@ -5,2 +5,36 @@ # Changelog

## [2.0.3](https://github.com/wind2sing/cparse/compare/v2.0.2...v2.0.3) (2024-12-16)
### ✨ 新增功能 - 简化语法
添加了更简洁的调用方式,现在可以直接在 Cheerio 实例上调用 `parse` 方法!
### 🎯 核心改进
- **新增 `$.parse()` 方法**:直接在 Cheerio 实例上调用,无需传递 `$` 参数
- **向后兼容**:传统的 `parse(rule, $)` 语法仍然完全支持
- **TypeScript 支持**:完整的类型定义,包含新的简化语法
- **全面测试**:新增 6 个测试用例,总测试数量达到 209 个
### 🚀 使用示例
```javascript
// ✅ 新的简化语法
const $ = loadCheerio(html);
const title = $.parse('.title');
const data = $.parse({ title: '.title', items: '[.item]' });
// ❌ 传统语法(仍然支持)
const title2 = parse('.title', $);
```
### 📚 文档更新
- 更新 README 文档,突出新的简化语法
- 添加语法对比表格和使用示例
- 更新 TypeScript 类型定义
### 🧪 测试覆盖
- 新增 `$.parse()` 方法基础功能测试
- 新增属性提取、数组提取测试
- 新增结构化数据和自定义过滤器测试
- 所有 209 个测试用例通过
## [2.0.2](https://github.com/wind2sing/cparse/compare/v2.0.1...v2.0.2) (2024-12-16)

@@ -7,0 +41,0 @@

@@ -85,2 +85,9 @@ /**

<T extends Element>(selector?: any): ExtendedCheerio<T>;
/**
* 简化的解析方法,直接在 Cheerio 实例上调用
* @param rule 解析规则
* @param filters 自定义过滤器
*/
parse<T = any>(rule: ParseRule, filters?: Filters): T;
}

@@ -87,0 +94,0 @@ }

@@ -5,3 +5,3 @@ /**

* @author wind2sing
* @version 2.0.1
* @version 2.0.3
* @license MIT

@@ -8,0 +8,0 @@ */

{
"name": "cparse",
"version": "2.0.2",
"version": "2.0.3",
"description": "一个基于 Cheerio 的 HTML 解析和数据提取工具库",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -51,15 +51,30 @@ # cparse

// 基本提取
// 传统用法
const title = parse('.title', $); // "Hello World"
// 🎯 新增:简化语法 - 直接在 $ 实例上调用 parse
const title2 = $.parse('.title'); // "Hello World"
// 数组提取(语法糖)
const items = parse('[.item]', $); // 所有 .item 元素的文本数组
const items = $.parse('[.item]'); // 所有 .item 元素的文本数组
// 属性提取(语法糖)
const links = parse('[a@href]', $); // 所有链接的 href 属性数组
const links = $.parse('[a@href]'); // 所有链接的 href 属性数组
// 过滤器链
const price = parse('.price | trim | float', $); // 文本 -> 去空格 -> 转浮点数
const price = $.parse('.price | trim | float'); // 文本 -> 去空格 -> 转浮点数
```
### 🎯 简化语法对比
```javascript
// ❌ 传统用法:需要传递 $ 参数
const title = parse('.title', $);
const data = parse({ title: '.title', count: '.count | int' }, $);
// ✅ 简化用法:直接在 $ 实例上调用
const title = $.parse('.title');
const data = $.parse({ title: '.title', count: '.count | int' });
```
### 结构化数据提取

@@ -78,8 +93,8 @@

// 提取结构化数据
const product = parse({
// 使用简化语法提取结构化数据
const product = $.parse({
title: '.title',
price: '.price | regex:\\d+\\.\\d+ | float',
rating: '.rating@data-score | float'
}, $);
});

@@ -101,6 +116,6 @@ console.log(product);

// 响应自动包含 $ 属性
// 响应自动包含 $ 属性,可以直接使用简化语法
const response = await client.get('https://example.com');
const title = parse('title', response.$);
const links = parse('[a@href]', response.$);
const title = response.$.parse('title');
const links = response.$.parse('[a@href]');
```

@@ -118,8 +133,41 @@

const response = await client.get('https://example.com');
const data = parse({
const data = response.$.parse({
title: 'title',
description: 'meta[name="description"]@content'
}, response.$);
});
```
## 🎯 简化语法 - 直接在 $ 实例上调用
**v2.0.2+ 新增功能**:现在可以直接在 Cheerio 实例上调用 `parse` 方法,无需传递 `$` 参数!
### 语法对比
| 传统用法 | 简化用法 | 说明 |
|---------|---------|------|
| `parse('.title', $)` | `$.parse('.title')` | 基本选择器 |
| `parse('[.item]', $)` | `$.parse('[.item]')` | 数组提取 |
| `parse('a@href', $)` | `$.parse('a@href')` | 属性提取 |
| `parse('.price \| float', $)` | `$.parse('.price \| float')` | 过滤器链 |
| `parse({...}, $)` | `$.parse({...})` | 结构化数据 |
### 使用示例
```javascript
const { loadCheerio } = require('cparse');
const $ = loadCheerio('<div class="title">Hello</div>');
// ✅ 推荐:使用简化语法
const title = $.parse('.title');
const data = $.parse({
title: '.title',
items: '[.item]',
link: 'a@href'
});
// ❌ 传统用法(仍然支持)
const { parse } = require('cparse');
const title2 = parse('.title', $);
```
## 🎯 核心语法糖功能

@@ -135,4 +183,4 @@

// cparse 语法糖
parse('[a@href]', $);
// cparse 简化语法
$.parse('[a@href]');
```

@@ -146,4 +194,4 @@

// cparse 语法糖
parse('[.item]', $);
// cparse 简化语法
$.parse('[.item]');
```

@@ -155,5 +203,5 @@

// 完全支持 Cheerio 原生 CSS 选择器
parse('div.active', $); // 类选择器
parse('input[type="text"]', $); // 属性选择器
parse('li:first-child', $); // 伪选择器
$.parse('div.active'); // 类选择器
$.parse('input[type="text"]'); // 属性选择器
$.parse('li:first-child'); // 伪选择器
```

@@ -273,5 +321,5 @@

// 语法糖(cparse 扩展)
parse('p:not-empty', $)
$.parse('p:not-empty')
// 转换为 Cheerio 原生
parse('p:not(:empty)', $)
$.parse('p:not(:empty)')
```

@@ -278,0 +326,0 @@

@@ -70,4 +70,13 @@

}
// 在 Cheerio 实例上添加 parse 方法
if ($ && typeof $ === 'function') {
$.parse = function(rule, filters) {
// 延迟加载 parse 函数以避免循环依赖
const parse = require('../parse');
return parse(rule, $, filters);
};
}
}
module.exports = plugin;