Finishing | Xiyan
Image source | Vision China
Listing | CSDN (ID: CSDNnews)
Recently, faker.js, a project that generates fake data on GitHub, has become popular, and it has climbed to second place on the Trending list. The number of stars currently exceeds 27.1k. It only takes a few simple steps to generate a lot of fake data in the browser and node.js.
It is understood that this is not a new project anymore. It was created in 2017 by an open source enthusiast named Marak Squires. He has been writing JavaScript product code since 1999 and started writing Node.js in 2009. In addition to faker.js, he has also participated in more than 100 NPM open source projects, including the independent function server project microcule, which is also popular on GitHub, and the microservice platform hook.io.
Because it is very useful in mock data, faker.js has been welcomed by many application developers once it was launched. Now that faker.js appears in front of our eyes again, let’s review this useful tool. Maybe it can help when creating database use case data.
One-click generation of name, home address, and vehicle information
As usual, put the GitHub address of the project first : https://github.com/marak/faker.js/
faker.js is a great nede module for mock data. Contains a super useful generator method Faker.fake, used to combine the beard string format with faker API.
such as:
console.log(faker.fake("{
{name.lastName}{
{name.firstName{
{name.suffix}}"));// outputs: "Marks, Dean Sr."
This operation can insert the values of name.lastName(), name.firstName() and name.suffix() into the format string.
Take a look at the demo first, and you can see that as long as you click "Generate New", you can generate a lot of information about "this person", including name, date of birth, home address, international, telephone, email and password, financial information, and even Photos, etc. But this person does not actually exist!
With fake images, fake faces, and fake videos, faker.js seems to have given the fake game a new idea. The original information data can also be generated in large quantities!
It looks amazing, are you eager to play it? In fact, creating a faker.js operation is not complicated, and interested friends may wish to refer to the GitHub open source code and try it out.
How to create faker.js
Browser
One of the benefits of Faker.js is that it can not only use JavaScript on the server side, but also JavaScript on the browser side. The code is as follows:
<script src = "faker.js" type = "text/javascript"></script> <script> var randomName = faker.name.findName(); // Caitlyn Kerluke var randomEmail = faker.internet.email(); // [email protected] var randomCard = faker.helpers.createCard(); // random contact card containing many properties</script>
Node.js
var faker = require('faker');
var randomName = faker.name.findName(); // Rowan Nikolaus var randomEmail = faker.internet.email(); // [email protected] var randomCard = faker.helpers.createCard(); // random contact card containing many properties
API
Faker.js has its own API and very rich functions.
-
JSDoc API Browser
http://marak.github.io/faker.js/
-
Declare interface
-
address
-
Postal code
State zip code
city
City prefix
City suffix
Street name
Street address
Street suffix
Street prefix
Other address
county
country
country code
State
State abbreviation
latitude
longitude
direction
Base direction
Sequence direction
GPS coordinates nearby
Time zone
business
-
colour
department
product name
price
Product adjectives
product material
product
product description
As well as API interfaces such as company, database, date, financial status, git, hacker, helper, image, network, music, name, etc., there are sub-projects under each project:
Faker.js can easily mock data, just add nmp dependency.
In addition, faker.js supports multiple languages. The default language is English, but you can set it yourself. The method is also very simple:
// sets locale to defaker.locale = "de";
the
With
cz
from
de_AT
de_CH
in
in the
en_AU_ocker
en_BORK
en_CA
en_GB
en_IE
en_IND
en_US
en_ZA
it is
es_MX
fa
...
Localization pack
faker.js supports incremental loading of locale.
By default, faker is required to include all locale data.
In a production environment, you may only want to use a set of data for a specific locale:
// loads only de localevar faker = require('faker/locale/de');
Set random seed
If you want consistent results, you can set the seed yourself:
faker.seed(123);
var firstRandom = faker.random.number();// Setting the seed again resets the sequence.faker.seed(123);
var secondRandom = faker.random.number();
console.log(firstRandom === secondRando
You can use gulp to automate the creation process. Each build operation is a separate task and can be run independently.
Browser binding
npm run browser
Create JSDocs
JSDOC v3 HTML API documentation
npm run jsdoc
Create ReadMe
The ReadMe.md file of faker.js is automatically generated and cannot be modified directly. If you want to modify, perform all the updates to ReadMe.md in ./build/src/docs.md, and then run the script.
npm run readme
The project sponsor stated that this project will generally be updated once a month, large or small version, and fix bugs as soon as possible according to the degree of urgency. Those interested in the project can continue to pay attention to the code update: https://github .com/marak/faker.js/
Finally, this method can generate a lot of fake data. Although the project sponsor stated that Node.js is mainly used by some organizations and individuals to set up the production environment, but for the sake of safety, I still need to remind and declare here that this article only uses For technical exchanges, friends look and cherish~
更多精彩推荐
☞全网销售额超 2.67 亿!德施曼连续 5 年蝉联双11全网智能锁销冠
☞苹果发布首款 Mac 自研芯片 M1,贯通生态快人一步
☞详解微软 ALUM:当语言模型遇到对抗训练
☞腾讯竟然是这样招人的,哈哈哈哈哈
☞AI 隐身术,能让物体在视频中消失的魔法
☞一文教你如何在生产环境中在Kubernetes上部署Jaeger
点分享点点赞点在看