stringify.js 6.1 KB

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