npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@maarkis/fluent-faker

v0.2.0

Published

[![CI](https://github.com/Maarkis/fluent-faker/actions/workflows/ci.yml/badge.svg)](https://github.com/Maarkis/fluent-faker/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/Maarkis/fluent-faker/branch/main/graph/badge.svg?token=5EEBKP7XZQ)](ht

Downloads

48

Readme

Fluent-Faker

CI codecov

Overview

A library for generating data in a simple way for testing

Faker is used for fake data generation

Getting Started

Install

Install it as a Dev Dependency using your favorite package manager

npm install @maarkis/fluent-faker --save-dev

or

pnpm install @maarkis/fluent-faker --save-dev

or

yarn add @maarkis/fluent-faker --dev

Usage

interface Todo {
    id: number,
    name: string,
    description: string,
    done: boolean,
}
import {createBuilder} from '@maarkis/fluent-faker'

// unique instance
createBuilder<Todo>({id: 1, name: 'Todo 1'}).generate() // { id: 1, name: 'Todo 1' }

// using faker
createBuilder<Todo>((faker) = (
    {
        id: faker.number.int(),
        name: faker.lorem.word()
    }))
    .generate() // { id: 654, name: 'eaque' }
import {generate} from '@maarkis/fluent-faker'

// unique instance
generate<Todo>({id: 1, name: 'Todo 1'}) // { id: 1, name: 'Todo 1' }

// unique instance
generate<Todo>({id: 1, name: 'Todo 1'}, 2)
// [{ id: 1, name: 'Todo 1' },{ id: 1, name: 'Todo 1' }]

Switching locales

By default, fluent-faker uses the default locale of Faker.js (en) when constructing new instances of the Builder.

You can change the locale by providing the locale parameter during the construction of the Builder class.

import {Builder} from '@maarkis/fluent-faker'

new Builder<Todo>('pt_BR')
Check Available locales in Faker.js documentation.

Using seed

Sets the seed or generates a new one

Please note that generated values are dependent on both the seed and the number of calls that have been made since it was set

This method is intended to allow for consistent values in a tests, so you might want to use hardcoded values as the seed

Global:

import {useSeed} from '@maarkis/fluent-faker'

useSeed(596) // 596

global scope, values modify Faker.js lib

Local:

import {Builder} from '@maarkis/fluent-faker'

new Builder<Todo>().useSeed(596) // 596

API Reference

createBuilder

Parameters:

| Name | Type | Description | required | |----------|-------------------------|-------------------------------|:--------:| | model | Partial<T> / Function | Initial setup for the builder | no | | locale | string | The locale to set | no |

Returns: new Builder instance

Usage:

import {createBuilder} from "@maarkis/fluent-faker";

createBuilder<Todo>({ id: 1, name: 'Todo 1' })

or

import {createBuilder} from "@maarkis/fluent-faker";

createBuilder<Todo>((faker) => ({
    id: faker.number.int(),
    name: 'Todo 1'
}))

or

import {createBuilder} from "@maarkis/fluent-faker";

createBuilder<Todo>(() => ({ id: 1, name: 'Todo 1' }), 'pt_BR')

generate

Parameters:

| Name | Type | Description | required | |----------|-------------------------|-------------------------------|:--------:| | model | Partial<T> / Function | Initial setup for the builder | no | | length | number | The locale to set | no |

Returns: new T instance or collection

Usage:

import {generate} from "@maarkis/fluent-faker";

generate<Todo>({ id: 1, name: 'Todo 1'}) // { id: 1, name: 'Todo 1' }

or

import {generate} from "@maarkis/fluent-faker";

generate<Todo>((faker) => ({
    id: faker.number.int(),
    name: 'Todo 1'
})) // { id: 8874, name: 'Todo 1' }

or

import {generate} from "@maarkis/fluent-faker";

generate<Todo>((faker) => ({
    id: faker.number.int(),
    name: 'Todo 1'
}), 2) // [{ id: 8874, name: 'Todo 1' },{ id: 97856, name: 'Todo 1' }]

Builder

addModel

Parameters:

| Name | Type | Description | required | |---------|-------------------------|-------------------------|:--------:| | model | Partial<T> / Function | Defines a set of rules. | yes |

Returns: Builder instance

Usage:

new Builder<Todo>()
    .addModel({id: 1, name: 'Todo 1'})
    .generate() // { id: 1, name: 'Todo 1' }

or

new Builder<Todo>()
    .addModel((faker) => ({
        id: faker.number.int(),
        name: 'Todo 1'
    }))
    .generate() // { id: 9763, name: 'Todo 1' }

addSet

Parameters:

| Name | Type | Description | required | |-----------|-------------------------|------------------------------------------------|:--------:| | name | string | The set name | yes | | dataSet | Partial<T> / Function | The dataset to apply when the set is specified | yes |

Returns: Builder instance

Usage:

new Builder<Todo>()
    .addSet('todo done', {id: 1, name: 'Todo 1', done: true})
    .useSet('todo done')
    .generate() // { id: 1, name: 'Todo 1', done: true }

or

new Builder<Todo>()
    .addSet('todo done', (faker) => ({
        id: faker.number.int(),
        name: 'Todo 1',
        done: true
    }))
    .useSet('todo done')
    .generate() // { id: 9763, name: 'Todo 1', done: true }

clone

Description: Clone hte internal state into a new so that both are isolated from each other

Returns: new Builder instance

Usage:

new Builder<Todo>().clone()

generate

Parameters:

| Name | Type | Description | required | note | |----------|----------|----------------------------------|:--------:|----------------------------------------------------------------| | length | number | The number of instances to spawn | no | If you don't pass a length, only one entity will be generated. |

Returns: T instance or collection of T

Usage:

new Builder<Todo>()
    .addModel({id: 1, name: 'Todo 1'})
    .generate() // { id: 1, name: 'Todo 1' }

or

new Builder<Todo>()
    .addModel({id: 1, name: 'Todo 1'})
    .generate(2) // [{ id: 1, name: 'Todo 1' }, { id: 1, name: 'Todo 1' }]

ruleFor

Parameters:

| Name | Type | Description | required | |-----------------|------------------------------|-----------------------|:--------:| | property | P extends keyof T / string | Property of an entity | yes | | valueFunction | T[keyof T] / Function | Value of a property | yes |

Returns: Builder instance

Usage:

new Builder<Todo>()
    .ruleFor('id', 1)
    .generate() // { id: 1 }

or

new Builder<Todo>()
    .ruleFor('id', () => 1)
    .generate() // { id: 1 }

or

new Builder<Todo>()
    .ruleFor('id', (faker) => faker.number.int())
    .generate() // { id: 1564 }

useSeed

Parameters:

| Name | Type | Description | required | |--------|----------|-----------------|:--------:| | seed | number | The seed to set | yes |

Returns: number

Usage:

new Builder<Todo>().useSeed(1) // 1

useSet

Parameters:

| Name | Type | Description | |--------|----------|--------------| | name | string | The set name |

Returns: Builder instance

Usage:

new Builder<Todo>()
    .addSet('todo done', {id: 1, name: 'Todo 1', done: true})
    .useSet('todo done')

or

new Builder<Todo>()
    .addSet('todo done', (faker) => ({
        id: faker.number.int(),
        name: 'Todo 1',
        done: true
    }))
    .useSet('todo done')