From e31e8b54825f56c25dd6f6e834974f061ddaf68b Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Wed, 20 Oct 2021 14:38:26 +0300 Subject: [PATCH] refactor!: `parseString` was removed in favor `new URLSearchParams(loaderContext.resourceQuery.slice(1))` where `loaderContext` is `this` in loader function --- README.md | 30 --------------- lib/index.js | 2 - lib/parseQuery.js | 69 ----------------------------------- test/parseQuery.test.js | 81 ----------------------------------------- 4 files changed, 182 deletions(-) delete mode 100644 lib/parseQuery.js delete mode 100644 test/parseQuery.test.js diff --git a/README.md b/README.md index 6b61d87..3057748 100644 --- a/README.md +++ b/README.md @@ -6,36 +6,6 @@ If the loader options have been passed as loader query string (`loader?some¶ms`), the string is parsed by using [`parseQuery`](#parsequery). -### `parseQuery` - -Parses a passed string (e.g. `loaderContext.resourceQuery`) as a query string, and returns an object. - -```javascript -const params = loaderUtils.parseQuery(this.resourceQuery); // resource: `file?param1=foo` - -if (params.param1 === "foo") { - // do something -} -``` - -The string is parsed like this: - -```text - -> Error -? -> {} -?flag -> { flag: true } -?+flag -> { flag: true } -?-flag -> { flag: false } -?xyz=test -> { xyz: "test" } -?xyz=1 -> { xyz: "1" } // numbers are NOT parsed -?xyz[]=a -> { xyz: ["a"] } -?flag1&flag2 -> { flag1: true, flag2: true } -?+flag1,-flag2 -> { flag1: true, flag2: false } -?xyz[]=a,xyz[]=b -> { xyz: ["a", "b"] } -?a%2C%26b=c%2C%26d -> { "a,&b": "c,&d" } -?{data:{a:1},isJSON5:true} -> { data: { a: 1 }, isJSON5: true } -``` - ### `urlToRequest` Converts some resource URL to a webpack module request. diff --git a/lib/index.js b/lib/index.js index 3e1c5e8..86722dc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,9 @@ "use strict"; -const parseQuery = require("./parseQuery"); const urlToRequest = require("./urlToRequest"); const getHashDigest = require("./getHashDigest"); const interpolateName = require("./interpolateName"); -exports.parseQuery = parseQuery; exports.urlToRequest = urlToRequest; exports.getHashDigest = getHashDigest; exports.interpolateName = interpolateName; diff --git a/lib/parseQuery.js b/lib/parseQuery.js deleted file mode 100644 index c54fc6e..0000000 --- a/lib/parseQuery.js +++ /dev/null @@ -1,69 +0,0 @@ -"use strict"; - -const JSON5 = require("json5"); - -const specialValues = { - null: null, - true: true, - false: false, -}; - -function parseQuery(query) { - if (query.substr(0, 1) !== "?") { - throw new Error( - "A valid query string passed to parseQuery should begin with '?'" - ); - } - - query = query.substr(1); - - if (!query) { - return {}; - } - - if (query.substr(0, 1) === "{" && query.substr(-1) === "}") { - return JSON5.parse(query); - } - - const queryArgs = query.split(/[,&]/g); - const result = {}; - - queryArgs.forEach((arg) => { - const idx = arg.indexOf("="); - - if (idx >= 0) { - let name = arg.substr(0, idx); - let value = decodeURIComponent(arg.substr(idx + 1)); - - // eslint-disable-next-line no-prototype-builtins - if (specialValues.hasOwnProperty(value)) { - value = specialValues[value]; - } - - if (name.substr(-2) === "[]") { - name = decodeURIComponent(name.substr(0, name.length - 2)); - - if (!Array.isArray(result[name])) { - result[name] = []; - } - - result[name].push(value); - } else { - name = decodeURIComponent(name); - result[name] = value; - } - } else { - if (arg.substr(0, 1) === "-") { - result[decodeURIComponent(arg.substr(1))] = false; - } else if (arg.substr(0, 1) === "+") { - result[decodeURIComponent(arg.substr(1))] = true; - } else { - result[decodeURIComponent(arg)] = true; - } - } - }); - - return result; -} - -module.exports = parseQuery; diff --git a/test/parseQuery.test.js b/test/parseQuery.test.js deleted file mode 100644 index f11d92a..0000000 --- a/test/parseQuery.test.js +++ /dev/null @@ -1,81 +0,0 @@ -"use strict"; - -const loaderUtils = require("../"); - -describe("parseQuery()", () => { - describe("when passed string is a query string starting with ?", () => { - [ - { - it: "should return an empty object by default", - query: "?", - expected: {}, - }, - { - it: "should parse query params", - query: "?name=cheesecake&slices=8&delicious&warm=false", - expected: { - delicious: true, - name: "cheesecake", - slices: "8", // numbers are still strings with query params - warm: false, - }, - }, - { - it: "should parse query params with arrays", - query: "?ingredients[]=flour&ingredients[]=sugar", - expected: { - ingredients: ["flour", "sugar"], - }, - }, - { - it: "should parse query params in JSON format", - query: - "?" + - JSON.stringify({ - delicious: true, - name: "cheesecake", - slices: 8, - warm: false, - }), - expected: { - delicious: true, - name: "cheesecake", - slices: 8, - warm: false, - }, - }, - { - it: "should use decodeURIComponent", - query: "?%3d", - expected: { "=": true }, - }, - { - it: "should recognize params starting with + as boolean params with the value true", - query: "?+%3d", - expected: { "=": true }, - }, - { - it: "should recognize params starting with - as boolean params with the value false", - query: "?-%3d", - expected: { "=": false }, - }, - { - it: "should not confuse regular equal signs and encoded equal signs", - query: "?%3d=%3D", - expected: { "=": "=" }, - }, - ].forEach((test) => { - it(test.it, () => { - expect(loaderUtils.parseQuery(test.query)).toEqual(test.expected); - }); - }); - }); - - describe("when passed string is any other string not starting with ?", () => { - it("should throw an error", () => { - expect(() => loaderUtils.parseQuery("a")).toThrow( - /A valid query string passed to parseQuery should begin with '\?'/ - ); - }); - }); -});