@rolldown/pluginutils
Advanced tools
+56
-9
@@ -8,2 +8,9 @@ "use strict"; | ||
| } | ||
| function extractQueryWithoutFragment(url) { | ||
| const questionMarkIndex = url.indexOf("?"); | ||
| if (questionMarkIndex === -1) return ""; | ||
| const fragmentIndex = url.indexOf("#", questionMarkIndex); | ||
| if (fragmentIndex === -1) return url.substring(questionMarkIndex); | ||
| else return url.substring(questionMarkIndex, fragmentIndex); | ||
| } | ||
@@ -64,2 +71,12 @@ //#endregion | ||
| }; | ||
| var Query = class { | ||
| kind; | ||
| key; | ||
| pattern; | ||
| constructor(key, pattern) { | ||
| this.pattern = pattern; | ||
| this.key = key; | ||
| this.kind = "query"; | ||
| } | ||
| }; | ||
| var Include = class { | ||
@@ -99,2 +116,5 @@ kind; | ||
| } | ||
| function query(key, pattern) { | ||
| return new Query(key, pattern); | ||
| } | ||
| function include(expr) { | ||
@@ -106,2 +126,16 @@ return new Include(expr); | ||
| } | ||
| /** | ||
| * convert a queryObject to FilterExpression like | ||
| * ```js | ||
| * and(query(k1, v1), query(k2, v2)) | ||
| * ``` | ||
| * @param queryFilterObject The query filter object needs to be matched. | ||
| * @returns a `And` FilterExpression | ||
| */ | ||
| function queries(queryFilter) { | ||
| let arr = Object.entries(queryFilter).map(([key, value]) => { | ||
| return new Query(key, value); | ||
| }); | ||
| return and(...arr); | ||
| } | ||
| function interpreter(exprs, code$1, id$1, moduleType$1) { | ||
@@ -113,3 +147,3 @@ let arr = []; | ||
| } | ||
| function interpreterImpl(expr, code$1, id$1, moduleType$1) { | ||
| function interpreterImpl(expr, code$1, id$1, moduleType$1, ctx = {}) { | ||
| let hasInclude = false; | ||
@@ -119,3 +153,3 @@ for (const e of expr) switch (e.kind) { | ||
| hasInclude = true; | ||
| if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return true; | ||
| if (exprInterpreter(e.expr, code$1, id$1, moduleType$1, ctx)) return true; | ||
| break; | ||
@@ -130,7 +164,7 @@ } | ||
| } | ||
| function exprInterpreter(expr, code$1, id$1, moduleType$1) { | ||
| function exprInterpreter(expr, code$1, id$1, moduleType$1, ctx = {}) { | ||
| switch (expr.kind) { | ||
| case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1)); | ||
| case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1)); | ||
| case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1); | ||
| case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx)); | ||
| case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx)); | ||
| case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1, ctx); | ||
| case "id": { | ||
@@ -149,3 +183,15 @@ if (id$1 === void 0) throw new Error("`id` is required for `id` expression"); | ||
| } | ||
| default: throw new Error(`Expression kind ${expr.kind} is not expected.`); | ||
| case "query": { | ||
| if (id$1 === void 0) throw new Error("`id` is required for `Query` expression"); | ||
| if (!ctx.urlSearchParamsCache) { | ||
| let queryString = extractQueryWithoutFragment(id$1); | ||
| ctx.urlSearchParamsCache = new URLSearchParams(queryString); | ||
| } | ||
| let urlParams = ctx.urlSearchParamsCache; | ||
| if (typeof expr.pattern === "boolean") if (expr.pattern) return urlParams.has(expr.key); | ||
| else return !urlParams.has(expr.key); | ||
| else if (typeof expr.pattern === "string") return urlParams.get(expr.key) === expr.pattern; | ||
| else return expr.pattern.test(urlParams.get(expr.key) ?? ""); | ||
| } | ||
| default: throw new Error(`Expression ${JSON.stringify(expr)} is not expected.`); | ||
| } | ||
@@ -203,3 +249,2 @@ } | ||
| //#endregion | ||
| exports.And = And | ||
| exports.and = and | ||
@@ -218,2 +263,4 @@ exports.code = code | ||
| exports.or = or | ||
| exports.prefixRegex = prefixRegex | ||
| exports.prefixRegex = prefixRegex | ||
| exports.queries = queries | ||
| exports.query = query |
+26
-4
@@ -5,3 +5,3 @@ //#region src/composable-filters.d.ts | ||
| type FilterExpressionKind = FilterExpression["kind"]; | ||
| type FilterExpression = And | Or | Not | Id | ModuleType | Code | Include | Exclude; | ||
| type FilterExpression = And | Or | Not | Id | ModuleType | Code | Query; | ||
| type TopLevelFilterExpression = Include | Exclude; | ||
@@ -23,2 +23,5 @@ declare class And { | ||
| } | ||
| interface QueryFilterObject { | ||
| [key: string]: StringOrRegExp | boolean; | ||
| } | ||
| interface IdParams { | ||
@@ -43,2 +46,8 @@ cleanUrl?: boolean; | ||
| } | ||
| declare class Query { | ||
| kind: "query"; | ||
| key: string; | ||
| pattern: StringOrRegExp | boolean; | ||
| constructor(key: string, pattern: StringOrRegExp | boolean); | ||
| } | ||
| declare class Include { | ||
@@ -60,7 +69,20 @@ kind: "include"; | ||
| declare function code(pattern: StringOrRegExp): Code; | ||
| declare function query(key: string, pattern: StringOrRegExp | boolean): Query; | ||
| declare function include(expr: FilterExpression): Include; | ||
| declare function exclude(expr: FilterExpression): Exclude; | ||
| /** | ||
| * convert a queryObject to FilterExpression like | ||
| * ```js | ||
| * and(query(k1, v1), query(k2, v2)) | ||
| * ``` | ||
| * @param queryFilterObject The query filter object needs to be matched. | ||
| * @returns a `And` FilterExpression | ||
| */ | ||
| declare function queries(queryFilter: QueryFilterObject): And; | ||
| declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean; | ||
| declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean; | ||
| declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType): boolean; | ||
| interface InterpreterCtx { | ||
| urlSearchParamsCache?: URLSearchParams; | ||
| } | ||
| declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean; | ||
| declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean; | ||
@@ -98,2 +120,2 @@ //#endregion | ||
| //#endregion | ||
| export { And, FilterExpression, FilterExpressionKind, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex }; | ||
| export { FilterExpression, FilterExpressionKind, QueryFilterObject, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, queries, query }; |
+26
-4
@@ -5,3 +5,3 @@ //#region src/composable-filters.d.ts | ||
| type FilterExpressionKind = FilterExpression["kind"]; | ||
| type FilterExpression = And | Or | Not | Id | ModuleType | Code | Include | Exclude; | ||
| type FilterExpression = And | Or | Not | Id | ModuleType | Code | Query; | ||
| type TopLevelFilterExpression = Include | Exclude; | ||
@@ -23,2 +23,5 @@ declare class And { | ||
| } | ||
| interface QueryFilterObject { | ||
| [key: string]: StringOrRegExp | boolean; | ||
| } | ||
| interface IdParams { | ||
@@ -43,2 +46,8 @@ cleanUrl?: boolean; | ||
| } | ||
| declare class Query { | ||
| kind: "query"; | ||
| key: string; | ||
| pattern: StringOrRegExp | boolean; | ||
| constructor(key: string, pattern: StringOrRegExp | boolean); | ||
| } | ||
| declare class Include { | ||
@@ -60,7 +69,20 @@ kind: "include"; | ||
| declare function code(pattern: StringOrRegExp): Code; | ||
| declare function query(key: string, pattern: StringOrRegExp | boolean): Query; | ||
| declare function include(expr: FilterExpression): Include; | ||
| declare function exclude(expr: FilterExpression): Exclude; | ||
| /** | ||
| * convert a queryObject to FilterExpression like | ||
| * ```js | ||
| * and(query(k1, v1), query(k2, v2)) | ||
| * ``` | ||
| * @param queryFilterObject The query filter object needs to be matched. | ||
| * @returns a `And` FilterExpression | ||
| */ | ||
| declare function queries(queryFilter: QueryFilterObject): And; | ||
| declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean; | ||
| declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean; | ||
| declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType): boolean; | ||
| interface InterpreterCtx { | ||
| urlSearchParamsCache?: URLSearchParams; | ||
| } | ||
| declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean; | ||
| declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean; | ||
@@ -98,2 +120,2 @@ //#endregion | ||
| //#endregion | ||
| export { And, FilterExpression, FilterExpressionKind, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex }; | ||
| export { FilterExpression, FilterExpressionKind, QueryFilterObject, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, queries, query }; |
+54
-8
@@ -6,2 +6,9 @@ //#region src/utils.ts | ||
| } | ||
| function extractQueryWithoutFragment(url) { | ||
| const questionMarkIndex = url.indexOf("?"); | ||
| if (questionMarkIndex === -1) return ""; | ||
| const fragmentIndex = url.indexOf("#", questionMarkIndex); | ||
| if (fragmentIndex === -1) return url.substring(questionMarkIndex); | ||
| else return url.substring(questionMarkIndex, fragmentIndex); | ||
| } | ||
@@ -62,2 +69,12 @@ //#endregion | ||
| }; | ||
| var Query = class { | ||
| kind; | ||
| key; | ||
| pattern; | ||
| constructor(key, pattern) { | ||
| this.pattern = pattern; | ||
| this.key = key; | ||
| this.kind = "query"; | ||
| } | ||
| }; | ||
| var Include = class { | ||
@@ -97,2 +114,5 @@ kind; | ||
| } | ||
| function query(key, pattern) { | ||
| return new Query(key, pattern); | ||
| } | ||
| function include(expr) { | ||
@@ -104,2 +124,16 @@ return new Include(expr); | ||
| } | ||
| /** | ||
| * convert a queryObject to FilterExpression like | ||
| * ```js | ||
| * and(query(k1, v1), query(k2, v2)) | ||
| * ``` | ||
| * @param queryFilterObject The query filter object needs to be matched. | ||
| * @returns a `And` FilterExpression | ||
| */ | ||
| function queries(queryFilter) { | ||
| let arr = Object.entries(queryFilter).map(([key, value]) => { | ||
| return new Query(key, value); | ||
| }); | ||
| return and(...arr); | ||
| } | ||
| function interpreter(exprs, code$1, id$1, moduleType$1) { | ||
@@ -111,3 +145,3 @@ let arr = []; | ||
| } | ||
| function interpreterImpl(expr, code$1, id$1, moduleType$1) { | ||
| function interpreterImpl(expr, code$1, id$1, moduleType$1, ctx = {}) { | ||
| let hasInclude = false; | ||
@@ -117,3 +151,3 @@ for (const e of expr) switch (e.kind) { | ||
| hasInclude = true; | ||
| if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return true; | ||
| if (exprInterpreter(e.expr, code$1, id$1, moduleType$1, ctx)) return true; | ||
| break; | ||
@@ -128,7 +162,7 @@ } | ||
| } | ||
| function exprInterpreter(expr, code$1, id$1, moduleType$1) { | ||
| function exprInterpreter(expr, code$1, id$1, moduleType$1, ctx = {}) { | ||
| switch (expr.kind) { | ||
| case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1)); | ||
| case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1)); | ||
| case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1); | ||
| case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx)); | ||
| case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx)); | ||
| case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1, ctx); | ||
| case "id": { | ||
@@ -147,3 +181,15 @@ if (id$1 === void 0) throw new Error("`id` is required for `id` expression"); | ||
| } | ||
| default: throw new Error(`Expression kind ${expr.kind} is not expected.`); | ||
| case "query": { | ||
| if (id$1 === void 0) throw new Error("`id` is required for `Query` expression"); | ||
| if (!ctx.urlSearchParamsCache) { | ||
| let queryString = extractQueryWithoutFragment(id$1); | ||
| ctx.urlSearchParamsCache = new URLSearchParams(queryString); | ||
| } | ||
| let urlParams = ctx.urlSearchParamsCache; | ||
| if (typeof expr.pattern === "boolean") if (expr.pattern) return urlParams.has(expr.key); | ||
| else return !urlParams.has(expr.key); | ||
| else if (typeof expr.pattern === "string") return urlParams.get(expr.key) === expr.pattern; | ||
| else return expr.pattern.test(urlParams.get(expr.key) ?? ""); | ||
| } | ||
| default: throw new Error(`Expression ${JSON.stringify(expr)} is not expected.`); | ||
| } | ||
@@ -201,2 +247,2 @@ } | ||
| //#endregion | ||
| export { And, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex }; | ||
| export { and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, queries, query }; |
+1
-1
| { | ||
| "name": "@rolldown/pluginutils", | ||
| "version": "1.0.0-beta.8-commit.66f4623", | ||
| "version": "1.0.0-beta.8-commit.709eb63", | ||
| "license": "MIT", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23858
23.89%599
23.76%0
-100%