Skip to content

Commit

Permalink
Allow importing multiple rulesets
Browse files Browse the repository at this point in the history
  • Loading branch information
PRO-2684 committed Aug 9, 2024
1 parent 687a72b commit e9af8dd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ const purifier = new Purlfy({ // Instantiate a Purlfy object
});
const rules = await (await fetch("https://cdn.jsdelivr.net/gh/PRO-2684/[email protected]/<ruleset>.json")).json(); // Rules
// You may also use GitHub raw link for really latest rules: https://raw.githubusercontent.com/PRO-2684/pURLfy-rules/core-0.3.x/<ruleset>.json
purifier.importRules(rules); // Import rules
const additionalRules = {}; // You can also add your own rules
purifier.importRules(additionalRules);
purifier.importRules(rules, additionalRules); // Import rules
purifier.addEventListener("statisticschange", e => { // Add an event listener for statistics change
console.log("Statistics increment:", e.detail); // Only available in platforms that support `CustomEvent`
console.log("Current statistics:", purifier.getStatistics());
Expand Down Expand Up @@ -76,7 +75,7 @@ new Purlfy({

#### Instance Methods

- `importRules(rules: object): void`: Import rules.
- `importRules(...rulesets: object[]): void`: Import a series of rulesets.
- `purify(url: string): Promise<object>`: Purify a URL.
- `url`: The URL to be purified.
- Returns a `Promise` that resolves to an object containing:
Expand Down
9 changes: 4 additions & 5 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ const purifier = new Purlfy({ // 实例化一个 Purlfy 对象
fetchEnabled: true,
lambdaEnabled: true,
});
const rules = await (await fetch("https://cdn.jsdelivr.net/gh/PRO-2684/pURLfy-rules/<country>.json")).json(); // 规则
// 你也可以使用 GitHub raw 链接来获取真正的最新规则: https://raw.githubusercontent.com/PRO-2684/pURLfy-rules/main/<country>.json
const rules = await (await fetch("https://cdn.jsdelivr.net/gh/PRO-2684/pURLfy-rules/<ruleset>.json")).json(); // 规则
// 你也可以使用 GitHub raw 链接来获取真正的最新规则: https://raw.githubusercontent.com/PRO-2684/pURLfy-rules/main/<ruleset>.json
const additionalRules = {}; // 你也可以添加自己的规则
purifier.importRules(additionalRules);
purifier.importRules(rules); // 导入规则
purifier.importRules(rules, additionalRules); // 导入规则
purifier.addEventListener("statisticschange", e => { // 添加统计数据变化的事件监听器
console.log("Statistics increment:", e.detail); // 只有在支持 `CustomEvent` 的环境下才能使用
console.log("Current statistics:", purifier.getStatistics());
Expand Down Expand Up @@ -76,7 +75,7 @@ new Purlfy({

#### 实例方法

- `importRules(rules: object): void`: 导入规则
- `importRules(...rulesets: object[]): void`: 导入一系列规则集
- `purify(url: string): Promise<object>`: 净化一个 URL
- `url`: 要净化的 URL
- 返回值: `Promise`,解析为一个对象,包含:
Expand Down
8 changes: 4 additions & 4 deletions purlfy.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Purlfy extends EventTarget {
* @returns {Object} The merged object.
* @see https://stackoverflow.com/questions/27936772
*/
static #mergeDeep(target, ...sources) {
static #mergeDeep(target, ...sources) { // TODO: handle rules conflict (e.g. "path" and "path/")
if (!sources.length) return target;
const source = sources.shift();
if (Purlfy.#isObject(target) && Purlfy.#isObject(source)) {
Expand Down Expand Up @@ -212,11 +212,11 @@ class Purlfy extends EventTarget {

/**
* Imports the given rules.
* @param {Object} rules The rules to import.
* @param {...Object} rulesets The rulesets to import.
* @returns {void}
*/
importRules(rules) {
Purlfy.#mergeDeep(this.#rules, rules);
importRules(...rulesets) {
Purlfy.#mergeDeep(this.#rules, ...rulesets);
}

/**
Expand Down

0 comments on commit e9af8dd

Please sign in to comment.