You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.1 KiB

  1. 'use strict';
  2. var utils = require('./utils');
  3. var formats = require('./formats');
  4. var arrayPrefixGenerators = {
  5. brackets: function brackets(prefix) { // eslint-disable-line func-name-matching
  6. return prefix + '[]';
  7. },
  8. indices: function indices(prefix, key) { // eslint-disable-line func-name-matching
  9. return prefix + '[' + key + ']';
  10. },
  11. repeat: function repeat(prefix) { // eslint-disable-line func-name-matching
  12. return prefix;
  13. }
  14. };
  15. var toISO = Date.prototype.toISOString;
  16. var defaults = {
  17. delimiter: '&',
  18. encode: true,
  19. encoder: utils.encode,
  20. encodeValuesOnly: false,
  21. serializeDate: function serializeDate(date) { // eslint-disable-line func-name-matching
  22. return toISO.call(date);
  23. },
  24. skipNulls: false,
  25. strictNullHandling: false
  26. };
  27. var stringify = function stringify( // eslint-disable-line func-name-matching
  28. object,
  29. prefix,
  30. generateArrayPrefix,
  31. strictNullHandling,
  32. skipNulls,
  33. encoder,
  34. filter,
  35. sort,
  36. allowDots,
  37. serializeDate,
  38. formatter,
  39. encodeValuesOnly
  40. ) {
  41. var obj = object;
  42. if (typeof filter === 'function') {
  43. obj = filter(prefix, obj);
  44. } else if (obj instanceof Date) {
  45. obj = serializeDate(obj);
  46. } else if (obj === null) {
  47. if (strictNullHandling) {
  48. return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder) : prefix;
  49. }
  50. obj = '';
  51. }
  52. if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || utils.isBuffer(obj)) {
  53. if (encoder) {
  54. var keyValue = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder);
  55. return [formatter(keyValue) + '=' + formatter(encoder(obj, defaults.encoder))];
  56. }
  57. return [formatter(prefix) + '=' + formatter(String(obj))];
  58. }
  59. var values = [];
  60. if (typeof obj === 'undefined') {
  61. return values;
  62. }
  63. var objKeys;
  64. if (Array.isArray(filter)) {
  65. objKeys = filter;
  66. } else {
  67. var keys = Object.keys(obj);
  68. objKeys = sort ? keys.sort(sort) : keys;
  69. }
  70. for (var i = 0; i < objKeys.length; ++i) {
  71. var key = objKeys[i];
  72. if (skipNulls && obj[key] === null) {
  73. continue;
  74. }
  75. if (Array.isArray(obj)) {
  76. values = values.concat(stringify(
  77. obj[key],
  78. generateArrayPrefix(prefix, key),
  79. generateArrayPrefix,
  80. strictNullHandling,
  81. skipNulls,
  82. encoder,
  83. filter,
  84. sort,
  85. allowDots,
  86. serializeDate,
  87. formatter,
  88. encodeValuesOnly
  89. ));
  90. } else {
  91. values = values.concat(stringify(
  92. obj[key],
  93. prefix + (allowDots ? '.' + key : '[' + key + ']'),
  94. generateArrayPrefix,
  95. strictNullHandling,
  96. skipNulls,
  97. encoder,
  98. filter,
  99. sort,
  100. allowDots,
  101. serializeDate,
  102. formatter,
  103. encodeValuesOnly
  104. ));
  105. }
  106. }
  107. return values;
  108. };
  109. module.exports = function (object, opts) {
  110. var obj = object;
  111. var options = opts ? utils.assign({}, opts) : {};
  112. if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') {
  113. throw new TypeError('Encoder has to be a function.');
  114. }
  115. var delimiter = typeof options.delimiter === 'undefined' ? defaults.delimiter : options.delimiter;
  116. var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
  117. var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls;
  118. var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode;
  119. var encoder = typeof options.encoder === 'function' ? options.encoder : defaults.encoder;
  120. var sort = typeof options.sort === 'function' ? options.sort : null;
  121. var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots;
  122. var serializeDate = typeof options.serializeDate === 'function' ? options.serializeDate : defaults.serializeDate;
  123. var encodeValuesOnly = typeof options.encodeValuesOnly === 'boolean' ? options.encodeValuesOnly : defaults.encodeValuesOnly;
  124. if (typeof options.format === 'undefined') {
  125. options.format = formats['default'];
  126. } else if (!Object.prototype.hasOwnProperty.call(formats.formatters, options.format)) {
  127. throw new TypeError('Unknown format option provided.');
  128. }
  129. var formatter = formats.formatters[options.format];
  130. var objKeys;
  131. var filter;
  132. if (typeof options.filter === 'function') {
  133. filter = options.filter;
  134. obj = filter('', obj);
  135. } else if (Array.isArray(options.filter)) {
  136. filter = options.filter;
  137. objKeys = filter;
  138. }
  139. var keys = [];
  140. if (typeof obj !== 'object' || obj === null) {
  141. return '';
  142. }
  143. var arrayFormat;
  144. if (options.arrayFormat in arrayPrefixGenerators) {
  145. arrayFormat = options.arrayFormat;
  146. } else if ('indices' in options) {
  147. arrayFormat = options.indices ? 'indices' : 'repeat';
  148. } else {
  149. arrayFormat = 'indices';
  150. }
  151. var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
  152. if (!objKeys) {
  153. objKeys = Object.keys(obj);
  154. }
  155. if (sort) {
  156. objKeys.sort(sort);
  157. }
  158. for (var i = 0; i < objKeys.length; ++i) {
  159. var key = objKeys[i];
  160. if (skipNulls && obj[key] === null) {
  161. continue;
  162. }
  163. keys = keys.concat(stringify(
  164. obj[key],
  165. key,
  166. generateArrayPrefix,
  167. strictNullHandling,
  168. skipNulls,
  169. encode ? encoder : null,
  170. filter,
  171. sort,
  172. allowDots,
  173. serializeDate,
  174. formatter,
  175. encodeValuesOnly
  176. ));
  177. }
  178. var joined = keys.join(delimiter);
  179. var prefix = options.addQueryPrefix === true ? '?' : '';
  180. return joined.length > 0 ? prefix + joined : '';
  181. };