Reputation: 4149
I made a function that checks whether all elements in userRequest
array are in whitelist
array, using lodash.
The implementation is:
var isValidRequest = function (whitelist, userRequest) {
return _.isArray(userRequest) && _.all(userRequest, _.curry(_.contains)(whitelist));
};
And made some tests to prove the function work properly:
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);
});
}
Those two tests passed. But when I add a test using real-world data, it failed:
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);
});
Data size has increased(whitelist
: 3 -> 114, userRequest
: 2 -> 65), and I verified all elements in userRequest
are in whitelist
. So the test should pass, but failed.
I made a function that works exactly same but implementation is different:
var isValidRequest2 = function (whitelist, userRequest) {
return _.isArray(userRequest) && _.all(userRequest, function(elem) { return _.contains(whitelist, elem); });
};
With this new function, all tests passed.
In console of Chorme dev tools, same things happen:
> _.all(userRequest, _.curry(_.contains)(whitelist))
false
> _.all(userRequest, function(elem) { return _.contains(whitelist, elem); })
true
I think there's some problem with _.curry
, but not sure.
It'll be great if someone explain the problem of the first function using _.curry
.
FYI, I'm using lodash v2.4.1 .
Upvotes: 0
Views: 432
Reputation: 12133
_.all calls iterator
(the second parameter) with additional parameters
_.all([1, 2, 3], function () { console.log(arguments); return true; });
[1, 0, Array[3]]
[2, 1, Array[3]]
[3, 2, Array[3]]
true
_.contains
has an optional third parameter, fromIndex
, which matches index given by _.all
. Thus real data breaks.
Try to reorder elements in your basic tests, and you'll see the fact.
isValidRequest(['a', 'b', 'c'], ['a', 'c']) // true
isValidRequest(['a', 'b', 'c'], ['c', 'a']) // false
Upvotes: 2