Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,23 @@ someLibrary(options);

#### Options as query strings

If the loader options have been passed as loader query string (`loader?some&params`), the string is parsed like this:
If the loader options have been passed as loader query string (`loader?some&params`), 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`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use const in the README if the value is not changed

if (params.param1 === 'foo') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 }
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const getOptions = require("./getOptions");
const parseQuery = require("./parseQuery");
const stringifyRequest = require("./stringifyRequest");
const getRemainingRequest = require("./getRemainingRequest");
const getCurrentRequest = require("./getCurrentRequest");
Expand All @@ -11,6 +12,7 @@ const getHashDigest = require("./getHashDigest");
const interpolateName = require("./interpolateName");

exports.getOptions = getOptions;
exports.parseQuery = parseQuery;
exports.stringifyRequest = stringifyRequest;
exports.getRemainingRequest = getRemainingRequest;
exports.getCurrentRequest = getCurrentRequest;
Expand Down
82 changes: 8 additions & 74 deletions test/getOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd write ... is a string with length > 0

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", () => {
Expand All @@ -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 = {};
Expand Down
83 changes: 83 additions & 0 deletions test/parseQuery.test.js
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 '?'"
);
});
});

});