(examples, dataType = 'string')
| 196 | * }] |
| 197 | * */ |
| 198 | export 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 | |
| 249 | export function anyExampleWithSummaryOrDescription(examples) { |
| 250 | return examples.some((x) => x.summary?.length > 0 || x.description?.length > 0); |
no test coverage detected
searching dependent graphs…