Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options #89

Open
buschtoens opened this issue Dec 5, 2013 · 3 comments
Open

Options #89

buschtoens opened this issue Dec 5, 2013 · 3 comments

Comments

@buschtoens
Copy link
Collaborator

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
Copy link
Collaborator Author

Would close #1.

@tj
Copy link
Owner

tj commented Dec 5, 2013

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

@buschtoens
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants