-
-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace parseQuery() with getOptions()
- Loading branch information
Showing
5 changed files
with
130 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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,13 @@ | ||
"use strict"; | ||
|
||
const parseQuery = require("./parseQuery"); | ||
|
||
function getOptions(loaderContext) { | ||
const query = loaderContext.query; | ||
if(typeof query === "string") { | ||
return parseQuery(loaderContext.query); | ||
} | ||
return query; | ||
} | ||
|
||
module.exports = getOptions; |
This file contains 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 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 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,112 @@ | ||
"use strict"; | ||
|
||
const assert = require("assert"); | ||
const loaderUtils = require("../lib"); | ||
|
||
describe("getOptions()", () => { | ||
describe("when loaderContext.query is a string", () => { | ||
[{ | ||
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("and the query string does not start 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 the object", () => { | ||
const query = {}; | ||
assert.strictEqual( | ||
loaderUtils.getOptions({ | ||
query | ||
}), | ||
query | ||
); | ||
}); | ||
}); | ||
describe("when loaderContext.query is anything else", () => { | ||
it("should just return it", () => { | ||
const query = []; | ||
assert.strictEqual( | ||
loaderUtils.getOptions({ | ||
query | ||
}), | ||
query | ||
); | ||
assert.strictEqual( | ||
loaderUtils.getOptions({ | ||
query: undefined | ||
}), | ||
undefined | ||
); | ||
assert.strictEqual( | ||
loaderUtils.getOptions({ | ||
query: null | ||
}), | ||
null | ||
); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.