Skip to content

Commit

Permalink
Replace parseQuery() with getOptions()
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Feb 18, 2017
1 parent 621114e commit dfe2608
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 54 deletions.
13 changes: 13 additions & 0 deletions lib/getOptions.js
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;
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

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

exports.parseQuery = parseQuery;
exports.getOptions = getOptions;
exports.getLoaderConfig = getLoaderConfig;
exports.stringifyRequest = stringifyRequest;
exports.getRemainingRequest = getRemainingRequest;
Expand Down
17 changes: 3 additions & 14 deletions lib/parseQuery.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
"use strict";

const JSON5 = require("json5");
const util = require("util");
const os = require("os");

const parseQueryDeprecationWarning = util.deprecate(() => {},
"loaderUtils.parseQuery() received a non-string value which can be problematic, " +
"see https://github.com/webpack/loader-utils/issues/56" + os.EOL +
"parseQuery() will be replaced with getOptions() in the next major version of loader-utils."
);
const specialValues = {
"null": null,
"true": true,
"false": false
};

function parseQuery(query) {
if(!query) return {};
if(typeof query !== "string") {
parseQueryDeprecationWarning();
return query;
if(query.substr(0, 1) !== "?") {
throw new Error("A valid query string passed to parseQuery should begin with '?'");
}
if(query.substr(0, 1) !== "?")
throw new Error("a valid query string passed to parseQuery should begin with '?'");
query = query.substr(1);
if(query.substr(0, 1) === "{" && query.substr(-1) === "}") {
return JSON5.parse(query);
}
const queryArgs = query.split(/[,\&]/g);
const queryArgs = query.split(/[,&]/g);
const result = {};
queryArgs.forEach(arg => {
const idx = arg.indexOf("=");
Expand Down
112 changes: 112 additions & 0 deletions test/getOptions.test.js
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
);
});
});
});
38 changes: 0 additions & 38 deletions test/parseQuery.test.js

This file was deleted.

0 comments on commit dfe2608

Please sign in to comment.