MCPcopy
hub / github.com/faker-js/faker / fake

Method fake

src/modules/helpers/index.ts:1350–1378  ·  view source on GitHub ↗
(pattern: string | ReadonlyArray<string>)

Source from the content-addressed store, hash-verified

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}

Callers 15

testAssertionFunction · 0.80
helpers.spec.tsFile · 0.80
zipCodeMethod · 0.80
cityMethod · 0.80
streetMethod · 0.80
streetAddressMethod · 0.80
postalAddressMethod · 0.80
secondaryAddressMethod · 0.80
descriptionMethod · 0.80
dishMethod · 0.80
phraseMethod · 0.80

Calls 2

fakeEvalFunction · 0.90
arrayElementMethod · 0.80

Tested by 1

testAssertionFunction · 0.64