lodash - 必要值檢查
Posted at 2016-04-13 lodash
lodash 是一個值得好好細細品嘗的模組,內含許多擴充的function去針對物件、字串、陣列處理。
必要值檢查
範例下載
12345678910111213141516171819202122232425262728293031323334353637383940
var should = require('chai').should();var _ = require('lodash');describe('require keys', function () { it('case 1:', function (done) { var requiredKeys = ['firstname', 'lastname', 'email'], params = { "firstname": "ailin", "lastname": "liou", "email": "NONE" }; var result = _.every(requiredKeys, _.partial(_.has, params)); result.should.be.equal(true); done(); }); it('case 2:', function (done) { var requiredKeys = ['firstname', 'lastname', 'email', 'phone'], params = { "firstname": "ailin", "lastname": "liou", "email": "NONE" }; var result = _.every(requiredKeys, _.partial(_.has, params)); result.should.be.equal(false); done(); }); it('case 3:', function (done) { var requiredKeys = ['firstname', 'email'], params = { "firstname": "ailin", "lastname": "liou", "email": "NONE" }; var result = _.every(requiredKeys, _.partial(_.has, params)); result.should.be.equal(true); done(); });});
Previous post: Buddy CI 與 PM2 的整合(踩雷) Next post: Vultr VPS