gpt4 book ai didi

javascript - 使用 _.curry 的函数无法按预期工作

转载 作者:行者123 更新时间:2023-12-02 17:36:42 26 4
gpt4 key购买 nike

我创建了一个函数,使用 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 中的所有元素> 位于白名单中。所以测试应该通过,但失败了。

我创建了一个工作原理完全相同但实现不同的函数:

var isValidRequest2 = function (whitelist, userRequest) {
return _.isArray(userRequest) && _.all(userRequest, function(elem) { return _.contains(whitelist, elem); });
};

有了这个新功能,所有测试都通过了。

在 Chorme 开发工具的控制台中,也会发生同样的事情:

> _.all(userRequest, _.curry(_.contains)(whitelist))
false
> _.all(userRequest, function(elem) { return _.contains(whitelist, elem); })
true

我认为 _.curry 有问题,但不确定。

如果有人使用 _.curry 解释第一个函数的问题,那就太好了。

仅供引用,我正在使用 lodash v2.4.1 。

最佳答案

_.all 调用带有附加参数的迭代器(第二个参数)

_.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

关于javascript - 使用 _.curry 的函数无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22553333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com