| 1348 | */ |
| 1349 | fake(pattern: string | ReadonlyArray<string>): string; |
| 1350 | fake(pattern: string | ReadonlyArray<string>): string { |
| 1351 | pattern = |
| 1352 | typeof pattern === 'string' ? pattern : this.arrayElement(pattern); |
| 1353 | |
| 1354 | // find first matching {{ and }} |
| 1355 | const start = pattern.search(/{{[a-z]/); |
| 1356 | const end = pattern.indexOf('}}', start); |
| 1357 | |
| 1358 | // if no {{ and }} is found, we are done |
| 1359 | if (start === -1 || end === -1) { |
| 1360 | return pattern; |
| 1361 | } |
| 1362 | |
| 1363 | // extract method name from between the {{ }} that we found |
| 1364 | // for example: {{person.firstName}} |
| 1365 | const token = pattern.substring(start + 2, end + 2); |
| 1366 | const method = token.replace('}}', '').replace('{{', ''); |
| 1367 | |
| 1368 | const result = fakeEval(method, this.faker); |
| 1369 | const stringified = String(result); |
| 1370 | |
| 1371 | // Replace the found tag with the returned fake value |
| 1372 | // We cannot use string.replace here because the result might contain evaluated characters |
| 1373 | const patched = |
| 1374 | pattern.substring(0, start) + stringified + pattern.substring(end + 2); |
| 1375 | |
| 1376 | // return the response recursively until we are done finding all tags |
| 1377 | return this.fake(patched); |
| 1378 | } |
| 1379 | } |