source: node_modules/swagger-client/es/execute/oas3/style-serializer.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[d24f17c]1const isRfc3986Reserved = char => ":/?#[]@!$&'()*+,;=".indexOf(char) > -1;
2const isRrc3986Unreserved = char => /^[a-z0-9\-._~]+$/i.test(char);
3
4// eslint-disable-next-line default-param-last
5export function encodeDisallowedCharacters(str, {
6 escape
7} = {}, parse) {
8 if (typeof str === 'number') {
9 str = str.toString();
10 }
11 if (typeof str !== 'string' || !str.length) {
12 return str;
13 }
14 if (!escape) {
15 return str;
16 }
17 if (parse) {
18 return JSON.parse(str);
19 }
20
21 // In ES6 you can do this quite easily by using the new ... spread operator.
22 // This causes the string iterator (another new ES6 feature) to be used internally,
23 // and because that iterator is designed to deal with
24 // code points rather than UCS-2/UTF-16 code units.
25 return [...str].map(char => {
26 if (isRrc3986Unreserved(char)) {
27 return char;
28 }
29 if (isRfc3986Reserved(char) && escape === 'unsafe') {
30 return char;
31 }
32 const encoder = new TextEncoder();
33 const encoded = Array.from(encoder.encode(char)).map(byte => `0${byte.toString(16).toUpperCase()}`.slice(-2)).map(encodedByte => `%${encodedByte}`).join('');
34 return encoded;
35 }).join('');
36}
37export default function stylize(config) {
38 const {
39 value
40 } = config;
41 if (Array.isArray(value)) {
42 return encodeArray(config);
43 }
44 if (typeof value === 'object') {
45 return encodeObject(config);
46 }
47 return encodePrimitive(config);
48}
49function encodeArray({
50 key,
51 value,
52 style,
53 explode,
54 escape
55}) {
56 const valueEncoder = str => encodeDisallowedCharacters(str, {
57 escape
58 });
59 if (style === 'simple') {
60 return value.map(val => valueEncoder(val)).join(',');
61 }
62 if (style === 'label') {
63 return `.${value.map(val => valueEncoder(val)).join('.')}`;
64 }
65 if (style === 'matrix') {
66 return value.map(val => valueEncoder(val)).reduce((prev, curr) => {
67 if (!prev || explode) {
68 return `${prev || ''};${key}=${curr}`;
69 }
70 return `${prev},${curr}`;
71 }, '');
72 }
73 if (style === 'form') {
74 const after = explode ? `&${key}=` : ',';
75 return value.map(val => valueEncoder(val)).join(after);
76 }
77 if (style === 'spaceDelimited') {
78 const after = explode ? `${key}=` : '';
79 return value.map(val => valueEncoder(val)).join(` ${after}`);
80 }
81 if (style === 'pipeDelimited') {
82 const after = explode ? `${key}=` : '';
83 return value.map(val => valueEncoder(val)).join(`|${after}`);
84 }
85 return undefined;
86}
87function encodeObject({
88 key,
89 value,
90 style,
91 explode,
92 escape
93}) {
94 const valueEncoder = str => encodeDisallowedCharacters(str, {
95 escape
96 });
97 const valueKeys = Object.keys(value);
98 if (style === 'simple') {
99 return valueKeys.reduce((prev, curr) => {
100 const val = valueEncoder(value[curr]);
101 const middleChar = explode ? '=' : ',';
102 const prefix = prev ? `${prev},` : '';
103 return `${prefix}${curr}${middleChar}${val}`;
104 }, '');
105 }
106 if (style === 'label') {
107 return valueKeys.reduce((prev, curr) => {
108 const val = valueEncoder(value[curr]);
109 const middleChar = explode ? '=' : '.';
110 const prefix = prev ? `${prev}.` : '.';
111 return `${prefix}${curr}${middleChar}${val}`;
112 }, '');
113 }
114 if (style === 'matrix' && explode) {
115 return valueKeys.reduce((prev, curr) => {
116 const val = valueEncoder(value[curr]);
117 const prefix = prev ? `${prev};` : ';';
118 return `${prefix}${curr}=${val}`;
119 }, '');
120 }
121 if (style === 'matrix') {
122 // no explode
123 return valueKeys.reduce((prev, curr) => {
124 const val = valueEncoder(value[curr]);
125 const prefix = prev ? `${prev},` : `;${key}=`;
126 return `${prefix}${curr},${val}`;
127 }, '');
128 }
129 if (style === 'form') {
130 return valueKeys.reduce((prev, curr) => {
131 const val = valueEncoder(value[curr]);
132 const prefix = prev ? `${prev}${explode ? '&' : ','}` : '';
133 const separator = explode ? '=' : ',';
134 return `${prefix}${curr}${separator}${val}`;
135 }, '');
136 }
137 return undefined;
138}
139function encodePrimitive({
140 key,
141 value,
142 style,
143 escape
144}) {
145 const valueEncoder = str => encodeDisallowedCharacters(str, {
146 escape
147 });
148 if (style === 'simple') {
149 return valueEncoder(value);
150 }
151 if (style === 'label') {
152 return `.${valueEncoder(value)}`;
153 }
154 if (style === 'matrix') {
155 return `;${key}=${valueEncoder(value)}`;
156 }
157 if (style === 'form') {
158 return valueEncoder(value);
159 }
160 if (style === 'deepObject') {
161 return valueEncoder(value, {}, true);
162 }
163 return undefined;
164}
Note: See TracBrowser for help on using the repository browser.