我创建了一个函数,使用lodash检查userRequest
数组中的所有元素是否都在whitelist
数组中。
实施是:
var isValidRequest = function (whitelist, userRequest) {
return _.isArray(userRequest) && _.all(userRequest, _.curry(_.contains)(whitelist));
};
并进行了一些测试以证明功能正常工作:
describe('#isValidRequest', function () {
it('should return true if all elements in userRequest are in whitelist', function () {
var whitelist = ['a', 'b', 'c'];
var userRequest = ['a', 'c'];
isValidRequest(whitelist, userRequest).should.eql(true);
});
it('should return false if any element in userRequest are not in whitelist', function () {
var whitelist = ['a', 'b', 'c'];
var userRequest = ['a', 'd'];
isValidRequest(whitelist, userRequest).should.eql(false);
});
}
这两项测试通过了。但是当我使用真实数据添加测试时,它失败了:
it('should return true if all elements in userRequest are in whitelist (with real data)', function () {
var whitelist = [
'88096706899774400',
'88096698426400385',
'88354827595032721',
'88770059167850369',
'88770060663799793',
'88770086312254177',
'88775479126833601',
'88810673591548449',
'88911136544698224',
'88911143320755648',
'89018389627510081',
'89091351154758240',
'89138464836818065',
'89183114982564704',
'89244113023388657',
'89295369695148528',
'89301959129114097',
'89504545841807728',
'89534114665094448',
'89606610296086240',
'89641336900131600',
'89731870992552785',
'89738745919815345',
'89823303352910497',
'89840086806894128',
'89845003632280944',
'89848539122007344',
'89880345515638625',
'89896204194964448',
'89969897520284480',
'90010241312333393',
'90031501817339552',
'90037811665186016',
'90127246523802417',
'90127326629435841',
'90135603990132225',
'90163753341695809',
'90172844493692480',
'90173968277693249',
'90207288890460865',
'90246427557475376',
'90252073971668320',
'90293572470161952',
'90321398525152736',
'90327469987102145',
'90332817802308640',
'90399158195724752',
'90473400215028352',
'90513809810131312',
'90598738792526944',
'90599866499311776',
'90688910073135297',
'90729048447052800',
'90791709927640224',
'90797430150059264',
'90836502447169952',
'90840789211604048',
'90910679553135265',
'90955553467111249',
'91005194750034464',
'91107097130270400',
'91120121727188609',
'91250514358592672',
'91329569249288769',
'91361847043080400',
'89329608853128288',
'89364936821344896',
'89839796561904929',
'89958125601566961',
'89997339418747777',
'90009609130378257',
'90059530916627969',
'90258899387578336',
'90360966983802448',
'90576680708221921',
'90949316113004816',
'90972311574832433',
'91080503304847328',
'91353087352736736',
'91437504999287200',
'89222620849510145',
'91005234554242641',
'88701724462867649',
'88786970344150112',
'89981796966546737',
'91402887254828368',
'89409996620502544',
'91210948485412928',
'89018361177929809',
'90724949851032945',
'90829728879723920',
'91193135505078640',
'90019861140866640',
'89538475013487072',
'89860038384233984',
'91210548905645393',
'89308012820785888',
'89617667142643760',
'89607245310211489',
'90757935350815969',
'89860541244171121',
'90892725971526912',
'90886830738859280',
'90853461057266320',
'90806805288251792',
'90472888904266304',
'89824038902302577',
'90135219888606641',
'91029359883680448',
'90434994851785744',
'90333231764534192',
'90529509422642336',
'90529639828432736',
'91357318422536304'
];
var userRequest = [
'88096698426400385',
'88096706899774400',
'88354827595032721',
'88770059167850369',
'88770060663799793',
'88770086312254177',
'88775479126833601',
'88810673591548449',
'88911136544698224',
'88911143320755648',
'89018389627510081',
'89091351154758240',
'89138464836818065',
'89183114982564704',
'89244113023388657',
'89295369695148528',
'89301959129114097',
'89504545841807728',
'89534114665094448',
'89606610296086240',
'89641336900131600',
'89731870992552785',
'89738745919815345',
'89823303352910497',
'89840086806894128',
'89845003632280944',
'89848539122007344',
'89880345515638625',
'89896204194964448',
'89969897520284480',
'90010241312333393',
'90031501817339552',
'90037811665186016',
'90127246523802417',
'90127326629435841',
'90135603990132225',
'90163753341695809',
'90172844493692480',
'90173968277693249',
'90207288890460865',
'90246427557475376',
'90252073971668320',
'90293572470161952',
'90321398525152736',
'90327469987102145',
'90332817802308640',
'90399158195724752',
'90473400215028352',
'90513809810131312',
'90598738792526944',
'90599866499311776',
'90688910073135297',
'90729048447052800',
'90791709927640224',
'90797430150059264',
'90836502447169952',
'90840789211604048',
'90910679553135265',
'90955553467111249',
'91005194750034464',
'91107097130270400',
'91120121727188609',
'91250514358592672',
'91329569249288769',
'91361847043080400'
];
isValidRequest(whitelist, userRequest).should.eql(true);
});
数据量已增加(whitelist
:3 - > 114,userRequest
:2 - > 65),我验证了userRequest
中的所有元素都在{{1} }。所以测试应该通过,但失败了。
我创建的功能完全相同但实现方式不同:
whitelist
使用这个新功能,所有测试都通过了。
在Chorme dev工具的控制台中,同样的事情发生了:
var isValidRequest2 = function (whitelist, userRequest) {
return _.isArray(userRequest) && _.all(userRequest, function(elem) { return _.contains(whitelist, elem); });
};
我认为_.curry
存在一些问题,但不确定。
如果有人使用> _.all(userRequest, _.curry(_.contains)(whitelist))
false
> _.all(userRequest, function(elem) { return _.contains(whitelist, elem); })
true
解释第一个函数的问题,那就太棒了。
仅供参考,我使用的是lodash v2.4.1。
答案 0 :(得分:2)
_。所有调用iterator
(第二个参数)以及其他参数
_.all([1, 2, 3], function () { console.log(arguments); return true; });
[1, 0, Array[3]]
[2, 1, Array[3]]
[3, 2, Array[3]]
true
_.contains
有一个可选的第三个参数fromIndex
,它与_.all
给出的索引相匹配。因此真正的数据中断。
尝试重新排序基本测试中的元素,你会看到事实。
isValidRequest(['a', 'b', 'c'], ['a', 'c']) // true
isValidRequest(['a', 'b', 'c'], ['c', 'a']) // false