Giter Club home page Giter Club logo

faker's Introduction

Faker

Generate massive amounts of fake (but realistic) data for testing and development.

Chat on Discord Continuous Integration

Installation

Please replace your faker dependency with @faker-js/faker. This is the official, stable fork of Faker.

npm install @faker-js/faker --save-dev

or yarn

yarn add @faker-js/faker -D

or pnpm

pnpm install @faker-js/faker -D

Browser

<script src="faker.js" type="text/javascript"></script>
<script>
  const randomName = faker.name.findName(); // Caitlyn Kerluke
  const randomEmail = faker.internet.email(); // [email protected]
  const randomCard = faker.helpers.createCard(); // random contact card containing many properties
</script>

Node.js

const faker = require('@faker-js/faker');
const randomName = faker.name.findName(); // Rowan Nikolaus
const randomEmail = faker.internet.email(); // [email protected]
const randomCard = faker.helpers.createCard(); // random contact card containing many properties

Typescript Support

Types are available via the @types/faker package. You must manually link them using a *.d.ts file, like so:

// faker.d.ts
declare module '@faker-js/faker' {
  import faker from 'faker';
  export default faker;
}

API

An in-depth overview of the API methods is available in the documentation. The API covers the following modules:

Module Example Output
Address faker.address.city() Lake Raoulfort
Animal faker.animal.type() Dog, cat, snake, bear, lion, etc.
Commerce faker.commerce.product() Polo t-shirt
Company faker.company.companyName() Zboncak and Sons
Database faker.database.engine() MyISAM
Datatype faker.datatype.uuid() 1oijf8-3iuhiu-21jddj-1092jf
Date faker.date.past() Sat Oct 20 2018 04:19:38 GMT-0700 (Pacific Daylight Time)
Finance faker.finance.amount() ¥23400 (After setting locale)
Git faker.git.commitMessage() feat: add products list page
Hacker faker.hacker.phrase() Try to reboot the SQL bus, maybe it will bypass the virtual application!
Helpers faker.helpers.userCard() { avatar: ‘...’, email: ‘{ first }{ last }{ number }@{domain}’, first: '...' }

All of the values are self-consistent (e.g. same first + last name in the email, too)
Image faker.image.avatar() https://s3.amazonaws.com/uifaces/faces/twitter/martip07/128.jpg
Internet faker.internet.color() #630c7b
Lorem faker.lorem.paragraph() Word, words, sentences, slug (lorem-ipsum), paragraph(s), text, lines
Music faker.music.genre() R&B
Name faker.name.firstName() Cameron
Phone faker.phone.phoneNumber() +1 291-299-0192
System faker.system.directoryPath() C:\Documents\Newsletters\
Vehicle faker.vehicle.vehicle() 2011 Dodge Caravan

Localization

Faker has support for multiple locales.

The default language locale is set to English.

Setting a new locale is simple:

// sets locale to de
faker.locale = 'de';

List of locales: az, ar, cz, de, de_AT, de_CH, en, en_AU, en_AU_ocker, en_BORK, en_CA, en_GB, en_IE, en_IND, en_US, en_ZA, es, es_MX, fa, fi, fr, fr_CA, fr_CH, ge, hy, hr, id_ID, it, ja, ko, nb_NO, ne, nl, nl_BE, pl, pt_BR, pt_PT, ro, ru, sk, sv, tr, uk, vi, zh_CN, zh_TW

Individual Localization Packages

Faker supports incremental loading of locales.

// loads only de locale
const faker = require('@faker-js/faker/locale/de');

Setting a randomness seed

If you want consistent results, you can set your own seed:

faker.seed(123);

const firstRandom = faker.datatype.number();

// Setting the seed again resets the sequence.
faker.seed(123);

const secondRandom = faker.datatype.number();

console.log(firstRandom === secondRandom);

Contributing

Building Faker

Faker uses gulp to automate its build process. Each build operation is a separate task which can be run independently.

Browser Bundle

npm run browser

Testing

npm install
npm run test

You can view a code coverage report generated in coverage/lcov-report/index.html.

Developing the docs

# build the Faker library for the browser
# it's used inside of certain routes
npm run browser

npm run docs:dev

Building and serving the docs statically

# build the Faker library for the browser
# it's used inside of certain routes
npm run browser

npm run docs:build # Output docs to /dist
npm run docs:serve # Serve docs from /dist

Deploying Documentation

The website is kindly hosted for free by the Netlify team under their Open Source plan. See the netlify.toml for configuration.

Building JSDocs

JSDOC v3 HTML API documentation

npm run jsdoc

What happened to the original faker.js?

Read the team update (January 14th, 2022).

faker's People

Contributors

marak avatar fotoverite avatar shinigami92 avatar bryandonovan avatar umairjibran avatar fariborzemami avatar tylerreichle avatar jsoref avatar moosh-be avatar jayliu50 avatar cliffpyles avatar damienwebdev avatar stichoza avatar prinx avatar josefsalyer avatar lucasjellema avatar glyad avatar mandulaj avatar van100j avatar robscotts4rb avatar jreina avatar mrstebo avatar ducin avatar lbuerste avatar 0xchimz avatar portse avatar vivekseth avatar sbmart avatar moonwalker0 avatar almazn avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.