123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 'use strict';
- var Utils = require('./utils');
- var arrayPrefixGenerators = {
- brackets: function brackets(prefix) {
- return prefix + '[]';
- },
- indices: function indices(prefix, key) {
- return prefix + '[' + key + ']';
- },
- repeat: function repeat(prefix) {
- return prefix;
- }
- };
- var defaults = {
- delimiter: '&',
- strictNullHandling: false,
- skipNulls: false,
- encode: true,
- encoder: Utils.encode
- };
- var stringify = function stringify(object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots) {
- var obj = object;
- if (typeof filter === 'function') {
- obj = filter(prefix, obj);
- } else if (obj instanceof Date) {
- obj = obj.toISOString();
- } else if (obj === null) {
- if (strictNullHandling) {
- return encoder ? encoder(prefix) : prefix;
- }
- obj = '';
- }
- if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || Utils.isBuffer(obj)) {
- if (encoder) {
- return [encoder(prefix) + '=' + encoder(obj)];
- }
- return [prefix + '=' + String(obj)];
- }
- var values = [];
- if (typeof obj === 'undefined') {
- return values;
- }
- var objKeys;
- if (Array.isArray(filter)) {
- objKeys = filter;
- } else {
- var keys = Object.keys(obj);
- objKeys = sort ? keys.sort(sort) : keys;
- }
- for (var i = 0; i < objKeys.length; ++i) {
- var key = objKeys[i];
- if (skipNulls && obj[key] === null) {
- continue;
- }
- if (Array.isArray(obj)) {
- values = values.concat(stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
- } else {
- values = values.concat(stringify(obj[key], prefix + (allowDots ? '.' + key : '[' + key + ']'), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
- }
- }
- return values;
- };
- module.exports = function (object, opts) {
- var obj = object;
- var options = opts || {};
- var delimiter = typeof options.delimiter === 'undefined' ? defaults.delimiter : options.delimiter;
- var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
- var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls;
- var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode;
- var encoder = encode ? (typeof options.encoder === 'function' ? options.encoder : defaults.encoder) : null;
- var sort = typeof options.sort === 'function' ? options.sort : null;
- var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots;
- var objKeys;
- var filter;
- if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') {
- throw new TypeError('Encoder has to be a function.');
- }
- if (typeof options.filter === 'function') {
- filter = options.filter;
- obj = filter('', obj);
- } else if (Array.isArray(options.filter)) {
- objKeys = filter = options.filter;
- }
- var keys = [];
- if (typeof obj !== 'object' || obj === null) {
- return '';
- }
- var arrayFormat;
- if (options.arrayFormat in arrayPrefixGenerators) {
- arrayFormat = options.arrayFormat;
- } else if ('indices' in options) {
- arrayFormat = options.indices ? 'indices' : 'repeat';
- } else {
- arrayFormat = 'indices';
- }
- var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
- if (!objKeys) {
- objKeys = Object.keys(obj);
- }
- if (sort) {
- objKeys.sort(sort);
- }
- for (var i = 0; i < objKeys.length; ++i) {
- var key = objKeys[i];
- if (skipNulls && obj[key] === null) {
- continue;
- }
- keys = keys.concat(stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
- }
- return keys.join(delimiter);
- };
|