Skip to content

Commit ddf64e4

Browse files
ansekijhnns
authored andcommitted
feat(parseQuery): export parseQuery
- Add `exports.parseQuery` in `lib/index.js` - Separate `test/parseQuery.test.js` from `test/getOptions.test.js` - Add `parseQuery` in `README.md` - Fix: object is passed to loaderUtils.parseQuery in test - Change the title of the test and example code in README - In example code, replace `var`s with `const` #76 #77
1 parent 16e99a1 commit ddf64e4

File tree

4 files changed

+120
-88
lines changed

4 files changed

+120
-88
lines changed

Diff for: README.md

+27-14
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,23 @@ someLibrary(options);
3636

3737
#### Options as query strings
3838

39-
If the loader options have been passed as loader query string (`loader?some&params`), the string is parsed like this:
39+
If the loader options have been passed as loader query string (`loader?some&params`), the string is parsed by using [`parseQuery`](#parsequery).
40+
41+
### `parseQuery`
42+
43+
Parses a passed string (e.g. `loaderContext.resourceQuery`) as a query string, and returns an object.
44+
45+
``` javascript
46+
const params = loaderUtils.parseQuery(this.resourceQuery); // resource: `file?param1=foo`
47+
if (params.param1 === "foo") {
48+
// do something
49+
}
50+
```
51+
52+
The string is parsed like this:
4053

4154
``` text
42-
-> null
55+
-> Error
4356
? -> {}
4457
?flag -> { flag: true }
4558
?+flag -> { flag: true }
@@ -103,35 +116,35 @@ loaderUtils.stringifyRequest(this, "\\\\network-drive\\test.js");
103116
Converts some resource URL to a webpack module request.
104117

105118
```javascript
106-
var url = "path/to/module.js";
107-
var request = loaderUtils.urlToRequest(url); // "./path/to/module.js"
119+
const url = "path/to/module.js";
120+
const request = loaderUtils.urlToRequest(url); // "./path/to/module.js"
108121
```
109122

110123
#### Module URLs
111124

112125
Any URL containing a `~` will be interpreted as a module request. Anything after the `~` will be considered the request path.
113126

114127
```javascript
115-
var url = "~path/to/module.js";
116-
var request = loaderUtils.urlToRequest(url); // "path/to/module.js"
128+
const url = "~path/to/module.js";
129+
const request = loaderUtils.urlToRequest(url); // "path/to/module.js"
117130
```
118131

119132
#### Root-relative URLs
120133

121134
URLs that are root-relative (start with `/`) can be resolved relative to some arbitrary path by using the `root` parameter:
122135

123136
```javascript
124-
var url = "/path/to/module.js";
125-
var root = "./root";
126-
var request = loaderUtils.urlToRequest(url, root); // "./root/path/to/module.js"
137+
const url = "/path/to/module.js";
138+
const root = "./root";
139+
const request = loaderUtils.urlToRequest(url, root); // "./root/path/to/module.js"
127140
```
128141

129142
To convert a root-relative URL into a module URL, specify a `root` value that starts with `~`:
130143

131144
```javascript
132-
var url = "/path/to/module.js";
133-
var root = "~";
134-
var request = loaderUtils.urlToRequest(url, root); // "path/to/module.js"
145+
const url = "/path/to/module.js";
146+
const root = "~";
147+
const request = loaderUtils.urlToRequest(url, root); // "path/to/module.js"
135148
```
136149

137150
### `interpolateName`
@@ -140,7 +153,7 @@ Interpolates a filename template using multiple placeholders and/or a regular ex
140153
The template and regular expression are set as query params called `name` and `regExp` on the current loader's context.
141154

142155
```javascript
143-
var interpolatedName = loaderUtils.interpolateName(loaderContext, name, options);
156+
const interpolatedName = loaderUtils.interpolateName(loaderContext, name, options);
144157
```
145158

146159
The following tokens are replaced in the `name` parameter:
@@ -203,7 +216,7 @@ loaderUtils.interpolateName(loaderContext, "script-[1].[ext]", { regExp: "page-(
203216
### `getHashDigest`
204217

205218
``` javascript
206-
var digestString = loaderUtils.getHashDigest(buffer, hashType, digestType, maxLength);
219+
const digestString = loaderUtils.getHashDigest(buffer, hashType, digestType, maxLength);
207220
```
208221

209222
* `buffer` the content that should be hashed

Diff for: lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
const getOptions = require("./getOptions");
4+
const parseQuery = require("./parseQuery");
45
const stringifyRequest = require("./stringifyRequest");
56
const getRemainingRequest = require("./getRemainingRequest");
67
const getCurrentRequest = require("./getCurrentRequest");
@@ -11,6 +12,7 @@ const getHashDigest = require("./getHashDigest");
1112
const interpolateName = require("./interpolateName");
1213

1314
exports.getOptions = getOptions;
15+
exports.parseQuery = parseQuery;
1416
exports.stringifyRequest = stringifyRequest;
1517
exports.getRemainingRequest = getRemainingRequest;
1618
exports.getCurrentRequest = getCurrentRequest;

Diff for: test/getOptions.test.js

+8-74
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,14 @@ const assert = require("assert");
44
const loaderUtils = require("../lib");
55

66
describe("getOptions()", () => {
7-
describe("when loaderContext.query is a query string starting with ?", () => {
8-
[{
9-
it: "should return an empty object by default",
10-
query: "?",
11-
expected: {}
12-
},
13-
{
14-
it: "should parse query params",
15-
query: "?name=cheesecake&slices=8&delicious&warm=false",
16-
expected: {
17-
delicious: true,
18-
name: "cheesecake",
19-
slices: "8", // numbers are still strings with query params
20-
warm: false
21-
}
22-
},
23-
{
24-
it: "should parse query params with arrays",
25-
query: "?ingredients[]=flour&ingredients[]=sugar",
26-
expected: {
27-
ingredients: ["flour", "sugar"]
28-
}
29-
},
30-
{
31-
it: "should parse query params in JSON format",
32-
query: "?" + JSON.stringify({
33-
delicious: true,
34-
name: "cheesecake",
35-
slices: 8,
36-
warm: false
37-
}),
38-
expected: {
39-
delicious: true,
40-
name: "cheesecake",
41-
slices: 8,
42-
warm: false
43-
}
44-
},
45-
{
46-
it: "should use decodeURIComponent",
47-
query: "?%3d",
48-
expected: { "=": true }
49-
},
50-
{
51-
it: "should recognize params starting with + as boolean params with the value true",
52-
query: "?+%3d",
53-
expected: { "=": true }
54-
},
55-
{
56-
it: "should recognize params starting with - as boolean params with the value false",
57-
query: "?-%3d",
58-
expected: { "=": false }
59-
},
60-
{
61-
it: "should not confuse regular equal signs and encoded equal signs",
62-
query: "?%3d=%3D",
63-
expected: { "=": "=" }
64-
}].forEach(test => {
65-
it(test.it, () => {
66-
assert.deepEqual(
67-
loaderUtils.getOptions({
68-
query: test.query
69-
}),
70-
test.expected
71-
);
72-
});
7+
describe("when loaderContext.query is a string with length > 0", () => {
8+
it("should call parseQuery() and return its result", () => {
9+
assert.deepEqual(
10+
loaderUtils.getOptions({
11+
query: "?something=getOptions_cannot_parse"
12+
}),
13+
{ something: "getOptions_cannot_parse" }
14+
);
7315
});
7416
});
7517
describe("when loaderContext.query is an empty string", () => {
@@ -82,14 +24,6 @@ describe("getOptions()", () => {
8224
);
8325
});
8426
});
85-
describe("when loaderContext.query is any other string not starting with ?", () => {
86-
it("should throw an error", () => {
87-
assert.throws(
88-
() => loaderUtils.getOptions({ query: "a" }),
89-
"A valid query string passed to parseQuery should begin with '?'"
90-
);
91-
});
92-
});
9327
describe("when loaderContext.query is an object", () => {
9428
it("should just return it", () => {
9529
const query = {};

Diff for: test/parseQuery.test.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"use strict";
2+
3+
const assert = require("assert");
4+
const loaderUtils = require("../");
5+
6+
describe("parseQuery()", () => {
7+
describe("when passed string is a query string starting with ?", () => {
8+
[{
9+
it: "should return an empty object by default",
10+
query: "?",
11+
expected: {}
12+
},
13+
{
14+
it: "should parse query params",
15+
query: "?name=cheesecake&slices=8&delicious&warm=false",
16+
expected: {
17+
delicious: true,
18+
name: "cheesecake",
19+
slices: "8", // numbers are still strings with query params
20+
warm: false
21+
}
22+
},
23+
{
24+
it: "should parse query params with arrays",
25+
query: "?ingredients[]=flour&ingredients[]=sugar",
26+
expected: {
27+
ingredients: ["flour", "sugar"]
28+
}
29+
},
30+
{
31+
it: "should parse query params in JSON format",
32+
query: "?" + JSON.stringify({
33+
delicious: true,
34+
name: "cheesecake",
35+
slices: 8,
36+
warm: false
37+
}),
38+
expected: {
39+
delicious: true,
40+
name: "cheesecake",
41+
slices: 8,
42+
warm: false
43+
}
44+
},
45+
{
46+
it: "should use decodeURIComponent",
47+
query: "?%3d",
48+
expected: { "=": true }
49+
},
50+
{
51+
it: "should recognize params starting with + as boolean params with the value true",
52+
query: "?+%3d",
53+
expected: { "=": true }
54+
},
55+
{
56+
it: "should recognize params starting with - as boolean params with the value false",
57+
query: "?-%3d",
58+
expected: { "=": false }
59+
},
60+
{
61+
it: "should not confuse regular equal signs and encoded equal signs",
62+
query: "?%3d=%3D",
63+
expected: { "=": "=" }
64+
}].forEach(test => {
65+
it(test.it, () => {
66+
assert.deepEqual(
67+
loaderUtils.parseQuery(test.query),
68+
test.expected
69+
);
70+
});
71+
});
72+
});
73+
74+
describe("when passed string is any other string not starting with ?", () => {
75+
it("should throw an error", () => {
76+
assert.throws(
77+
() => loaderUtils.parseQuery("a"),
78+
"A valid query string passed to parseQuery should begin with '?'"
79+
);
80+
});
81+
});
82+
83+
});

0 commit comments

Comments
 (0)