MCPcopy Create free account
hub / github.com/sindresorhus/query-string / encoderForArrayFormat

Function encoderForArrayFormat

base.js:37–79  ·  view source on GitHub ↗
(options)

Source from the content-addressed store, hash-verified

35}
36
37function encoderForArrayFormat(options) {
38 const {arrayFormat} = options;
39
40 if (['comma', 'separator', 'bracket-separator'].includes(arrayFormat)) {
41 const keyValueSeparator = arrayFormat === 'bracket-separator' ? '[]=' : '=';
42
43 return key => (result, value) => {
44 if (isSkipped(value, options)) {
45 return result;
46 }
47
48 // Translate null to an empty string so that it doesn't serialize as 'null'
49 const item = encodeArrayItem(value === null ? '' : value, options);
50
51 if (result.length === 0) {
52 result.push([encode(key, options), keyValueSeparator, item].join(''));
53 return result;
54 }
55
56 result.push(item);
57 return result;
58 };
59 }
60
61 // These formats write one parameter per item, with the marker in the name.
62 const marker = itemMarkerFor(arrayFormat);
63
64 return key => (result, value) => {
65 if (isSkipped(value, options)) {
66 return result;
67 }
68
69 const name = encode(key, options) + marker(result.length);
70
71 if (value === null) {
72 result.push(name);
73 return result;
74 }
75
76 result.push([name, '=', encode(value, options)].join(''));
77 return result;
78 };
79}
80
81function parserForArrayFormat(options) {
82 switch (options.arrayFormat) {

Callers 1

stringifyFunction · 0.85

Calls 4

isSkippedFunction · 0.85
encodeArrayItemFunction · 0.85
encodeFunction · 0.85
itemMarkerForFunction · 0.85

Tested by

no test coverage detected