@faker-js/faker vs casual vs chance vs lorem-ipsum vs mockjs vs randexp
数据生成和模拟
@faker-js/fakercasualchancelorem-ipsummockjsrandexp类似的npm包:

数据生成和模拟

数据生成和模拟库是用于在开发和测试过程中自动生成虚拟数据的工具。这些库可以创建随机的、逼真的数据,以填充数据库、测试应用程序或生成占位符内容。它们在前端和后端开发中都非常有用,尤其是在需要大量数据进行性能测试、用户界面设计或数据驱动应用程序时。@faker-js/faker 是一个现代化的、功能丰富的库,提供多种数据生成器,支持国际化和自定义;casual 是一个简单易用的库,支持生成随机数据,具有良好的可扩展性;chance 提供丰富的随机数据生成 API,支持多种数据类型,适合需要高质量随机数据的场景;lorem-ipsum 专注于生成虚拟文本,特别是拉丁文占位符文本,适合设计和排版;mockjs 是一个强大的模拟数据生成工具,支持根据定义的规则生成复杂的 JSON 数据,适合前端开发和接口模拟;randexp 专注于根据正则表达式生成随机字符串,适合需要特定格式数据的场景。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
@faker-js/faker014,9954.16 MB12311 天前MIT
casual03,011-417 年前MIT
chance06,5542.13 MB1771 年前MIT
lorem-ipsum0296133 kB6-ISC
mockjs019,653-3426 年前-
randexp01,872-148 年前MIT

功能对比: @faker-js/faker vs casual vs chance vs lorem-ipsum vs mockjs vs randexp

数据生成能力

  • @faker-js/faker:

    @faker-js/faker 提供多种数据生成器,包括姓名、地址、公司、电子邮件、图片等,支持国际化和自定义,生成的数据类型丰富且多样。

  • casual:

    casual 提供基本的随机数据生成能力,包括姓名、地址、日期、颜色等,支持自定义数据生成器,但数据类型相对较少。

  • chance:

    chance 提供广泛的随机数据生成 API,支持姓名、地址、日期、颜色、电话号码、信用卡等多种数据类型,数据生成能力强大且多样化。

  • lorem-ipsum:

    lorem-ipsum 专注于生成拉丁文占位符文本,支持自定义段落、句子和词的数量,适合需要文本占位符的场景。

  • mockjs:

    mockjs 支持根据定义的规则生成复杂的 JSON 数据,支持随机生成字符串、数字、布尔值,支持自定义数据生成规则,适合模拟真实数据结构。

  • randexp:

    randexp 根据正则表达式生成随机字符串,适合生成特定格式的数据,数据生成能力集中在字符串领域。

易用性

  • @faker-js/faker:

    @faker-js/faker 提供直观易用的 API,文档完善,支持快速上手和自定义数据生成。

  • casual:

    casual 设计简单,易于使用,特别是对于快速生成随机数据非常方便。

  • chance:

    chance 提供清晰的 API 和文档,易于理解和使用,特别是在需要生成多种类型数据时。

  • lorem-ipsum:

    lorem-ipsum API 简单明了,易于使用,特别是在需要快速生成占位符文本时。

  • mockjs:

    mockjs 提供灵活的 API 进行数据模拟,文档详细,适合前端开发者使用。

  • randexp:

    randexp API 简洁,特别是对于需要根据正则表达式生成数据的场景。

自定义能力

  • @faker-js/faker:

    @faker-js/faker 支持高度自定义,包括自定义数据生成器、国际化和主题,灵活性强。

  • casual:

    casual 支持自定义数据生成器,易于扩展,但自定义能力相对有限。

  • chance:

    chance 支持自定义数据生成器和算法,灵活性高,特别是在需要生成特定类型数据时。

  • lorem-ipsum:

    lorem-ipsum 支持自定义生成文本的数量,但自定义能力较为简单。

  • mockjs:

    mockjs 支持根据自定义规则生成数据,灵活性高,特别是在定义复杂数据结构时。

  • randexp:

    randexp 支持根据自定义正则表达式生成数据,适合需要特定格式的自定义。

示例代码

  • @faker-js/faker:

    使用 @faker-js/faker 生成随机用户数据

    import { faker } from '@faker-js/faker';
    
    const randomUser = {
      name: faker.name.findName(),
      email: faker.internet.email(),
      address: faker.address.streetAddress(),
      phone: faker.phone.phoneNumber(),
    };
    
    console.log(randomUser);
    
  • casual:

    使用 casual 生成随机数据

    const casual = require('casual');
    
    const randomData = {
      name: casual.name,
      email: casual.email,
      city: casual.city,
      age: casual.age,
    };
    
    console.log(randomData);
    
  • chance:

    使用 chance 生成随机数据

    const Chance = require('chance');
    const chance = new Chance();
    
    const randomData = {
      name: chance.name(),
      email: chance.email(),
      address: chance.address(),
      phone: chance.phone(),
    };
    
    console.log(randomData);
    
  • lorem-ipsum:

    使用 lorem-ipsum 生成占位符文本

    const { LoremIpsum } = require('lorem-ipsum');
    const lorem = new LoremIpsum();
    
    const placeholderText = lorem.generateParagraphs(2);
    console.log(placeholderText);
    
  • mockjs:

    使用 mockjs 生成模拟数据

    const Mock = require('mockjs');
    
    const randomData = Mock.mock({
      'name': '@name',
      'age|18-60': 1,
      'email': '@email',
      'address': '@city',
    });
    
    console.log(randomData);
    
  • randexp:

    使用 randexp 根据正则表达式生成随机字符串

    const RandExp = require('randexp');
    const randomString = new RandExp(/[a-z]{5}/).gen();
    console.log(randomString);
    

如何选择: @faker-js/faker vs casual vs chance vs lorem-ipsum vs mockjs vs randexp

  • @faker-js/faker:

    选择 @faker-js/faker 如果你需要一个功能全面、支持国际化的虚拟数据生成器,适合生成各种类型的随机数据,且易于自定义和扩展。

  • casual:

    选择 casual 如果你需要一个简单、轻量级的随机数据生成器,易于使用且支持自定义数据类型,适合快速生成少量随机数据。

  • chance:

    选择 chance 如果你需要一个功能丰富、支持多种数据类型的随机数据生成器,特别是在需要高质量随机数据和多样化数据类型时。

  • lorem-ipsum:

    选择 lorem-ipsum 如果你只需要生成占位符文本,特别是拉丁文文本,适合设计、排版和内容填充。

  • mockjs:

    选择 mockjs 如果你需要根据定义的规则生成复杂的模拟数据,特别是在前端开发中需要模拟 API 响应时。

  • randexp:

    选择 randexp 如果你需要根据正则表达式生成特定格式的随机字符串,适合需要精确控制数据格式的场景。

@faker-js/faker的README

Faker

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

npm version npm downloads Continuous Integration codecov Chat on Discord Open Collective sponsor

⚡️ Try it Online

Open in StackBlitz

📙 API Documentation

🚀 Features

  • 🧍 Person - Generate Names, Genders, Bios, Job titles, and more.
  • 📍 Location - Generate Addresses, Zip Codes, Street Names, States, and Countries!
  • ⏰ Date - Past, present, future, recent, soon... whenever!
  • 💸 Finance - Create stubbed out Account Details, Transactions, and Crypto Addresses.
  • 👠 Commerce - Generate Prices, Product Names, Adjectives, and Descriptions.
  • 👾 Hacker - “Try to reboot the SQL bus, maybe it will bypass the virtual application!”
  • 🔢 Number and String - Of course, we can also generate random numbers and strings.
  • 🌏 Localization - Pick from over 70 locales to generate realistic looking Names, Addresses, and Phone Numbers.

Note: Faker tries to generate realistic data and not obvious fake data. The generated names, addresses, emails, phone numbers, and/or other data might be coincidentally valid information. Please do not send any of your messages/calls to them from your test setup.

📦 Install

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

🪄 Usage

// ESM
import { faker } from '@faker-js/faker';

// CJS
const { faker } = require('@faker-js/faker');

export function createRandomUser() {
  return {
    userId: faker.string.uuid(),
    username: faker.internet.username(),
    email: faker.internet.email(),
    avatar: faker.image.avatar(),
    password: faker.internet.password(),
    birthdate: faker.date.birthdate(),
    registeredAt: faker.date.past(),
  };
}

export const users = faker.helpers.multiple(createRandomUser, {
  count: 5,
});

💎 Modules

An in-depth overview of the API methods is available in the documentation for v10 (stable) and v10.* (next).

Templates

Faker contains a generator method faker.helpers.fake for combining faker API methods using a mustache string format.

console.log(
  faker.helpers.fake(
    'Hello {{person.prefix}} {{person.lastName}}, how are you today?'
  )
);

🌏 Localization

Faker has support for multiple locales.

The main faker instance uses the English locale. But you can also import instances using other locales.

// ESM
import { fakerDE as faker } from '@faker-js/faker';

// CJS
const { fakerDE: faker } = require('@faker-js/faker');

See our documentation for a list of provided languages.

Please note: Not every locale provides data for every module. In our pre-made faker instances, we fall back to English in such a case as this is the most complete and most commonly used language. If you don't want that or prefer a different fallback, you can also build your own instances.

import { de, de_CH, Faker } from '@faker-js/faker';

export const faker = new Faker({
  locale: [de_CH, de],
});

⚙️ Setting a randomness seed

If you want consistent results, you can set your own seed. If you are using faker.date methods, there are additional considerations. See Reproducible Results.

faker.seed(123);

const firstRandom = faker.number.int();

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

const secondRandom = faker.number.int();

console.log(firstRandom === secondRandom);

🤝 Sponsors

Faker is an MIT-licensed open source project with its ongoing development made possible entirely by the support of these awesome backers

Sponsors

Backers

✨ Contributing

Please make sure to read the Contributing Guide before making a pull request.

📘 Credits

Thanks to all the people who already contributed to Faker!

The fakerjs.dev website is generously hosted by Netlify, with search functionality powered by Algolia.

📝 Changelog

Detailed changes for each release are documented in the release notes.

📜 What happened to the original faker.js?

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

🔑 License

MIT