Lodash - conforms 方法
語法
_.conforms(source)
建立一個函式來呼叫 source 的謂詞屬性以及給定物件中對應的屬性值,如果所有謂詞返回真,則返回真,否則返回假。
引數
source (物件) - 要符合的屬性謂詞的物件。
輸出
(函式) - 返回新的 spec 函式。
示例
var _ = require('lodash');
var check = _.cond([
[_.matches({ 'a': 1 }), _.constant('matches A')],
[_.conforms({ 'b': _.isNumber }), _.constant('matches B')],
[_.stubTrue, _.constant('no match')]
]);
console.log(check({ 'a': 1, 'b': 2 }));
console.log(check({ 'a': 0, 'b': 1 }));
console.log(check({ 'a': '1', 'b': '2' }));
將上述程式儲存在 tester.js 中。執行以下命令來執行此程式。
命令
\>node tester.js
輸出
matches A matches B no match
lodash_util.htm
廣告