Node-Red configuration
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.

stringify.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. 'use strict';
  2. var getSideChannel = require('side-channel');
  3. var utils = require('./utils');
  4. var formats = require('./formats');
  5. var has = Object.prototype.hasOwnProperty;
  6. var arrayPrefixGenerators = {
  7. brackets: function brackets(prefix) {
  8. return prefix + '[]';
  9. },
  10. comma: 'comma',
  11. indices: function indices(prefix, key) {
  12. return prefix + '[' + key + ']';
  13. },
  14. repeat: function repeat(prefix) {
  15. return prefix;
  16. }
  17. };
  18. var isArray = Array.isArray;
  19. var split = String.prototype.split;
  20. var push = Array.prototype.push;
  21. var pushToArray = function (arr, valueOrArray) {
  22. push.apply(arr, isArray(valueOrArray) ? valueOrArray : [valueOrArray]);
  23. };
  24. var toISO = Date.prototype.toISOString;
  25. var defaultFormat = formats['default'];
  26. var defaults = {
  27. addQueryPrefix: false,
  28. allowDots: false,
  29. charset: 'utf-8',
  30. charsetSentinel: false,
  31. delimiter: '&',
  32. encode: true,
  33. encoder: utils.encode,
  34. encodeValuesOnly: false,
  35. format: defaultFormat,
  36. formatter: formats.formatters[defaultFormat],
  37. // deprecated
  38. indices: false,
  39. serializeDate: function serializeDate(date) {
  40. return toISO.call(date);
  41. },
  42. skipNulls: false,
  43. strictNullHandling: false
  44. };
  45. var isNonNullishPrimitive = function isNonNullishPrimitive(v) {
  46. return typeof v === 'string'
  47. || typeof v === 'number'
  48. || typeof v === 'boolean'
  49. || typeof v === 'symbol'
  50. || typeof v === 'bigint';
  51. };
  52. var sentinel = {};
  53. var stringify = function stringify(
  54. object,
  55. prefix,
  56. generateArrayPrefix,
  57. commaRoundTrip,
  58. strictNullHandling,
  59. skipNulls,
  60. encoder,
  61. filter,
  62. sort,
  63. allowDots,
  64. serializeDate,
  65. format,
  66. formatter,
  67. encodeValuesOnly,
  68. charset,
  69. sideChannel
  70. ) {
  71. var obj = object;
  72. var tmpSc = sideChannel;
  73. var step = 0;
  74. var findFlag = false;
  75. while ((tmpSc = tmpSc.get(sentinel)) !== void undefined && !findFlag) {
  76. // Where object last appeared in the ref tree
  77. var pos = tmpSc.get(object);
  78. step += 1;
  79. if (typeof pos !== 'undefined') {
  80. if (pos === step) {
  81. throw new RangeError('Cyclic object value');
  82. } else {
  83. findFlag = true; // Break while
  84. }
  85. }
  86. if (typeof tmpSc.get(sentinel) === 'undefined') {
  87. step = 0;
  88. }
  89. }
  90. if (typeof filter === 'function') {
  91. obj = filter(prefix, obj);
  92. } else if (obj instanceof Date) {
  93. obj = serializeDate(obj);
  94. } else if (generateArrayPrefix === 'comma' && isArray(obj)) {
  95. obj = utils.maybeMap(obj, function (value) {
  96. if (value instanceof Date) {
  97. return serializeDate(value);
  98. }
  99. return value;
  100. });
  101. }
  102. if (obj === null) {
  103. if (strictNullHandling) {
  104. return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder, charset, 'key', format) : prefix;
  105. }
  106. obj = '';
  107. }
  108. if (isNonNullishPrimitive(obj) || utils.isBuffer(obj)) {
  109. if (encoder) {
  110. var keyValue = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder, charset, 'key', format);
  111. if (generateArrayPrefix === 'comma' && encodeValuesOnly) {
  112. var valuesArray = split.call(String(obj), ',');
  113. var valuesJoined = '';
  114. for (var i = 0; i < valuesArray.length; ++i) {
  115. valuesJoined += (i === 0 ? '' : ',') + formatter(encoder(valuesArray[i], defaults.encoder, charset, 'value', format));
  116. }
  117. return [formatter(keyValue) + (commaRoundTrip && isArray(obj) && valuesArray.length === 1 ? '[]' : '') + '=' + valuesJoined];
  118. }
  119. return [formatter(keyValue) + '=' + formatter(encoder(obj, defaults.encoder, charset, 'value', format))];
  120. }
  121. return [formatter(prefix) + '=' + formatter(String(obj))];
  122. }
  123. var values = [];
  124. if (typeof obj === 'undefined') {
  125. return values;
  126. }
  127. var objKeys;
  128. if (generateArrayPrefix === 'comma' && isArray(obj)) {
  129. // we need to join elements in
  130. objKeys = [{ value: obj.length > 0 ? obj.join(',') || null : void undefined }];
  131. } else if (isArray(filter)) {
  132. objKeys = filter;
  133. } else {
  134. var keys = Object.keys(obj);
  135. objKeys = sort ? keys.sort(sort) : keys;
  136. }
  137. var adjustedPrefix = commaRoundTrip && isArray(obj) && obj.length === 1 ? prefix + '[]' : prefix;
  138. for (var j = 0; j < objKeys.length; ++j) {
  139. var key = objKeys[j];
  140. var value = typeof key === 'object' && typeof key.value !== 'undefined' ? key.value : obj[key];
  141. if (skipNulls && value === null) {
  142. continue;
  143. }
  144. var keyPrefix = isArray(obj)
  145. ? typeof generateArrayPrefix === 'function' ? generateArrayPrefix(adjustedPrefix, key) : adjustedPrefix
  146. : adjustedPrefix + (allowDots ? '.' + key : '[' + key + ']');
  147. sideChannel.set(object, step);
  148. var valueSideChannel = getSideChannel();
  149. valueSideChannel.set(sentinel, sideChannel);
  150. pushToArray(values, stringify(
  151. value,
  152. keyPrefix,
  153. generateArrayPrefix,
  154. commaRoundTrip,
  155. strictNullHandling,
  156. skipNulls,
  157. encoder,
  158. filter,
  159. sort,
  160. allowDots,
  161. serializeDate,
  162. format,
  163. formatter,
  164. encodeValuesOnly,
  165. charset,
  166. valueSideChannel
  167. ));
  168. }
  169. return values;
  170. };
  171. var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
  172. if (!opts) {
  173. return defaults;
  174. }
  175. if (opts.encoder !== null && typeof opts.encoder !== 'undefined' && typeof opts.encoder !== 'function') {
  176. throw new TypeError('Encoder has to be a function.');
  177. }
  178. var charset = opts.charset || defaults.charset;
  179. if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
  180. throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
  181. }
  182. var format = formats['default'];
  183. if (typeof opts.format !== 'undefined') {
  184. if (!has.call(formats.formatters, opts.format)) {
  185. throw new TypeError('Unknown format option provided.');
  186. }
  187. format = opts.format;
  188. }
  189. var formatter = formats.formatters[format];
  190. var filter = defaults.filter;
  191. if (typeof opts.filter === 'function' || isArray(opts.filter)) {
  192. filter = opts.filter;
  193. }
  194. return {
  195. addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
  196. allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots,
  197. charset: charset,
  198. charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
  199. delimiter: typeof opts.delimiter === 'undefined' ? defaults.delimiter : opts.delimiter,
  200. encode: typeof opts.encode === 'boolean' ? opts.encode : defaults.encode,
  201. encoder: typeof opts.encoder === 'function' ? opts.encoder : defaults.encoder,
  202. encodeValuesOnly: typeof opts.encodeValuesOnly === 'boolean' ? opts.encodeValuesOnly : defaults.encodeValuesOnly,
  203. filter: filter,
  204. format: format,
  205. formatter: formatter,
  206. serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate,
  207. skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls,
  208. sort: typeof opts.sort === 'function' ? opts.sort : null,
  209. strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
  210. };
  211. };
  212. module.exports = function (object, opts) {
  213. var obj = object;
  214. var options = normalizeStringifyOptions(opts);
  215. var objKeys;
  216. var filter;
  217. if (typeof options.filter === 'function') {
  218. filter = options.filter;
  219. obj = filter('', obj);
  220. } else if (isArray(options.filter)) {
  221. filter = options.filter;
  222. objKeys = filter;
  223. }
  224. var keys = [];
  225. if (typeof obj !== 'object' || obj === null) {
  226. return '';
  227. }
  228. var arrayFormat;
  229. if (opts && opts.arrayFormat in arrayPrefixGenerators) {
  230. arrayFormat = opts.arrayFormat;
  231. } else if (opts && 'indices' in opts) {
  232. arrayFormat = opts.indices ? 'indices' : 'repeat';
  233. } else {
  234. arrayFormat = 'indices';
  235. }
  236. var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
  237. if (opts && 'commaRoundTrip' in opts && typeof opts.commaRoundTrip !== 'boolean') {
  238. throw new TypeError('`commaRoundTrip` must be a boolean, or absent');
  239. }
  240. var commaRoundTrip = generateArrayPrefix === 'comma' && opts && opts.commaRoundTrip;
  241. if (!objKeys) {
  242. objKeys = Object.keys(obj);
  243. }
  244. if (options.sort) {
  245. objKeys.sort(options.sort);
  246. }
  247. var sideChannel = getSideChannel();
  248. for (var i = 0; i < objKeys.length; ++i) {
  249. var key = objKeys[i];
  250. if (options.skipNulls && obj[key] === null) {
  251. continue;
  252. }
  253. pushToArray(keys, stringify(
  254. obj[key],
  255. key,
  256. generateArrayPrefix,
  257. commaRoundTrip,
  258. options.strictNullHandling,
  259. options.skipNulls,
  260. options.encode ? options.encoder : null,
  261. options.filter,
  262. options.sort,
  263. options.allowDots,
  264. options.serializeDate,
  265. options.format,
  266. options.formatter,
  267. options.encodeValuesOnly,
  268. options.charset,
  269. sideChannel
  270. ));
  271. }
  272. var joined = keys.join(options.delimiter);
  273. var prefix = options.addQueryPrefix === true ? '?' : '';
  274. if (options.charsetSentinel) {
  275. if (options.charset === 'iso-8859-1') {
  276. // encodeURIComponent('&#10003;'), the "numeric entity" representation of a checkmark
  277. prefix += 'utf8=%26%2310003%3B&';
  278. } else {
  279. // encodeURIComponent('✓')
  280. prefix += 'utf8=%E2%9C%93&';
  281. }
  282. }
  283. return joined.length > 0 ? prefix + joined : '';
  284. };