Expressions¶
Datafaker supports different kinds of expressions which allows to customize the output.
Letterify¶
This one will replace ? symbols with latin letters e.g.
Numerify¶
This one will replace # symbols with digits e.g.
Bothify¶
Applies both letterify and numerify e.g.
Templatify¶
This is available since 1.2.0.
This one will replace symbol mentioned in the second args with one of symbols mentioned after it.
Faker faker = new Faker();
// e.g. there is expression test and we want to replace t with q or @
faker.expression("#{templatify 'test','t','q','@'}"); // could give @esq
// another example there is expression test and we want to replace t with q or @ or $ or *
faker.expression("#{templatify 'test','t','q','@','$','*'}"); // could give @esq
Examplify¶
This one will replace symbols by example: uppercase with uppercase, digit with digit, lowercase with lowercase.
Regexify¶
This one allows generating output based on regular expression(s), e.g.
Options¶
This is available since 1.2.0.
This will return one from the provided options e.g.
CSV¶
This is available since 1.4.0.
The expression will return generated csv based on input parameters.
faker.expression("#{csv '1','name_column','#{Name.first_name}','last_name_column','#{Name.last_name}'}");
// "name_column","last_name_column"
// "Sabrina","Kihn"
faker.expression("#{csv ' ### ','\"','false','3','name_column','#{Name.first_name}','last_name_column','#{Name.last_name}'}");
// "Thad" ### "Crist"
// "Kathryne" ### "Wuckert"
// "Sybil" ### "Connelly"
JSON¶
This is available since 1.4.0.
The expression will return generated json based on input parameters.
faker.expression("#{json 'person','#{json ''first_name'',''#{Name.first_name}'',''last_name'',''#{Name.last_name}''}','address','#{json ''country'',''#{Address.country}'',''city'',''#{Address.city}''}'}");
// {"person": {"first_name": "Barbie", "last_name": "Durgan"}, "address": {"country": "Albania", "city": "East Catarinahaven"}}
Others¶
It is possible to call methods returning string values and taking primitive or string args via expressions e.g.