Fake data的使用和產(chǎn)生 - JavaScript篇

接上篇Fake data文章 - Python篇吠架。本文主要介紹基于JavaScript的fake data generator尖殃。

API https://randomuser.me/

randomuser.me提供一個APIhttps://randomuser.me/api/來返回隨機用戶數(shù)據(jù),免費且十分方便好用。下面我們來看如何使用這個API宦焦。

多格式

簡單的一個GET請求https://randomuser.me/api/即可獲得包含用戶數(shù)據(jù)的response,默認格式是JSON鲤看,但通過?format=xxx參數(shù)我們可以指定不同的數(shù)據(jù)格式违柏,目前支持的有

  • JSON (default)
  • PrettyJSON or pretty
  • CSV
  • YAML
  • XML

結合chakram我們來看看這個API如何使用

var chakram = require('./node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

const randomuserURL = "https://randomuser.me/api/";

describe("try random user API in different format", function () {
  it("should return JSON", function () {
    return chakram.get(randomuserURL).then(function(response){
      console.log(response.body);
      expect(response).to.have.status(200);
    });
  });

  it("should return YAML", function () {
    return chakram.get(randomuserURL + "?format=yaml").then(function(response){
      console.log(response.body);
      expect(response).to.have.status(200);
    });
  });

  it("should return XML", function () {
    return chakram.get(randomuserURL + "?format=xml").then(function(response){
      console.log(response.body);
      expect(response).to.have.status(200);
    });
  });

});
多用戶

?results=5000參數(shù)可以一次性返回多個用戶數(shù)據(jù)。上限是5000证逻。

var chakram = require('./node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

const randomuserURL = "https://randomuser.me/api/";

describe("try random user API", function () {
  it("should two prettyJSON", function () {
    return chakram.get(randomuserURL + "?format=pretty&results=2").then(function(response){
      console.log(response.body);
      expect(response).to.have.status(200);
    });
  });

});
定制性別相關數(shù)據(jù)

?gender=male?gender=female可以返回數(shù)據(jù)去接近于male還是female

var chakram = require('./node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

const randomuserURL = "https://randomuser.me/api/";

describe("try random user API", function () {

  it("should return male", function () {
    return chakram.get(randomuserURL + "?gender=male").then(function(response){
      console.log(response.body);
      expect(response).to.have.status(200);
    });
  });

});

產(chǎn)生一樣的數(shù)據(jù)

?seed=danteyu可以讓每次執(zhí)行產(chǎn)生一致的數(shù)據(jù)乐埠。Seeds可以為任意字符串。比如下面的代碼重復執(zhí)行會產(chǎn)生一致的結果囚企。

var chakram = require('./node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

const randomuserURL = "https://randomuser.me/api/";

describe("try random user API", function () {

  it("should seed", function () {
    return chakram.get(randomuserURL + "?seed=danteyu").then(function(response){
      console.log(response.body);
      expect(response).to.have.status(200);
    });
  });

});
Locale

locale可以通過?nat=us丈咐。通過設置locale,可以返回更加真實的適用于不同國籍的數(shù)據(jù)龙宏。

var chakram = require('./node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

const randomuserURL = "https://randomuser.me/api/";

describe("try random user API", function () {

  it("should return CH", function () {
    return chakram.get(randomuserURL + "?nat=ch").then(function(response){
      console.log(response.body.results[0]);
      expect(response).to.have.status(200);
    });
  });
});

刷選特定數(shù)據(jù)

如果只是想要返回特定的幾個數(shù)據(jù)可以使用?inc=gender,name,nat來包含只需要返回的數(shù)據(jù)或是?exc=login來排除不想要的數(shù)據(jù)棵逊。

var chakram = require('./node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

const randomuserURL = "https://randomuser.me/api/";

describe("try random user API", function () {
  it("should return name and gender", function () {
    return chakram.get(randomuserURL + "?inc=name,gender").then(function(response){
      console.log(response.body);
      expect(response).to.have.status(200);
    });
  });

});

使用?noinfo可以只返回用戶數(shù)據(jù)而沒有seed,result和version等數(shù)據(jù)。

var chakram = require('./node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

const randomuserURL = "https://randomuser.me/api/";

describe("try random user API", function () {
  it("should return only user data", function () {
    return chakram.get(randomuserURL + "?noinfo").then(function(response){
      console.log(response.body.results);
      expect(response).to.have.status(200);
    });
  });

});

下面為輸出

> randomuser@1.0.0 test /Users/diyu/workspace/chakram
> mocha demo.js

  try random user API
[ { gender: 'male',
    name: { title: 'mr', first: 'ryan', last: 'mackay' },
    location:
     { street: '6297 richmond ave',
       city: 'stratford',
       state: 'new brunswick',
       postcode: 77478 },
    email: 'ryan.mackay@example.com',
    login:
     { username: 'heavywolf629',
       password: 'method',
       salt: 'EVZMskM7',
       md5: '6756ee051abaccca0214c997204d8d67',
       sha1: '4c265c506210e365c9e1acadcdc2321fe899d8c4',
       sha256: '90dfbb565010f9524be4531ec8fc712aad3fca69658976c8dc7615a84afa86b9' },
    dob: '1979-08-06 17:27:58',
    registered: '2002-04-02 00:35:37',
    phone: '448-153-9250',
    cell: '979-394-3188',
    id: { name: '', value: null },
    picture:
     { large: 'https://randomuser.me/api/portraits/men/62.jpg',
       medium: 'https://randomuser.me/api/portraits/med/men/62.jpg',
       thumbnail: 'https://randomuser.me/api/portraits/thumb/men/62.jpg' },
    nat: 'CA' } ]
    ? should return only user data (990ms)

  1 passing (999ms)

Faker

類似pythonfaker提供了各類各樣數(shù)據(jù)银酗。Faker的使用非常簡單辆影,直接引用調(diào)用就行了徒像。下面例子列舉了部分常用數(shù)據(jù)類型。

const faker = require('faker');

//address
console.log(faker.address.zipCode());
console.log(faker.address.city());
//commerce
console.log(faker.commerce.product());
console.log(faker.commerce.department());
//company
console.log(faker.company.companyName());
console.log(faker.company.bsBuzz());
//date
console.log(faker.date.future());
console.log(faker.date.month());
console.log(faker.date.weekday());
//finance
console.log(faker.finance.account());
console.log(faker.finance.transactionType());
//internet
console.log(faker.internet.email());
console.log(faker.internet.ip());
//lorem
console.log(faker.lorem.words());
console.log(faker.lorem.text());
//name
console.log(faker.name.findName());
console.log(faker.name.jobTitle());
//phone
console.log(faker.phone.phoneNumber());

Faker.fake()可以接受不同的API來組合字符串蛙讥。

const faker = require('faker');
console.log(faker.fake("{{company.companyName}}, {{internet.email}} {{phone.phoneNumber}}"))

同樣地锯蛀,faker也支持多個locale。通過下面代碼次慢,可以進行設置谬墙。

const faker = require('faker');
faker.locale = "zh_CN";

faker也支持seed()來產(chǎn)生一致的數(shù)據(jù),這樣的數(shù)據(jù)多用于單元測試等場景中经备。重復執(zhí)行下面的代碼會產(chǎn)生一致的結果拭抬。

const faker = require('faker');
faker.seed(123);
console.log(faker.company.companyName());

faker也有CLIJSON Schema對應的實現(xiàn)。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末侵蒙,一起剝皮案震驚了整個濱河市造虎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌纷闺,老刑警劉巖算凿,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異犁功,居然都是意外死亡氓轰,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門浸卦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來署鸡,“玉大人,你說我怎么就攤上這事限嫌⊙デ欤” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵怒医,是天一觀的道長炉抒。 經(jīng)常有香客問我,道長稚叹,這世上最難降的妖魔是什么焰薄? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮扒袖,結果婚禮上塞茅,老公的妹妹穿的比我還像新娘。我一直安慰自己僚稿,他們只是感情好凡桥,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蚀同,像睡著了一般缅刽。 火紅的嫁衣襯著肌膚如雪啊掏。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天衰猛,我揣著相機與錄音迟蜜,去河邊找鬼。 笑死啡省,一個胖子當著我的面吹牛娜睛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播卦睹,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼畦戒,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了结序?” 一聲冷哼從身側響起障斋,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎徐鹤,沒想到半個月后垃环,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡返敬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年遂庄,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片劲赠。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡涛目,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出经磅,到底是詐尸還是另有隱情泌绣,我是刑警寧澤钮追,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布预厌,位于F島的核電站,受9級特大地震影響元媚,放射性物質(zhì)發(fā)生泄漏轧叽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一刊棕、第九天 我趴在偏房一處隱蔽的房頂上張望炭晒。 院中可真熱鬧,春花似錦甥角、人聲如沸网严。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽震束。三九已至怜庸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間垢村,已是汗流浹背割疾。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留嘉栓,地道東北人宏榕。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像侵佃,于是被迫代替她去往敵國和親麻昼。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359