stringify.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. var Utils = require('./utils');
  3. var arrayPrefixGenerators = {
  4. brackets: function brackets(prefix) {
  5. return prefix + '[]';
  6. },
  7. indices: function indices(prefix, key) {
  8. return prefix + '[' + key + ']';
  9. },
  10. repeat: function repeat(prefix) {
  11. return prefix;
  12. }
  13. };
  14. var defaults = {
  15. delimiter: '&',
  16. strictNullHandling: false,
  17. skipNulls: false,
  18. encode: true,
  19. encoder: Utils.encode
  20. };
  21. var stringify = function stringify(object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots) {
  22. var obj = object;
  23. if (typeof filter === 'function') {
  24. obj = filter(prefix, obj);
  25. } else if (obj instanceof Date) {
  26. obj = obj.toISOString();
  27. } else if (obj === null) {
  28. if (strictNullHandling) {
  29. return encoder ? encoder(prefix) : prefix;
  30. }
  31. obj = '';
  32. }
  33. if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || Utils.isBuffer(obj)) {
  34. if (encoder) {
  35. return [encoder(prefix) + '=' + encoder(obj)];
  36. }
  37. return [prefix + '=' + String(obj)];
  38. }
  39. var values = [];
  40. if (typeof obj === 'undefined') {
  41. return values;
  42. }
  43. var objKeys;
  44. if (Array.isArray(filter)) {
  45. objKeys = filter;
  46. } else {
  47. var keys = Object.keys(obj);
  48. objKeys = sort ? keys.sort(sort) : keys;
  49. }
  50. for (var i = 0; i < objKeys.length; ++i) {
  51. var key = objKeys[i];
  52. if (skipNulls && obj[key] === null) {
  53. continue;
  54. }
  55. if (Array.isArray(obj)) {
  56. values = values.concat(stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
  57. } else {
  58. values = values.concat(stringify(obj[key], prefix + (allowDots ? '.' + key : '[' + key + ']'), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
  59. }
  60. }
  61. return values;
  62. };
  63. module.exports = function (object, opts) {
  64. var obj = object;
  65. var options = opts || {};
  66. var delimiter = typeof options.delimiter === 'undefined' ? defaults.delimiter : options.delimiter;
  67. var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
  68. var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls;
  69. var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode;
  70. var encoder = encode ? (typeof options.encoder === 'function' ? options.encoder : defaults.encoder) : null;
  71. var sort = typeof options.sort === 'function' ? options.sort : null;
  72. var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots;
  73. var objKeys;
  74. var filter;
  75. if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') {
  76. throw new TypeError('Encoder has to be a function.');
  77. }
  78. if (typeof options.filter === 'function') {
  79. filter = options.filter;
  80. obj = filter('', obj);
  81. } else if (Array.isArray(options.filter)) {
  82. objKeys = filter = options.filter;
  83. }
  84. var keys = [];
  85. if (typeof obj !== 'object' || obj === null) {
  86. return '';
  87. }
  88. var arrayFormat;
  89. if (options.arrayFormat in arrayPrefixGenerators) {
  90. arrayFormat = options.arrayFormat;
  91. } else if ('indices' in options) {
  92. arrayFormat = options.indices ? 'indices' : 'repeat';
  93. } else {
  94. arrayFormat = 'indices';
  95. }
  96. var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
  97. if (!objKeys) {
  98. objKeys = Object.keys(obj);
  99. }
  100. if (sort) {
  101. objKeys.sort(sort);
  102. }
  103. for (var i = 0; i < objKeys.length; ++i) {
  104. var key = objKeys[i];
  105. if (skipNulls && obj[key] === null) {
  106. continue;
  107. }
  108. keys = keys.concat(stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
  109. }
  110. return keys.join(delimiter);
  111. };