diff --git a/README.md b/README.md index 0a064a5..974cc12 100644 --- a/README.md +++ b/README.md @@ -2,38 +2,6 @@ ## Methods -### `getOptions` - -Recommended way to retrieve the options of a loader invocation: - -```javascript -// inside your loader -const options = loaderUtils.getOptions(this); -``` - -1. If `this.query` is a string: - - Tries to parse the query string and returns a new object - - Throws if it's not a valid query string -2. If `this.query` is object-like, it just returns `this.query` -3. In any other case, it just returns `null` - -**Please note:** The returned `options` object is _read-only_. It may be re-used across multiple invocations. -If you pass it on to another library, make sure to make a _deep copy_ of it: - -```javascript -const options = Object.assign( - {}, - defaultOptions, - loaderUtils.getOptions(this) // it is safe to pass null to Object.assign() -); -// don't forget nested objects or arrays -options.obj = Object.assign({}, options.obj); -options.arr = options.arr.slice(); -someLibrary(options); -``` - -[clone](https://www.npmjs.com/package/clone) is a good library to make a deep copy of the options. - #### Options as query strings If the loader options have been passed as loader query string (`loader?some¶ms`), the string is parsed by using [`parseQuery`](#parsequery). diff --git a/lib/getOptions.js b/lib/getOptions.js deleted file mode 100644 index d5512e0..0000000 --- a/lib/getOptions.js +++ /dev/null @@ -1,20 +0,0 @@ -"use strict"; - -const parseQuery = require("./parseQuery"); - -function getOptions(loaderContext) { - const query = loaderContext.query; - - if (typeof query === "string" && query !== "") { - return parseQuery(loaderContext.query); - } - - if (!query || typeof query !== "object") { - // Not object-like queries are not supported. - return {}; - } - - return query; -} - -module.exports = getOptions; diff --git a/lib/index.js b/lib/index.js index 325fe1e..3486d04 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,5 @@ "use strict"; -const getOptions = require("./getOptions"); const parseQuery = require("./parseQuery"); const stringifyRequest = require("./stringifyRequest"); const isUrlRequest = require("./isUrlRequest"); @@ -9,7 +8,6 @@ const parseString = require("./parseString"); const getHashDigest = require("./getHashDigest"); const interpolateName = require("./interpolateName"); -exports.getOptions = getOptions; exports.parseQuery = parseQuery; exports.stringifyRequest = stringifyRequest; exports.isUrlRequest = isUrlRequest; diff --git a/test/getOptions.test.js b/test/getOptions.test.js deleted file mode 100644 index a8cc2d6..0000000 --- a/test/getOptions.test.js +++ /dev/null @@ -1,82 +0,0 @@ -"use strict"; - -const loaderUtils = require("../lib"); - -describe("getOptions()", () => { - it("should work", () => { - expect( - loaderUtils.getOptions({ - query: true, - }) - ).toEqual({}); - expect( - loaderUtils.getOptions({ - query: false, - }) - ).toEqual({}); - expect( - loaderUtils.getOptions({ - query: null, - }) - ).toEqual({}); - expect( - loaderUtils.getOptions({ - query: undefined, - }) - ).toEqual({}); - expect( - loaderUtils.getOptions({ - query: 1, - }) - ).toEqual({}); - expect( - loaderUtils.getOptions({ - query: 0, - }) - ).toEqual({}); - expect( - loaderUtils.getOptions({ - query: -0, - }) - ).toEqual({}); - expect( - loaderUtils.getOptions({ - query: -1, - }) - ).toEqual({}); - expect( - loaderUtils.getOptions({ - query: "", - }) - ).toEqual({}); - expect( - loaderUtils.getOptions({ - query: "?something=getOptions_cannot_parse", - }) - ).toEqual({ something: "getOptions_cannot_parse" }); - - const query1 = {}; - - expect( - loaderUtils.getOptions({ - query: query1, - }) - ).toEqual(query1); - - const query2 = { foo: { bar: "baz" } }; - - expect( - loaderUtils.getOptions({ - query: query2, - }) - ).toEqual(query2); - - const query3 = []; - - expect(loaderUtils.getOptions({ query: query3 })).toEqual(query3); - - const query4 = [1, true, "foobar"]; - - expect(loaderUtils.getOptions({ query: query4 })).toEqual(query4); - }); -});