const seeded = Fiona(2983938)

Examples

Populate Template

Template String

An easy way to build up a long string is to use a javascript template literal populated from data generated by Fiona.

// input

const data = Fiona(2983938).object({
    fullname: Fiona.Fullname(),
    age: Fiona.Number({ max: 100 })
  }, {
    // pass as second argument so values in first argument
    // are calculated and can be consumed
    playThing: (seeded, value) => value.age < 5 ? 'cuddly toys' : 'friends',
    sentences: Fiona.Array({ min: 1, max: 5 }, Fiona.Sentence, '\n\n')
})

const templateFunction = d => `Dear ${d.fullname},

I know you are ${d.age} years old, and like playing with ${d.playThing}.

${d.sentences}

Have a nice day,

Fiona
x x x`

templateFunction(data)
// output

Dear Mr Elliot Alex Thomas Wilson,

I know you are 1 years old, and like playing with cuddly toys.

Minim id et non irure veniam reprehenderit amet ut officia ut eiusmod esse dolor ut dolor adipisicing labore in pariatur exercitation consequat ex dolore qui culpa ullamco.

Lorem ipsum eu ut elit deserunt mollit veniam consectetur in anim minim tempor dolor ut excepteur aute adipisicing et id.

Lorem ipsum amet ullamco cupidatat eu dolor magna in in fugiat nulla deserunt officia laborum minim est labore eiusmod aliquip irure.

Tempor irure elit dolor aute aliqua duis anim mollit sint sed eu magna pariatur ex sunt voluptate incididunt dolore dolore commodo dolor id ut culpa exercitation adipisicing labore in.

Ut tempor eu sunt ea sed fugiat incididunt dolore esse consequat ut enim duis officia proident magna non aliqua cupidatat minim id laborum qui dolor ad aute dolore.

Have a nice day,

Fiona
x x x

Template Plugin

To build a custom template plugin, you could use any template language. This example uses a lodash template.

// input

Fiona.register(['template', (seeded, ...data) => (templateArray, ...args) => {
  const templateString = templateArray.reduce((a, b) => a + args.shift().toString() + b)
  // assuming lodash is loaded, render template with values
  return _.template(templateString)(seeded.object(...data))
}])

Fiona(2983938).template({
    fullname: seeded => seeded.fullname(),
    color: seeded => seeded.oneOf(['red', 'orange', 'yellow', 'green', 'blue'])
})`Hi <%= fullname %>,

Your favourite colour is <%= color %>.

Have a nice day,

Fiona
x x x`
// output

Hi Mr Elliot Alex Thomas Wilson,

Your favourite colour is green.

Have a nice day,

Fiona
x x x