tj / node-querystring

querystring parser for node and the browser - supporting nesting (used by Express, Connect, etc)
MIT License
455 stars 66 forks source link

Options #89

Open buschtoens opened 10 years ago

buschtoens commented 10 years ago
var qs = require("qs");

qs.options;
qs.parse.options;
qs.stringify.options;

/**
 * Filled with general defaults that apply to `qs.parse` and `qs.stringify`.
 */
console.log(qs.options); // { "delimeter": "&", /* ... */ }

/**
 * Filled with `qs.parse` specific defaults.
 */
console.log(qs.parse.options); // { "parseNumbers": true, /* ... */ }

/**
 * Filled with `qs.stringify` specific defaults.
 */
console.log(qs.stringify.options); // { "includeEmptyValues": true, /* ... */ }

/**
 * Modifications in `qs.parse.options` and `qs.stringify.options`
 * take precedence over `qs.parse.options`.
 */
qs.stringify.options.delimeter = ";";
console.log(
  qs.options.get("delimeter")           // "&"
, qs.parse.options.get("delimeter")     // "&"
, qs.stringify.options.get("delimeter") // ";"
);

/**
 * Finally options can also be passed directly to the call.
 * They take precedence over all other options.
 */
console.log(
  qs.stringify({ "foo": "bar", "baz": "bam" }, { delimeter: "!" }) // "foo=bar!baz=bam
);
buschtoens commented 10 years ago

Would close #1.

tj commented 10 years ago

-1 to global options, that would mess with other libs, I think we can close #1, not very useful

buschtoens commented 10 years ago

Ah, yeah. I see, but maybe the user is really interested in doing so because of reasons? Haha.

The user would be left with two options: Carrying a temporary options object around and including it in every call or (the better way) building custom functions around qs.

var myqs = {};
myqs.parse = function(str) { qs.parse(str, { /* ... */ }); };

Still, if you're in an environment where you need to change, let's say, the delimeter, this would most likely apply to all other libs accessing qs and you'd want them to use the same qs you do.

Libs that really want to access the defaults, because they know what they're doing, could qs.parse("blah", false) which we could interpret as "use the defaults" or pass in the specific options they require to be set in a certain way. However, this is not backwards compatible. When users start using global options, they could break other libs, if both access the same version (never use "*", guys!).

Still -1 on global options? Then we'll go along with the second parameter only and recommend custom functions in the docs.