MCPcopy
hub / github.com/rapi-doc/RapiDoc / normalizeExamples

Function normalizeExamples

src/utils/schema-utils.js:198–247  ·  view source on GitHub ↗
(examples, dataType = 'string')

Source from the content-addressed store, hash-verified

196 * }]
197 * */
198export function normalizeExamples(examples, dataType = 'string') {
199 if (!examples) {
200 return {
201 exampleVal: '',
202 exampleList: [],
203 };
204 }
205 if (examples.constructor === Object) {
206 const exampleList = Object.values(examples)
207 .filter((v) => (v['x-example-show-value'] !== false))
208 .map((v) => ({
209 value: (typeof v.value === 'boolean' || typeof v.value === 'number' ? `${v.value}` : (v.value || '')),
210 printableValue: getPrintableVal(v.value),
211 summary: v.summary || '',
212 description: v.description || '',
213 }));
214 const exampleVal = exampleList.length > 0
215 ? exampleList[0].value
216 : '';
217 return { exampleVal, exampleList };
218 }
219
220 // This is non-standard way to provide example but will support for now
221 if (!Array.isArray(examples)) {
222 examples = examples ? [examples] : [];
223 }
224
225 if (examples.length === 0) {
226 return {
227 exampleVal: '',
228 exampleList: [],
229 };
230 }
231
232 if (dataType === 'array') {
233 const [exampleVal] = examples;
234 const exampleList = examples.map((v) => ({
235 value: v,
236 printableValue: getPrintableVal(v),
237 }));
238 return { exampleVal, exampleList };
239 }
240
241 const exampleVal = examples[0].toString();
242 const exampleList = examples.map((v) => ({
243 value: v.toString(),
244 printableValue: getPrintableVal(v),
245 }));
246 return { exampleVal, exampleList };
247}
248
249export function anyExampleWithSummaryOrDescription(examples) {
250 return examples.some((x) => x.summary?.length > 0 || x.description?.length > 0);

Callers 2

formDataTemplateMethod · 0.90

Calls 5

getPrintableValFunction · 0.70
mapMethod · 0.45
filterMethod · 0.45
valuesMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…