This repository was archived by the owner on Dec 17, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 187
Export parseQuery (#76) #77
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,10 +36,23 @@ someLibrary(options); | |
|
|
||
| #### Options as query strings | ||
|
|
||
| If the loader options have been passed as loader query string (`loader?some¶ms`), the string is parsed like this: | ||
| 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 | ||
| var params = loaderUtils.parseQuery(this.resourceQuery); // resource: `file?param1=foo` | ||
| if (params.param1 === 'foo') { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use double quotes |
||
| // do something | ||
| } | ||
| ``` | ||
|
|
||
| The string is parsed like this: | ||
|
|
||
| ``` text | ||
| -> null | ||
| -> Error | ||
| ? -> {} | ||
| ?flag -> { flag: true } | ||
| ?+flag -> { flag: true } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,72 +4,14 @@ const assert = require("assert"); | |
| const loaderUtils = require("../lib"); | ||
|
|
||
| describe("getOptions()", () => { | ||
| describe("when loaderContext.query 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, () => { | ||
| assert.deepEqual( | ||
| loaderUtils.getOptions({ | ||
| query: test.query | ||
| }), | ||
| test.expected | ||
| ); | ||
| }); | ||
| describe("when loaderContext.query is a string that has length", () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd write |
||
| it("should call parseQuery() and return its result", () => { | ||
| assert.deepEqual( | ||
| loaderUtils.getOptions({ | ||
| query: "?something=getOptions_cannot_parse" | ||
| }), | ||
| { something: "getOptions_cannot_parse" } | ||
| ); | ||
| }); | ||
| }); | ||
| describe("when loaderContext.query is an empty string", () => { | ||
|
|
@@ -82,14 +24,6 @@ describe("getOptions()", () => { | |
| ); | ||
| }); | ||
| }); | ||
| describe("when loaderContext.query is any other string not starting with ?", () => { | ||
| it("should throw an error", () => { | ||
| assert.throws( | ||
| () => loaderUtils.getOptions({ query: "a" }), | ||
| "A valid query string passed to parseQuery should begin with '?'" | ||
| ); | ||
| }); | ||
| }); | ||
| describe("when loaderContext.query is an object", () => { | ||
| it("should just return it", () => { | ||
| const query = {}; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| "use strict"; | ||
|
|
||
| const assert = require("assert"); | ||
| 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, () => { | ||
| assert.deepEqual( | ||
| loaderUtils.parseQuery(test.query), | ||
| test.expected | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("when passed string is any other string not starting with ?", () => { | ||
| it("should throw an error", () => { | ||
| assert.throws( | ||
| () => loaderUtils.parseQuery("a"), | ||
| "A valid query string passed to parseQuery should begin with '?'" | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
constin the README if the value is not changed