index.js 402 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351
  1. /**
  2. * @license
  3. * lodash 3.10.1 (Custom Build) <https://lodash.com/>
  4. * Build: `lodash modern -d -o ./index.js`
  5. * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
  6. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  7. * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. * Available under MIT license <https://lodash.com/license>
  9. */
  10. ;(function() {
  11. /** Used as a safe reference for `undefined` in pre-ES5 environments. */
  12. var undefined;
  13. /** Used as the semantic version number. */
  14. var VERSION = '3.10.1';
  15. /** Used to compose bitmasks for wrapper metadata. */
  16. var BIND_FLAG = 1,
  17. BIND_KEY_FLAG = 2,
  18. CURRY_BOUND_FLAG = 4,
  19. CURRY_FLAG = 8,
  20. CURRY_RIGHT_FLAG = 16,
  21. PARTIAL_FLAG = 32,
  22. PARTIAL_RIGHT_FLAG = 64,
  23. ARY_FLAG = 128,
  24. REARG_FLAG = 256;
  25. /** Used as default options for `_.trunc`. */
  26. var DEFAULT_TRUNC_LENGTH = 30,
  27. DEFAULT_TRUNC_OMISSION = '...';
  28. /** Used to detect when a function becomes hot. */
  29. var HOT_COUNT = 150,
  30. HOT_SPAN = 16;
  31. /** Used as the size to enable large array optimizations. */
  32. var LARGE_ARRAY_SIZE = 200;
  33. /** Used to indicate the type of lazy iteratees. */
  34. var LAZY_FILTER_FLAG = 1,
  35. LAZY_MAP_FLAG = 2;
  36. /** Used as the `TypeError` message for "Functions" methods. */
  37. var FUNC_ERROR_TEXT = 'Expected a function';
  38. /** Used as the internal argument placeholder. */
  39. var PLACEHOLDER = '__lodash_placeholder__';
  40. /** `Object#toString` result references. */
  41. var argsTag = '[object Arguments]',
  42. arrayTag = '[object Array]',
  43. boolTag = '[object Boolean]',
  44. dateTag = '[object Date]',
  45. errorTag = '[object Error]',
  46. funcTag = '[object Function]',
  47. mapTag = '[object Map]',
  48. numberTag = '[object Number]',
  49. objectTag = '[object Object]',
  50. regexpTag = '[object RegExp]',
  51. setTag = '[object Set]',
  52. stringTag = '[object String]',
  53. weakMapTag = '[object WeakMap]';
  54. var arrayBufferTag = '[object ArrayBuffer]',
  55. float32Tag = '[object Float32Array]',
  56. float64Tag = '[object Float64Array]',
  57. int8Tag = '[object Int8Array]',
  58. int16Tag = '[object Int16Array]',
  59. int32Tag = '[object Int32Array]',
  60. uint8Tag = '[object Uint8Array]',
  61. uint8ClampedTag = '[object Uint8ClampedArray]',
  62. uint16Tag = '[object Uint16Array]',
  63. uint32Tag = '[object Uint32Array]';
  64. /** Used to match empty string literals in compiled template source. */
  65. var reEmptyStringLeading = /\b__p \+= '';/g,
  66. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  67. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  68. /** Used to match HTML entities and HTML characters. */
  69. var reEscapedHtml = /&(?:amp|lt|gt|quot|#39|#96);/g,
  70. reUnescapedHtml = /[&<>"'`]/g,
  71. reHasEscapedHtml = RegExp(reEscapedHtml.source),
  72. reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
  73. /** Used to match template delimiters. */
  74. var reEscape = /<%-([\s\S]+?)%>/g,
  75. reEvaluate = /<%([\s\S]+?)%>/g,
  76. reInterpolate = /<%=([\s\S]+?)%>/g;
  77. /** Used to match property names within property paths. */
  78. var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\n\\]|\\.)*?\1)\]/,
  79. reIsPlainProp = /^\w*$/,
  80. rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g;
  81. /**
  82. * Used to match `RegExp` [syntax characters](http://ecma-international.org/ecma-262/6.0/#sec-patterns)
  83. * and those outlined by [`EscapeRegExpPattern`](http://ecma-international.org/ecma-262/6.0/#sec-escaperegexppattern).
  84. */
  85. var reRegExpChars = /^[:!,]|[\\^$.*+?()[\]{}|\/]|(^[0-9a-fA-Fnrtuvx])|([\n\r\u2028\u2029])/g,
  86. reHasRegExpChars = RegExp(reRegExpChars.source);
  87. /** Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). */
  88. var reComboMark = /[\u0300-\u036f\ufe20-\ufe23]/g;
  89. /** Used to match backslashes in property paths. */
  90. var reEscapeChar = /\\(\\)?/g;
  91. /** Used to match [ES template delimiters](http://ecma-international.org/ecma-262/6.0/#sec-template-literal-lexical-components). */
  92. var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
  93. /** Used to match `RegExp` flags from their coerced string values. */
  94. var reFlags = /\w*$/;
  95. /** Used to detect hexadecimal string values. */
  96. var reHasHexPrefix = /^0[xX]/;
  97. /** Used to detect host constructors (Safari > 5). */
  98. var reIsHostCtor = /^\[object .+?Constructor\]$/;
  99. /** Used to detect unsigned integer values. */
  100. var reIsUint = /^\d+$/;
  101. /** Used to match latin-1 supplementary letters (excluding mathematical operators). */
  102. var reLatin1 = /[\xc0-\xd6\xd8-\xde\xdf-\xf6\xf8-\xff]/g;
  103. /** Used to ensure capturing order of template delimiters. */
  104. var reNoMatch = /($^)/;
  105. /** Used to match unescaped characters in compiled string literals. */
  106. var reUnescapedString = /['\n\r\u2028\u2029\\]/g;
  107. /** Used to match words to create compound words. */
  108. var reWords = (function() {
  109. var upper = '[A-Z\\xc0-\\xd6\\xd8-\\xde]',
  110. lower = '[a-z\\xdf-\\xf6\\xf8-\\xff]+';
  111. return RegExp(upper + '+(?=' + upper + lower + ')|' + upper + '?' + lower + '|' + upper + '+|[0-9]+', 'g');
  112. }());
  113. /** Used to assign default `context` object properties. */
  114. var contextProps = [
  115. 'Array', 'ArrayBuffer', 'Date', 'Error', 'Float32Array', 'Float64Array',
  116. 'Function', 'Int8Array', 'Int16Array', 'Int32Array', 'Math', 'Number',
  117. 'Object', 'RegExp', 'Set', 'String', '_', 'clearTimeout', 'isFinite',
  118. 'parseFloat', 'parseInt', 'setTimeout', 'TypeError', 'Uint8Array',
  119. 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array', 'WeakMap'
  120. ];
  121. /** Used to make template sourceURLs easier to identify. */
  122. var templateCounter = -1;
  123. /** Used to identify `toStringTag` values of typed arrays. */
  124. var typedArrayTags = {};
  125. typedArrayTags[float32Tag] = typedArrayTags[float64Tag] =
  126. typedArrayTags[int8Tag] = typedArrayTags[int16Tag] =
  127. typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] =
  128. typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] =
  129. typedArrayTags[uint32Tag] = true;
  130. typedArrayTags[argsTag] = typedArrayTags[arrayTag] =
  131. typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] =
  132. typedArrayTags[dateTag] = typedArrayTags[errorTag] =
  133. typedArrayTags[funcTag] = typedArrayTags[mapTag] =
  134. typedArrayTags[numberTag] = typedArrayTags[objectTag] =
  135. typedArrayTags[regexpTag] = typedArrayTags[setTag] =
  136. typedArrayTags[stringTag] = typedArrayTags[weakMapTag] = false;
  137. /** Used to identify `toStringTag` values supported by `_.clone`. */
  138. var cloneableTags = {};
  139. cloneableTags[argsTag] = cloneableTags[arrayTag] =
  140. cloneableTags[arrayBufferTag] = cloneableTags[boolTag] =
  141. cloneableTags[dateTag] = cloneableTags[float32Tag] =
  142. cloneableTags[float64Tag] = cloneableTags[int8Tag] =
  143. cloneableTags[int16Tag] = cloneableTags[int32Tag] =
  144. cloneableTags[numberTag] = cloneableTags[objectTag] =
  145. cloneableTags[regexpTag] = cloneableTags[stringTag] =
  146. cloneableTags[uint8Tag] = cloneableTags[uint8ClampedTag] =
  147. cloneableTags[uint16Tag] = cloneableTags[uint32Tag] = true;
  148. cloneableTags[errorTag] = cloneableTags[funcTag] =
  149. cloneableTags[mapTag] = cloneableTags[setTag] =
  150. cloneableTags[weakMapTag] = false;
  151. /** Used to map latin-1 supplementary letters to basic latin letters. */
  152. var deburredLetters = {
  153. '\xc0': 'A', '\xc1': 'A', '\xc2': 'A', '\xc3': 'A', '\xc4': 'A', '\xc5': 'A',
  154. '\xe0': 'a', '\xe1': 'a', '\xe2': 'a', '\xe3': 'a', '\xe4': 'a', '\xe5': 'a',
  155. '\xc7': 'C', '\xe7': 'c',
  156. '\xd0': 'D', '\xf0': 'd',
  157. '\xc8': 'E', '\xc9': 'E', '\xca': 'E', '\xcb': 'E',
  158. '\xe8': 'e', '\xe9': 'e', '\xea': 'e', '\xeb': 'e',
  159. '\xcC': 'I', '\xcd': 'I', '\xce': 'I', '\xcf': 'I',
  160. '\xeC': 'i', '\xed': 'i', '\xee': 'i', '\xef': 'i',
  161. '\xd1': 'N', '\xf1': 'n',
  162. '\xd2': 'O', '\xd3': 'O', '\xd4': 'O', '\xd5': 'O', '\xd6': 'O', '\xd8': 'O',
  163. '\xf2': 'o', '\xf3': 'o', '\xf4': 'o', '\xf5': 'o', '\xf6': 'o', '\xf8': 'o',
  164. '\xd9': 'U', '\xda': 'U', '\xdb': 'U', '\xdc': 'U',
  165. '\xf9': 'u', '\xfa': 'u', '\xfb': 'u', '\xfc': 'u',
  166. '\xdd': 'Y', '\xfd': 'y', '\xff': 'y',
  167. '\xc6': 'Ae', '\xe6': 'ae',
  168. '\xde': 'Th', '\xfe': 'th',
  169. '\xdf': 'ss'
  170. };
  171. /** Used to map characters to HTML entities. */
  172. var htmlEscapes = {
  173. '&': '&amp;',
  174. '<': '&lt;',
  175. '>': '&gt;',
  176. '"': '&quot;',
  177. "'": '&#39;',
  178. '`': '&#96;'
  179. };
  180. /** Used to map HTML entities to characters. */
  181. var htmlUnescapes = {
  182. '&amp;': '&',
  183. '&lt;': '<',
  184. '&gt;': '>',
  185. '&quot;': '"',
  186. '&#39;': "'",
  187. '&#96;': '`'
  188. };
  189. /** Used to determine if values are of the language type `Object`. */
  190. var objectTypes = {
  191. 'function': true,
  192. 'object': true
  193. };
  194. /** Used to escape characters for inclusion in compiled regexes. */
  195. var regexpEscapes = {
  196. '0': 'x30', '1': 'x31', '2': 'x32', '3': 'x33', '4': 'x34',
  197. '5': 'x35', '6': 'x36', '7': 'x37', '8': 'x38', '9': 'x39',
  198. 'A': 'x41', 'B': 'x42', 'C': 'x43', 'D': 'x44', 'E': 'x45', 'F': 'x46',
  199. 'a': 'x61', 'b': 'x62', 'c': 'x63', 'd': 'x64', 'e': 'x65', 'f': 'x66',
  200. 'n': 'x6e', 'r': 'x72', 't': 'x74', 'u': 'x75', 'v': 'x76', 'x': 'x78'
  201. };
  202. /** Used to escape characters for inclusion in compiled string literals. */
  203. var stringEscapes = {
  204. '\\': '\\',
  205. "'": "'",
  206. '\n': 'n',
  207. '\r': 'r',
  208. '\u2028': 'u2028',
  209. '\u2029': 'u2029'
  210. };
  211. /** Detect free variable `exports`. */
  212. var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
  213. /** Detect free variable `module`. */
  214. var freeModule = objectTypes[typeof module] && module && !module.nodeType && module;
  215. /** Detect free variable `global` from Node.js. */
  216. var freeGlobal = freeExports && freeModule && typeof global == 'object' && global && global.Object && global;
  217. /** Detect free variable `self`. */
  218. var freeSelf = objectTypes[typeof self] && self && self.Object && self;
  219. /** Detect free variable `window`. */
  220. var freeWindow = objectTypes[typeof window] && window && window.Object && window;
  221. /** Detect the popular CommonJS extension `module.exports`. */
  222. var moduleExports = freeModule && freeModule.exports === freeExports && freeExports;
  223. /**
  224. * Used as a reference to the global object.
  225. *
  226. * The `this` value is used if it's the global object to avoid Greasemonkey's
  227. * restricted `window` object, otherwise the `window` object is used.
  228. */
  229. var root = freeGlobal || ((freeWindow !== (this && this.window)) && freeWindow) || freeSelf || this;
  230. /*--------------------------------------------------------------------------*/
  231. /**
  232. * The base implementation of `compareAscending` which compares values and
  233. * sorts them in ascending order without guaranteeing a stable sort.
  234. *
  235. * @private
  236. * @param {*} value The value to compare.
  237. * @param {*} other The other value to compare.
  238. * @returns {number} Returns the sort order indicator for `value`.
  239. */
  240. function baseCompareAscending(value, other) {
  241. if (value !== other) {
  242. var valIsNull = value === null,
  243. valIsUndef = value === undefined,
  244. valIsReflexive = value === value;
  245. var othIsNull = other === null,
  246. othIsUndef = other === undefined,
  247. othIsReflexive = other === other;
  248. if ((value > other && !othIsNull) || !valIsReflexive ||
  249. (valIsNull && !othIsUndef && othIsReflexive) ||
  250. (valIsUndef && othIsReflexive)) {
  251. return 1;
  252. }
  253. if ((value < other && !valIsNull) || !othIsReflexive ||
  254. (othIsNull && !valIsUndef && valIsReflexive) ||
  255. (othIsUndef && valIsReflexive)) {
  256. return -1;
  257. }
  258. }
  259. return 0;
  260. }
  261. /**
  262. * The base implementation of `_.findIndex` and `_.findLastIndex` without
  263. * support for callback shorthands and `this` binding.
  264. *
  265. * @private
  266. * @param {Array} array The array to search.
  267. * @param {Function} predicate The function invoked per iteration.
  268. * @param {boolean} [fromRight] Specify iterating from right to left.
  269. * @returns {number} Returns the index of the matched value, else `-1`.
  270. */
  271. function baseFindIndex(array, predicate, fromRight) {
  272. var length = array.length,
  273. index = fromRight ? length : -1;
  274. while ((fromRight ? index-- : ++index < length)) {
  275. if (predicate(array[index], index, array)) {
  276. return index;
  277. }
  278. }
  279. return -1;
  280. }
  281. /**
  282. * The base implementation of `_.indexOf` without support for binary searches.
  283. *
  284. * @private
  285. * @param {Array} array The array to search.
  286. * @param {*} value The value to search for.
  287. * @param {number} fromIndex The index to search from.
  288. * @returns {number} Returns the index of the matched value, else `-1`.
  289. */
  290. function baseIndexOf(array, value, fromIndex) {
  291. if (value !== value) {
  292. return indexOfNaN(array, fromIndex);
  293. }
  294. var index = fromIndex - 1,
  295. length = array.length;
  296. while (++index < length) {
  297. if (array[index] === value) {
  298. return index;
  299. }
  300. }
  301. return -1;
  302. }
  303. /**
  304. * The base implementation of `_.isFunction` without support for environments
  305. * with incorrect `typeof` results.
  306. *
  307. * @private
  308. * @param {*} value The value to check.
  309. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  310. */
  311. function baseIsFunction(value) {
  312. // Avoid a Chakra JIT bug in compatibility modes of IE 11.
  313. // See https://github.com/jashkenas/underscore/issues/1621 for more details.
  314. return typeof value == 'function' || false;
  315. }
  316. /**
  317. * Converts `value` to a string if it's not one. An empty string is returned
  318. * for `null` or `undefined` values.
  319. *
  320. * @private
  321. * @param {*} value The value to process.
  322. * @returns {string} Returns the string.
  323. */
  324. function baseToString(value) {
  325. return value == null ? '' : (value + '');
  326. }
  327. /**
  328. * Used by `_.trim` and `_.trimLeft` to get the index of the first character
  329. * of `string` that is not found in `chars`.
  330. *
  331. * @private
  332. * @param {string} string The string to inspect.
  333. * @param {string} chars The characters to find.
  334. * @returns {number} Returns the index of the first character not found in `chars`.
  335. */
  336. function charsLeftIndex(string, chars) {
  337. var index = -1,
  338. length = string.length;
  339. while (++index < length && chars.indexOf(string.charAt(index)) > -1) {}
  340. return index;
  341. }
  342. /**
  343. * Used by `_.trim` and `_.trimRight` to get the index of the last character
  344. * of `string` that is not found in `chars`.
  345. *
  346. * @private
  347. * @param {string} string The string to inspect.
  348. * @param {string} chars The characters to find.
  349. * @returns {number} Returns the index of the last character not found in `chars`.
  350. */
  351. function charsRightIndex(string, chars) {
  352. var index = string.length;
  353. while (index-- && chars.indexOf(string.charAt(index)) > -1) {}
  354. return index;
  355. }
  356. /**
  357. * Used by `_.sortBy` to compare transformed elements of a collection and stable
  358. * sort them in ascending order.
  359. *
  360. * @private
  361. * @param {Object} object The object to compare.
  362. * @param {Object} other The other object to compare.
  363. * @returns {number} Returns the sort order indicator for `object`.
  364. */
  365. function compareAscending(object, other) {
  366. return baseCompareAscending(object.criteria, other.criteria) || (object.index - other.index);
  367. }
  368. /**
  369. * Used by `_.sortByOrder` to compare multiple properties of a value to another
  370. * and stable sort them.
  371. *
  372. * If `orders` is unspecified, all valuess are sorted in ascending order. Otherwise,
  373. * a value is sorted in ascending order if its corresponding order is "asc", and
  374. * descending if "desc".
  375. *
  376. * @private
  377. * @param {Object} object The object to compare.
  378. * @param {Object} other The other object to compare.
  379. * @param {boolean[]} orders The order to sort by for each property.
  380. * @returns {number} Returns the sort order indicator for `object`.
  381. */
  382. function compareMultiple(object, other, orders) {
  383. var index = -1,
  384. objCriteria = object.criteria,
  385. othCriteria = other.criteria,
  386. length = objCriteria.length,
  387. ordersLength = orders.length;
  388. while (++index < length) {
  389. var result = baseCompareAscending(objCriteria[index], othCriteria[index]);
  390. if (result) {
  391. if (index >= ordersLength) {
  392. return result;
  393. }
  394. var order = orders[index];
  395. return result * ((order === 'asc' || order === true) ? 1 : -1);
  396. }
  397. }
  398. // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
  399. // that causes it, under certain circumstances, to provide the same value for
  400. // `object` and `other`. See https://github.com/jashkenas/underscore/pull/1247
  401. // for more details.
  402. //
  403. // This also ensures a stable sort in V8 and other engines.
  404. // See https://code.google.com/p/v8/issues/detail?id=90 for more details.
  405. return object.index - other.index;
  406. }
  407. /**
  408. * Used by `_.deburr` to convert latin-1 supplementary letters to basic latin letters.
  409. *
  410. * @private
  411. * @param {string} letter The matched letter to deburr.
  412. * @returns {string} Returns the deburred letter.
  413. */
  414. function deburrLetter(letter) {
  415. return deburredLetters[letter];
  416. }
  417. /**
  418. * Used by `_.escape` to convert characters to HTML entities.
  419. *
  420. * @private
  421. * @param {string} chr The matched character to escape.
  422. * @returns {string} Returns the escaped character.
  423. */
  424. function escapeHtmlChar(chr) {
  425. return htmlEscapes[chr];
  426. }
  427. /**
  428. * Used by `_.escapeRegExp` to escape characters for inclusion in compiled regexes.
  429. *
  430. * @private
  431. * @param {string} chr The matched character to escape.
  432. * @param {string} leadingChar The capture group for a leading character.
  433. * @param {string} whitespaceChar The capture group for a whitespace character.
  434. * @returns {string} Returns the escaped character.
  435. */
  436. function escapeRegExpChar(chr, leadingChar, whitespaceChar) {
  437. if (leadingChar) {
  438. chr = regexpEscapes[chr];
  439. } else if (whitespaceChar) {
  440. chr = stringEscapes[chr];
  441. }
  442. return '\\' + chr;
  443. }
  444. /**
  445. * Used by `_.template` to escape characters for inclusion in compiled string literals.
  446. *
  447. * @private
  448. * @param {string} chr The matched character to escape.
  449. * @returns {string} Returns the escaped character.
  450. */
  451. function escapeStringChar(chr) {
  452. return '\\' + stringEscapes[chr];
  453. }
  454. /**
  455. * Gets the index at which the first occurrence of `NaN` is found in `array`.
  456. *
  457. * @private
  458. * @param {Array} array The array to search.
  459. * @param {number} fromIndex The index to search from.
  460. * @param {boolean} [fromRight] Specify iterating from right to left.
  461. * @returns {number} Returns the index of the matched `NaN`, else `-1`.
  462. */
  463. function indexOfNaN(array, fromIndex, fromRight) {
  464. var length = array.length,
  465. index = fromIndex + (fromRight ? 0 : -1);
  466. while ((fromRight ? index-- : ++index < length)) {
  467. var other = array[index];
  468. if (other !== other) {
  469. return index;
  470. }
  471. }
  472. return -1;
  473. }
  474. /**
  475. * Checks if `value` is object-like.
  476. *
  477. * @private
  478. * @param {*} value The value to check.
  479. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  480. */
  481. function isObjectLike(value) {
  482. return !!value && typeof value == 'object';
  483. }
  484. /**
  485. * Used by `trimmedLeftIndex` and `trimmedRightIndex` to determine if a
  486. * character code is whitespace.
  487. *
  488. * @private
  489. * @param {number} charCode The character code to inspect.
  490. * @returns {boolean} Returns `true` if `charCode` is whitespace, else `false`.
  491. */
  492. function isSpace(charCode) {
  493. return ((charCode <= 160 && (charCode >= 9 && charCode <= 13) || charCode == 32 || charCode == 160) || charCode == 5760 || charCode == 6158 ||
  494. (charCode >= 8192 && (charCode <= 8202 || charCode == 8232 || charCode == 8233 || charCode == 8239 || charCode == 8287 || charCode == 12288 || charCode == 65279)));
  495. }
  496. /**
  497. * Replaces all `placeholder` elements in `array` with an internal placeholder
  498. * and returns an array of their indexes.
  499. *
  500. * @private
  501. * @param {Array} array The array to modify.
  502. * @param {*} placeholder The placeholder to replace.
  503. * @returns {Array} Returns the new array of placeholder indexes.
  504. */
  505. function replaceHolders(array, placeholder) {
  506. var index = -1,
  507. length = array.length,
  508. resIndex = -1,
  509. result = [];
  510. while (++index < length) {
  511. if (array[index] === placeholder) {
  512. array[index] = PLACEHOLDER;
  513. result[++resIndex] = index;
  514. }
  515. }
  516. return result;
  517. }
  518. /**
  519. * An implementation of `_.uniq` optimized for sorted arrays without support
  520. * for callback shorthands and `this` binding.
  521. *
  522. * @private
  523. * @param {Array} array The array to inspect.
  524. * @param {Function} [iteratee] The function invoked per iteration.
  525. * @returns {Array} Returns the new duplicate-value-free array.
  526. */
  527. function sortedUniq(array, iteratee) {
  528. var seen,
  529. index = -1,
  530. length = array.length,
  531. resIndex = -1,
  532. result = [];
  533. while (++index < length) {
  534. var value = array[index],
  535. computed = iteratee ? iteratee(value, index, array) : value;
  536. if (!index || seen !== computed) {
  537. seen = computed;
  538. result[++resIndex] = value;
  539. }
  540. }
  541. return result;
  542. }
  543. /**
  544. * Used by `_.trim` and `_.trimLeft` to get the index of the first non-whitespace
  545. * character of `string`.
  546. *
  547. * @private
  548. * @param {string} string The string to inspect.
  549. * @returns {number} Returns the index of the first non-whitespace character.
  550. */
  551. function trimmedLeftIndex(string) {
  552. var index = -1,
  553. length = string.length;
  554. while (++index < length && isSpace(string.charCodeAt(index))) {}
  555. return index;
  556. }
  557. /**
  558. * Used by `_.trim` and `_.trimRight` to get the index of the last non-whitespace
  559. * character of `string`.
  560. *
  561. * @private
  562. * @param {string} string The string to inspect.
  563. * @returns {number} Returns the index of the last non-whitespace character.
  564. */
  565. function trimmedRightIndex(string) {
  566. var index = string.length;
  567. while (index-- && isSpace(string.charCodeAt(index))) {}
  568. return index;
  569. }
  570. /**
  571. * Used by `_.unescape` to convert HTML entities to characters.
  572. *
  573. * @private
  574. * @param {string} chr The matched character to unescape.
  575. * @returns {string} Returns the unescaped character.
  576. */
  577. function unescapeHtmlChar(chr) {
  578. return htmlUnescapes[chr];
  579. }
  580. /*--------------------------------------------------------------------------*/
  581. /**
  582. * Create a new pristine `lodash` function using the given `context` object.
  583. *
  584. * @static
  585. * @memberOf _
  586. * @category Utility
  587. * @param {Object} [context=root] The context object.
  588. * @returns {Function} Returns a new `lodash` function.
  589. * @example
  590. *
  591. * _.mixin({ 'foo': _.constant('foo') });
  592. *
  593. * var lodash = _.runInContext();
  594. * lodash.mixin({ 'bar': lodash.constant('bar') });
  595. *
  596. * _.isFunction(_.foo);
  597. * // => true
  598. * _.isFunction(_.bar);
  599. * // => false
  600. *
  601. * lodash.isFunction(lodash.foo);
  602. * // => false
  603. * lodash.isFunction(lodash.bar);
  604. * // => true
  605. *
  606. * // using `context` to mock `Date#getTime` use in `_.now`
  607. * var mock = _.runInContext({
  608. * 'Date': function() {
  609. * return { 'getTime': getTimeMock };
  610. * }
  611. * });
  612. *
  613. * // or creating a suped-up `defer` in Node.js
  614. * var defer = _.runInContext({ 'setTimeout': setImmediate }).defer;
  615. */
  616. function runInContext(context) {
  617. // Avoid issues with some ES3 environments that attempt to use values, named
  618. // after built-in constructors like `Object`, for the creation of literals.
  619. // ES5 clears this up by stating that literals must use built-in constructors.
  620. // See https://es5.github.io/#x11.1.5 for more details.
  621. context = context ? _.defaults(root.Object(), context, _.pick(root, contextProps)) : root;
  622. /** Native constructor references. */
  623. var Array = context.Array,
  624. Date = context.Date,
  625. Error = context.Error,
  626. Function = context.Function,
  627. Math = context.Math,
  628. Number = context.Number,
  629. Object = context.Object,
  630. RegExp = context.RegExp,
  631. String = context.String,
  632. TypeError = context.TypeError;
  633. /** Used for native method references. */
  634. var arrayProto = Array.prototype,
  635. objectProto = Object.prototype,
  636. stringProto = String.prototype;
  637. /** Used to resolve the decompiled source of functions. */
  638. var fnToString = Function.prototype.toString;
  639. /** Used to check objects for own properties. */
  640. var hasOwnProperty = objectProto.hasOwnProperty;
  641. /** Used to generate unique IDs. */
  642. var idCounter = 0;
  643. /**
  644. * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  645. * of values.
  646. */
  647. var objToString = objectProto.toString;
  648. /** Used to restore the original `_` reference in `_.noConflict`. */
  649. var oldDash = root._;
  650. /** Used to detect if a method is native. */
  651. var reIsNative = RegExp('^' +
  652. fnToString.call(hasOwnProperty).replace(/[\\^$.*+?()[\]{}|]/g, '\\$&')
  653. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
  654. );
  655. /** Native method references. */
  656. var ArrayBuffer = context.ArrayBuffer,
  657. clearTimeout = context.clearTimeout,
  658. parseFloat = context.parseFloat,
  659. pow = Math.pow,
  660. propertyIsEnumerable = objectProto.propertyIsEnumerable,
  661. Set = getNative(context, 'Set'),
  662. setTimeout = context.setTimeout,
  663. splice = arrayProto.splice,
  664. Uint8Array = context.Uint8Array,
  665. WeakMap = getNative(context, 'WeakMap');
  666. /* Native method references for those with the same name as other `lodash` methods. */
  667. var nativeCeil = Math.ceil,
  668. nativeCreate = getNative(Object, 'create'),
  669. nativeFloor = Math.floor,
  670. nativeIsArray = getNative(Array, 'isArray'),
  671. nativeIsFinite = context.isFinite,
  672. nativeKeys = getNative(Object, 'keys'),
  673. nativeMax = Math.max,
  674. nativeMin = Math.min,
  675. nativeNow = getNative(Date, 'now'),
  676. nativeParseInt = context.parseInt,
  677. nativeRandom = Math.random;
  678. /** Used as references for `-Infinity` and `Infinity`. */
  679. var NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY,
  680. POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
  681. /** Used as references for the maximum length and index of an array. */
  682. var MAX_ARRAY_LENGTH = 4294967295,
  683. MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1,
  684. HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1;
  685. /**
  686. * Used as the [maximum length](http://ecma-international.org/ecma-262/6.0/#sec-number.max_safe_integer)
  687. * of an array-like value.
  688. */
  689. var MAX_SAFE_INTEGER = 9007199254740991;
  690. /** Used to store function metadata. */
  691. var metaMap = WeakMap && new WeakMap;
  692. /** Used to lookup unminified function names. */
  693. var realNames = {};
  694. /*------------------------------------------------------------------------*/
  695. /**
  696. * Creates a `lodash` object which wraps `value` to enable implicit chaining.
  697. * Methods that operate on and return arrays, collections, and functions can
  698. * be chained together. Methods that retrieve a single value or may return a
  699. * primitive value will automatically end the chain returning the unwrapped
  700. * value. Explicit chaining may be enabled using `_.chain`. The execution of
  701. * chained methods is lazy, that is, execution is deferred until `_#value`
  702. * is implicitly or explicitly called.
  703. *
  704. * Lazy evaluation allows several methods to support shortcut fusion. Shortcut
  705. * fusion is an optimization strategy which merge iteratee calls; this can help
  706. * to avoid the creation of intermediate data structures and greatly reduce the
  707. * number of iteratee executions.
  708. *
  709. * Chaining is supported in custom builds as long as the `_#value` method is
  710. * directly or indirectly included in the build.
  711. *
  712. * In addition to lodash methods, wrappers have `Array` and `String` methods.
  713. *
  714. * The wrapper `Array` methods are:
  715. * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`,
  716. * `splice`, and `unshift`
  717. *
  718. * The wrapper `String` methods are:
  719. * `replace` and `split`
  720. *
  721. * The wrapper methods that support shortcut fusion are:
  722. * `compact`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`,
  723. * `first`, `initial`, `last`, `map`, `pluck`, `reject`, `rest`, `reverse`,
  724. * `slice`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `toArray`,
  725. * and `where`
  726. *
  727. * The chainable wrapper methods are:
  728. * `after`, `ary`, `assign`, `at`, `before`, `bind`, `bindAll`, `bindKey`,
  729. * `callback`, `chain`, `chunk`, `commit`, `compact`, `concat`, `constant`,
  730. * `countBy`, `create`, `curry`, `debounce`, `defaults`, `defaultsDeep`,
  731. * `defer`, `delay`, `difference`, `drop`, `dropRight`, `dropRightWhile`,
  732. * `dropWhile`, `fill`, `filter`, `flatten`, `flattenDeep`, `flow`, `flowRight`,
  733. * `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`,
  734. * `functions`, `groupBy`, `indexBy`, `initial`, `intersection`, `invert`,
  735. * `invoke`, `keys`, `keysIn`, `map`, `mapKeys`, `mapValues`, `matches`,
  736. * `matchesProperty`, `memoize`, `merge`, `method`, `methodOf`, `mixin`,
  737. * `modArgs`, `negate`, `omit`, `once`, `pairs`, `partial`, `partialRight`,
  738. * `partition`, `pick`, `plant`, `pluck`, `property`, `propertyOf`, `pull`,
  739. * `pullAt`, `push`, `range`, `rearg`, `reject`, `remove`, `rest`, `restParam`,
  740. * `reverse`, `set`, `shuffle`, `slice`, `sort`, `sortBy`, `sortByAll`,
  741. * `sortByOrder`, `splice`, `spread`, `take`, `takeRight`, `takeRightWhile`,
  742. * `takeWhile`, `tap`, `throttle`, `thru`, `times`, `toArray`, `toPlainObject`,
  743. * `transform`, `union`, `uniq`, `unshift`, `unzip`, `unzipWith`, `values`,
  744. * `valuesIn`, `where`, `without`, `wrap`, `xor`, `zip`, `zipObject`, `zipWith`
  745. *
  746. * The wrapper methods that are **not** chainable by default are:
  747. * `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clone`, `cloneDeep`,
  748. * `deburr`, `endsWith`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`,
  749. * `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `findWhere`, `first`,
  750. * `floor`, `get`, `gt`, `gte`, `has`, `identity`, `includes`, `indexOf`,
  751. * `inRange`, `isArguments`, `isArray`, `isBoolean`, `isDate`, `isElement`,
  752. * `isEmpty`, `isEqual`, `isError`, `isFinite` `isFunction`, `isMatch`,
  753. * `isNative`, `isNaN`, `isNull`, `isNumber`, `isObject`, `isPlainObject`,
  754. * `isRegExp`, `isString`, `isUndefined`, `isTypedArray`, `join`, `kebabCase`,
  755. * `last`, `lastIndexOf`, `lt`, `lte`, `max`, `min`, `noConflict`, `noop`,
  756. * `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, `random`, `reduce`,
  757. * `reduceRight`, `repeat`, `result`, `round`, `runInContext`, `shift`, `size`,
  758. * `snakeCase`, `some`, `sortedIndex`, `sortedLastIndex`, `startCase`,
  759. * `startsWith`, `sum`, `template`, `trim`, `trimLeft`, `trimRight`, `trunc`,
  760. * `unescape`, `uniqueId`, `value`, and `words`
  761. *
  762. * The wrapper method `sample` will return a wrapped value when `n` is provided,
  763. * otherwise an unwrapped value is returned.
  764. *
  765. * @name _
  766. * @constructor
  767. * @category Chain
  768. * @param {*} value The value to wrap in a `lodash` instance.
  769. * @returns {Object} Returns the new `lodash` wrapper instance.
  770. * @example
  771. *
  772. * var wrapped = _([1, 2, 3]);
  773. *
  774. * // returns an unwrapped value
  775. * wrapped.reduce(function(total, n) {
  776. * return total + n;
  777. * });
  778. * // => 6
  779. *
  780. * // returns a wrapped value
  781. * var squares = wrapped.map(function(n) {
  782. * return n * n;
  783. * });
  784. *
  785. * _.isArray(squares);
  786. * // => false
  787. *
  788. * _.isArray(squares.value());
  789. * // => true
  790. */
  791. function lodash(value) {
  792. if (isObjectLike(value) && !isArray(value) && !(value instanceof LazyWrapper)) {
  793. if (value instanceof LodashWrapper) {
  794. return value;
  795. }
  796. if (hasOwnProperty.call(value, '__chain__') && hasOwnProperty.call(value, '__wrapped__')) {
  797. return wrapperClone(value);
  798. }
  799. }
  800. return new LodashWrapper(value);
  801. }
  802. /**
  803. * The function whose prototype all chaining wrappers inherit from.
  804. *
  805. * @private
  806. */
  807. function baseLodash() {
  808. // No operation performed.
  809. }
  810. /**
  811. * The base constructor for creating `lodash` wrapper objects.
  812. *
  813. * @private
  814. * @param {*} value The value to wrap.
  815. * @param {boolean} [chainAll] Enable chaining for all wrapper methods.
  816. * @param {Array} [actions=[]] Actions to peform to resolve the unwrapped value.
  817. */
  818. function LodashWrapper(value, chainAll, actions) {
  819. this.__wrapped__ = value;
  820. this.__actions__ = actions || [];
  821. this.__chain__ = !!chainAll;
  822. }
  823. /**
  824. * An object environment feature flags.
  825. *
  826. * @static
  827. * @memberOf _
  828. * @type Object
  829. */
  830. var support = lodash.support = {};
  831. /**
  832. * By default, the template delimiters used by lodash are like those in
  833. * embedded Ruby (ERB). Change the following template settings to use
  834. * alternative delimiters.
  835. *
  836. * @static
  837. * @memberOf _
  838. * @type Object
  839. */
  840. lodash.templateSettings = {
  841. /**
  842. * Used to detect `data` property values to be HTML-escaped.
  843. *
  844. * @memberOf _.templateSettings
  845. * @type RegExp
  846. */
  847. 'escape': reEscape,
  848. /**
  849. * Used to detect code to be evaluated.
  850. *
  851. * @memberOf _.templateSettings
  852. * @type RegExp
  853. */
  854. 'evaluate': reEvaluate,
  855. /**
  856. * Used to detect `data` property values to inject.
  857. *
  858. * @memberOf _.templateSettings
  859. * @type RegExp
  860. */
  861. 'interpolate': reInterpolate,
  862. /**
  863. * Used to reference the data object in the template text.
  864. *
  865. * @memberOf _.templateSettings
  866. * @type string
  867. */
  868. 'variable': '',
  869. /**
  870. * Used to import variables into the compiled template.
  871. *
  872. * @memberOf _.templateSettings
  873. * @type Object
  874. */
  875. 'imports': {
  876. /**
  877. * A reference to the `lodash` function.
  878. *
  879. * @memberOf _.templateSettings.imports
  880. * @type Function
  881. */
  882. '_': lodash
  883. }
  884. };
  885. /*------------------------------------------------------------------------*/
  886. /**
  887. * Creates a lazy wrapper object which wraps `value` to enable lazy evaluation.
  888. *
  889. * @private
  890. * @param {*} value The value to wrap.
  891. */
  892. function LazyWrapper(value) {
  893. this.__wrapped__ = value;
  894. this.__actions__ = [];
  895. this.__dir__ = 1;
  896. this.__filtered__ = false;
  897. this.__iteratees__ = [];
  898. this.__takeCount__ = POSITIVE_INFINITY;
  899. this.__views__ = [];
  900. }
  901. /**
  902. * Creates a clone of the lazy wrapper object.
  903. *
  904. * @private
  905. * @name clone
  906. * @memberOf LazyWrapper
  907. * @returns {Object} Returns the cloned `LazyWrapper` object.
  908. */
  909. function lazyClone() {
  910. var result = new LazyWrapper(this.__wrapped__);
  911. result.__actions__ = arrayCopy(this.__actions__);
  912. result.__dir__ = this.__dir__;
  913. result.__filtered__ = this.__filtered__;
  914. result.__iteratees__ = arrayCopy(this.__iteratees__);
  915. result.__takeCount__ = this.__takeCount__;
  916. result.__views__ = arrayCopy(this.__views__);
  917. return result;
  918. }
  919. /**
  920. * Reverses the direction of lazy iteration.
  921. *
  922. * @private
  923. * @name reverse
  924. * @memberOf LazyWrapper
  925. * @returns {Object} Returns the new reversed `LazyWrapper` object.
  926. */
  927. function lazyReverse() {
  928. if (this.__filtered__) {
  929. var result = new LazyWrapper(this);
  930. result.__dir__ = -1;
  931. result.__filtered__ = true;
  932. } else {
  933. result = this.clone();
  934. result.__dir__ *= -1;
  935. }
  936. return result;
  937. }
  938. /**
  939. * Extracts the unwrapped value from its lazy wrapper.
  940. *
  941. * @private
  942. * @name value
  943. * @memberOf LazyWrapper
  944. * @returns {*} Returns the unwrapped value.
  945. */
  946. function lazyValue() {
  947. var array = this.__wrapped__.value(),
  948. dir = this.__dir__,
  949. isArr = isArray(array),
  950. isRight = dir < 0,
  951. arrLength = isArr ? array.length : 0,
  952. view = getView(0, arrLength, this.__views__),
  953. start = view.start,
  954. end = view.end,
  955. length = end - start,
  956. index = isRight ? end : (start - 1),
  957. iteratees = this.__iteratees__,
  958. iterLength = iteratees.length,
  959. resIndex = 0,
  960. takeCount = nativeMin(length, this.__takeCount__);
  961. if (!isArr || arrLength < LARGE_ARRAY_SIZE || (arrLength == length && takeCount == length)) {
  962. return baseWrapperValue((isRight && isArr) ? array.reverse() : array, this.__actions__);
  963. }
  964. var result = [];
  965. outer:
  966. while (length-- && resIndex < takeCount) {
  967. index += dir;
  968. var iterIndex = -1,
  969. value = array[index];
  970. while (++iterIndex < iterLength) {
  971. var data = iteratees[iterIndex],
  972. iteratee = data.iteratee,
  973. type = data.type,
  974. computed = iteratee(value);
  975. if (type == LAZY_MAP_FLAG) {
  976. value = computed;
  977. } else if (!computed) {
  978. if (type == LAZY_FILTER_FLAG) {
  979. continue outer;
  980. } else {
  981. break outer;
  982. }
  983. }
  984. }
  985. result[resIndex++] = value;
  986. }
  987. return result;
  988. }
  989. /*------------------------------------------------------------------------*/
  990. /**
  991. * Creates a cache object to store key/value pairs.
  992. *
  993. * @private
  994. * @static
  995. * @name Cache
  996. * @memberOf _.memoize
  997. */
  998. function MapCache() {
  999. this.__data__ = {};
  1000. }
  1001. /**
  1002. * Removes `key` and its value from the cache.
  1003. *
  1004. * @private
  1005. * @name delete
  1006. * @memberOf _.memoize.Cache
  1007. * @param {string} key The key of the value to remove.
  1008. * @returns {boolean} Returns `true` if the entry was removed successfully, else `false`.
  1009. */
  1010. function mapDelete(key) {
  1011. return this.has(key) && delete this.__data__[key];
  1012. }
  1013. /**
  1014. * Gets the cached value for `key`.
  1015. *
  1016. * @private
  1017. * @name get
  1018. * @memberOf _.memoize.Cache
  1019. * @param {string} key The key of the value to get.
  1020. * @returns {*} Returns the cached value.
  1021. */
  1022. function mapGet(key) {
  1023. return key == '__proto__' ? undefined : this.__data__[key];
  1024. }
  1025. /**
  1026. * Checks if a cached value for `key` exists.
  1027. *
  1028. * @private
  1029. * @name has
  1030. * @memberOf _.memoize.Cache
  1031. * @param {string} key The key of the entry to check.
  1032. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  1033. */
  1034. function mapHas(key) {
  1035. return key != '__proto__' && hasOwnProperty.call(this.__data__, key);
  1036. }
  1037. /**
  1038. * Sets `value` to `key` of the cache.
  1039. *
  1040. * @private
  1041. * @name set
  1042. * @memberOf _.memoize.Cache
  1043. * @param {string} key The key of the value to cache.
  1044. * @param {*} value The value to cache.
  1045. * @returns {Object} Returns the cache object.
  1046. */
  1047. function mapSet(key, value) {
  1048. if (key != '__proto__') {
  1049. this.__data__[key] = value;
  1050. }
  1051. return this;
  1052. }
  1053. /*------------------------------------------------------------------------*/
  1054. /**
  1055. *
  1056. * Creates a cache object to store unique values.
  1057. *
  1058. * @private
  1059. * @param {Array} [values] The values to cache.
  1060. */
  1061. function SetCache(values) {
  1062. var length = values ? values.length : 0;
  1063. this.data = { 'hash': nativeCreate(null), 'set': new Set };
  1064. while (length--) {
  1065. this.push(values[length]);
  1066. }
  1067. }
  1068. /**
  1069. * Checks if `value` is in `cache` mimicking the return signature of
  1070. * `_.indexOf` by returning `0` if the value is found, else `-1`.
  1071. *
  1072. * @private
  1073. * @param {Object} cache The cache to search.
  1074. * @param {*} value The value to search for.
  1075. * @returns {number} Returns `0` if `value` is found, else `-1`.
  1076. */
  1077. function cacheIndexOf(cache, value) {
  1078. var data = cache.data,
  1079. result = (typeof value == 'string' || isObject(value)) ? data.set.has(value) : data.hash[value];
  1080. return result ? 0 : -1;
  1081. }
  1082. /**
  1083. * Adds `value` to the cache.
  1084. *
  1085. * @private
  1086. * @name push
  1087. * @memberOf SetCache
  1088. * @param {*} value The value to cache.
  1089. */
  1090. function cachePush(value) {
  1091. var data = this.data;
  1092. if (typeof value == 'string' || isObject(value)) {
  1093. data.set.add(value);
  1094. } else {
  1095. data.hash[value] = true;
  1096. }
  1097. }
  1098. /*------------------------------------------------------------------------*/
  1099. /**
  1100. * Creates a new array joining `array` with `other`.
  1101. *
  1102. * @private
  1103. * @param {Array} array The array to join.
  1104. * @param {Array} other The other array to join.
  1105. * @returns {Array} Returns the new concatenated array.
  1106. */
  1107. function arrayConcat(array, other) {
  1108. var index = -1,
  1109. length = array.length,
  1110. othIndex = -1,
  1111. othLength = other.length,
  1112. result = Array(length + othLength);
  1113. while (++index < length) {
  1114. result[index] = array[index];
  1115. }
  1116. while (++othIndex < othLength) {
  1117. result[index++] = other[othIndex];
  1118. }
  1119. return result;
  1120. }
  1121. /**
  1122. * Copies the values of `source` to `array`.
  1123. *
  1124. * @private
  1125. * @param {Array} source The array to copy values from.
  1126. * @param {Array} [array=[]] The array to copy values to.
  1127. * @returns {Array} Returns `array`.
  1128. */
  1129. function arrayCopy(source, array) {
  1130. var index = -1,
  1131. length = source.length;
  1132. array || (array = Array(length));
  1133. while (++index < length) {
  1134. array[index] = source[index];
  1135. }
  1136. return array;
  1137. }
  1138. /**
  1139. * A specialized version of `_.forEach` for arrays without support for callback
  1140. * shorthands and `this` binding.
  1141. *
  1142. * @private
  1143. * @param {Array} array The array to iterate over.
  1144. * @param {Function} iteratee The function invoked per iteration.
  1145. * @returns {Array} Returns `array`.
  1146. */
  1147. function arrayEach(array, iteratee) {
  1148. var index = -1,
  1149. length = array.length;
  1150. while (++index < length) {
  1151. if (iteratee(array[index], index, array) === false) {
  1152. break;
  1153. }
  1154. }
  1155. return array;
  1156. }
  1157. /**
  1158. * A specialized version of `_.forEachRight` for arrays without support for
  1159. * callback shorthands and `this` binding.
  1160. *
  1161. * @private
  1162. * @param {Array} array The array to iterate over.
  1163. * @param {Function} iteratee The function invoked per iteration.
  1164. * @returns {Array} Returns `array`.
  1165. */
  1166. function arrayEachRight(array, iteratee) {
  1167. var length = array.length;
  1168. while (length--) {
  1169. if (iteratee(array[length], length, array) === false) {
  1170. break;
  1171. }
  1172. }
  1173. return array;
  1174. }
  1175. /**
  1176. * A specialized version of `_.every` for arrays without support for callback
  1177. * shorthands and `this` binding.
  1178. *
  1179. * @private
  1180. * @param {Array} array The array to iterate over.
  1181. * @param {Function} predicate The function invoked per iteration.
  1182. * @returns {boolean} Returns `true` if all elements pass the predicate check,
  1183. * else `false`.
  1184. */
  1185. function arrayEvery(array, predicate) {
  1186. var index = -1,
  1187. length = array.length;
  1188. while (++index < length) {
  1189. if (!predicate(array[index], index, array)) {
  1190. return false;
  1191. }
  1192. }
  1193. return true;
  1194. }
  1195. /**
  1196. * A specialized version of `baseExtremum` for arrays which invokes `iteratee`
  1197. * with one argument: (value).
  1198. *
  1199. * @private
  1200. * @param {Array} array The array to iterate over.
  1201. * @param {Function} iteratee The function invoked per iteration.
  1202. * @param {Function} comparator The function used to compare values.
  1203. * @param {*} exValue The initial extremum value.
  1204. * @returns {*} Returns the extremum value.
  1205. */
  1206. function arrayExtremum(array, iteratee, comparator, exValue) {
  1207. var index = -1,
  1208. length = array.length,
  1209. computed = exValue,
  1210. result = computed;
  1211. while (++index < length) {
  1212. var value = array[index],
  1213. current = +iteratee(value);
  1214. if (comparator(current, computed)) {
  1215. computed = current;
  1216. result = value;
  1217. }
  1218. }
  1219. return result;
  1220. }
  1221. /**
  1222. * A specialized version of `_.filter` for arrays without support for callback
  1223. * shorthands and `this` binding.
  1224. *
  1225. * @private
  1226. * @param {Array} array The array to iterate over.
  1227. * @param {Function} predicate The function invoked per iteration.
  1228. * @returns {Array} Returns the new filtered array.
  1229. */
  1230. function arrayFilter(array, predicate) {
  1231. var index = -1,
  1232. length = array.length,
  1233. resIndex = -1,
  1234. result = [];
  1235. while (++index < length) {
  1236. var value = array[index];
  1237. if (predicate(value, index, array)) {
  1238. result[++resIndex] = value;
  1239. }
  1240. }
  1241. return result;
  1242. }
  1243. /**
  1244. * A specialized version of `_.map` for arrays without support for callback
  1245. * shorthands and `this` binding.
  1246. *
  1247. * @private
  1248. * @param {Array} array The array to iterate over.
  1249. * @param {Function} iteratee The function invoked per iteration.
  1250. * @returns {Array} Returns the new mapped array.
  1251. */
  1252. function arrayMap(array, iteratee) {
  1253. var index = -1,
  1254. length = array.length,
  1255. result = Array(length);
  1256. while (++index < length) {
  1257. result[index] = iteratee(array[index], index, array);
  1258. }
  1259. return result;
  1260. }
  1261. /**
  1262. * Appends the elements of `values` to `array`.
  1263. *
  1264. * @private
  1265. * @param {Array} array The array to modify.
  1266. * @param {Array} values The values to append.
  1267. * @returns {Array} Returns `array`.
  1268. */
  1269. function arrayPush(array, values) {
  1270. var index = -1,
  1271. length = values.length,
  1272. offset = array.length;
  1273. while (++index < length) {
  1274. array[offset + index] = values[index];
  1275. }
  1276. return array;
  1277. }
  1278. /**
  1279. * A specialized version of `_.reduce` for arrays without support for callback
  1280. * shorthands and `this` binding.
  1281. *
  1282. * @private
  1283. * @param {Array} array The array to iterate over.
  1284. * @param {Function} iteratee The function invoked per iteration.
  1285. * @param {*} [accumulator] The initial value.
  1286. * @param {boolean} [initFromArray] Specify using the first element of `array`
  1287. * as the initial value.
  1288. * @returns {*} Returns the accumulated value.
  1289. */
  1290. function arrayReduce(array, iteratee, accumulator, initFromArray) {
  1291. var index = -1,
  1292. length = array.length;
  1293. if (initFromArray && length) {
  1294. accumulator = array[++index];
  1295. }
  1296. while (++index < length) {
  1297. accumulator = iteratee(accumulator, array[index], index, array);
  1298. }
  1299. return accumulator;
  1300. }
  1301. /**
  1302. * A specialized version of `_.reduceRight` for arrays without support for
  1303. * callback shorthands and `this` binding.
  1304. *
  1305. * @private
  1306. * @param {Array} array The array to iterate over.
  1307. * @param {Function} iteratee The function invoked per iteration.
  1308. * @param {*} [accumulator] The initial value.
  1309. * @param {boolean} [initFromArray] Specify using the last element of `array`
  1310. * as the initial value.
  1311. * @returns {*} Returns the accumulated value.
  1312. */
  1313. function arrayReduceRight(array, iteratee, accumulator, initFromArray) {
  1314. var length = array.length;
  1315. if (initFromArray && length) {
  1316. accumulator = array[--length];
  1317. }
  1318. while (length--) {
  1319. accumulator = iteratee(accumulator, array[length], length, array);
  1320. }
  1321. return accumulator;
  1322. }
  1323. /**
  1324. * A specialized version of `_.some` for arrays without support for callback
  1325. * shorthands and `this` binding.
  1326. *
  1327. * @private
  1328. * @param {Array} array The array to iterate over.
  1329. * @param {Function} predicate The function invoked per iteration.
  1330. * @returns {boolean} Returns `true` if any element passes the predicate check,
  1331. * else `false`.
  1332. */
  1333. function arraySome(array, predicate) {
  1334. var index = -1,
  1335. length = array.length;
  1336. while (++index < length) {
  1337. if (predicate(array[index], index, array)) {
  1338. return true;
  1339. }
  1340. }
  1341. return false;
  1342. }
  1343. /**
  1344. * A specialized version of `_.sum` for arrays without support for callback
  1345. * shorthands and `this` binding..
  1346. *
  1347. * @private
  1348. * @param {Array} array The array to iterate over.
  1349. * @param {Function} iteratee The function invoked per iteration.
  1350. * @returns {number} Returns the sum.
  1351. */
  1352. function arraySum(array, iteratee) {
  1353. var length = array.length,
  1354. result = 0;
  1355. while (length--) {
  1356. result += +iteratee(array[length]) || 0;
  1357. }
  1358. return result;
  1359. }
  1360. /**
  1361. * Used by `_.defaults` to customize its `_.assign` use.
  1362. *
  1363. * @private
  1364. * @param {*} objectValue The destination object property value.
  1365. * @param {*} sourceValue The source object property value.
  1366. * @returns {*} Returns the value to assign to the destination object.
  1367. */
  1368. function assignDefaults(objectValue, sourceValue) {
  1369. return objectValue === undefined ? sourceValue : objectValue;
  1370. }
  1371. /**
  1372. * Used by `_.template` to customize its `_.assign` use.
  1373. *
  1374. * **Note:** This function is like `assignDefaults` except that it ignores
  1375. * inherited property values when checking if a property is `undefined`.
  1376. *
  1377. * @private
  1378. * @param {*} objectValue The destination object property value.
  1379. * @param {*} sourceValue The source object property value.
  1380. * @param {string} key The key associated with the object and source values.
  1381. * @param {Object} object The destination object.
  1382. * @returns {*} Returns the value to assign to the destination object.
  1383. */
  1384. function assignOwnDefaults(objectValue, sourceValue, key, object) {
  1385. return (objectValue === undefined || !hasOwnProperty.call(object, key))
  1386. ? sourceValue
  1387. : objectValue;
  1388. }
  1389. /**
  1390. * A specialized version of `_.assign` for customizing assigned values without
  1391. * support for argument juggling, multiple sources, and `this` binding `customizer`
  1392. * functions.
  1393. *
  1394. * @private
  1395. * @param {Object} object The destination object.
  1396. * @param {Object} source The source object.
  1397. * @param {Function} customizer The function to customize assigned values.
  1398. * @returns {Object} Returns `object`.
  1399. */
  1400. function assignWith(object, source, customizer) {
  1401. var index = -1,
  1402. props = keys(source),
  1403. length = props.length;
  1404. while (++index < length) {
  1405. var key = props[index],
  1406. value = object[key],
  1407. result = customizer(value, source[key], key, object, source);
  1408. if ((result === result ? (result !== value) : (value === value)) ||
  1409. (value === undefined && !(key in object))) {
  1410. object[key] = result;
  1411. }
  1412. }
  1413. return object;
  1414. }
  1415. /**
  1416. * The base implementation of `_.assign` without support for argument juggling,
  1417. * multiple sources, and `customizer` functions.
  1418. *
  1419. * @private
  1420. * @param {Object} object The destination object.
  1421. * @param {Object} source The source object.
  1422. * @returns {Object} Returns `object`.
  1423. */
  1424. function baseAssign(object, source) {
  1425. return source == null
  1426. ? object
  1427. : baseCopy(source, keys(source), object);
  1428. }
  1429. /**
  1430. * The base implementation of `_.at` without support for string collections
  1431. * and individual key arguments.
  1432. *
  1433. * @private
  1434. * @param {Array|Object} collection The collection to iterate over.
  1435. * @param {number[]|string[]} props The property names or indexes of elements to pick.
  1436. * @returns {Array} Returns the new array of picked elements.
  1437. */
  1438. function baseAt(collection, props) {
  1439. var index = -1,
  1440. isNil = collection == null,
  1441. isArr = !isNil && isArrayLike(collection),
  1442. length = isArr ? collection.length : 0,
  1443. propsLength = props.length,
  1444. result = Array(propsLength);
  1445. while(++index < propsLength) {
  1446. var key = props[index];
  1447. if (isArr) {
  1448. result[index] = isIndex(key, length) ? collection[key] : undefined;
  1449. } else {
  1450. result[index] = isNil ? undefined : collection[key];
  1451. }
  1452. }
  1453. return result;
  1454. }
  1455. /**
  1456. * Copies properties of `source` to `object`.
  1457. *
  1458. * @private
  1459. * @param {Object} source The object to copy properties from.
  1460. * @param {Array} props The property names to copy.
  1461. * @param {Object} [object={}] The object to copy properties to.
  1462. * @returns {Object} Returns `object`.
  1463. */
  1464. function baseCopy(source, props, object) {
  1465. object || (object = {});
  1466. var index = -1,
  1467. length = props.length;
  1468. while (++index < length) {
  1469. var key = props[index];
  1470. object[key] = source[key];
  1471. }
  1472. return object;
  1473. }
  1474. /**
  1475. * The base implementation of `_.callback` which supports specifying the
  1476. * number of arguments to provide to `func`.
  1477. *
  1478. * @private
  1479. * @param {*} [func=_.identity] The value to convert to a callback.
  1480. * @param {*} [thisArg] The `this` binding of `func`.
  1481. * @param {number} [argCount] The number of arguments to provide to `func`.
  1482. * @returns {Function} Returns the callback.
  1483. */
  1484. function baseCallback(func, thisArg, argCount) {
  1485. var type = typeof func;
  1486. if (type == 'function') {
  1487. return thisArg === undefined
  1488. ? func
  1489. : bindCallback(func, thisArg, argCount);
  1490. }
  1491. if (func == null) {
  1492. return identity;
  1493. }
  1494. if (type == 'object') {
  1495. return baseMatches(func);
  1496. }
  1497. return thisArg === undefined
  1498. ? property(func)
  1499. : baseMatchesProperty(func, thisArg);
  1500. }
  1501. /**
  1502. * The base implementation of `_.clone` without support for argument juggling
  1503. * and `this` binding `customizer` functions.
  1504. *
  1505. * @private
  1506. * @param {*} value The value to clone.
  1507. * @param {boolean} [isDeep] Specify a deep clone.
  1508. * @param {Function} [customizer] The function to customize cloning values.
  1509. * @param {string} [key] The key of `value`.
  1510. * @param {Object} [object] The object `value` belongs to.
  1511. * @param {Array} [stackA=[]] Tracks traversed source objects.
  1512. * @param {Array} [stackB=[]] Associates clones with source counterparts.
  1513. * @returns {*} Returns the cloned value.
  1514. */
  1515. function baseClone(value, isDeep, customizer, key, object, stackA, stackB) {
  1516. var result;
  1517. if (customizer) {
  1518. result = object ? customizer(value, key, object) : customizer(value);
  1519. }
  1520. if (result !== undefined) {
  1521. return result;
  1522. }
  1523. if (!isObject(value)) {
  1524. return value;
  1525. }
  1526. var isArr = isArray(value);
  1527. if (isArr) {
  1528. result = initCloneArray(value);
  1529. if (!isDeep) {
  1530. return arrayCopy(value, result);
  1531. }
  1532. } else {
  1533. var tag = objToString.call(value),
  1534. isFunc = tag == funcTag;
  1535. if (tag == objectTag || tag == argsTag || (isFunc && !object)) {
  1536. result = initCloneObject(isFunc ? {} : value);
  1537. if (!isDeep) {
  1538. return baseAssign(result, value);
  1539. }
  1540. } else {
  1541. return cloneableTags[tag]
  1542. ? initCloneByTag(value, tag, isDeep)
  1543. : (object ? value : {});
  1544. }
  1545. }
  1546. // Check for circular references and return its corresponding clone.
  1547. stackA || (stackA = []);
  1548. stackB || (stackB = []);
  1549. var length = stackA.length;
  1550. while (length--) {
  1551. if (stackA[length] == value) {
  1552. return stackB[length];
  1553. }
  1554. }
  1555. // Add the source value to the stack of traversed objects and associate it with its clone.
  1556. stackA.push(value);
  1557. stackB.push(result);
  1558. // Recursively populate clone (susceptible to call stack limits).
  1559. (isArr ? arrayEach : baseForOwn)(value, function(subValue, key) {
  1560. result[key] = baseClone(subValue, isDeep, customizer, key, value, stackA, stackB);
  1561. });
  1562. return result;
  1563. }
  1564. /**
  1565. * The base implementation of `_.create` without support for assigning
  1566. * properties to the created object.
  1567. *
  1568. * @private
  1569. * @param {Object} prototype The object to inherit from.
  1570. * @returns {Object} Returns the new object.
  1571. */
  1572. var baseCreate = (function() {
  1573. function object() {}
  1574. return function(prototype) {
  1575. if (isObject(prototype)) {
  1576. object.prototype = prototype;
  1577. var result = new object;
  1578. object.prototype = undefined;
  1579. }
  1580. return result || {};
  1581. };
  1582. }());
  1583. /**
  1584. * The base implementation of `_.delay` and `_.defer` which accepts an index
  1585. * of where to slice the arguments to provide to `func`.
  1586. *
  1587. * @private
  1588. * @param {Function} func The function to delay.
  1589. * @param {number} wait The number of milliseconds to delay invocation.
  1590. * @param {Object} args The arguments provide to `func`.
  1591. * @returns {number} Returns the timer id.
  1592. */
  1593. function baseDelay(func, wait, args) {
  1594. if (typeof func != 'function') {
  1595. throw new TypeError(FUNC_ERROR_TEXT);
  1596. }
  1597. return setTimeout(function() { func.apply(undefined, args); }, wait);
  1598. }
  1599. /**
  1600. * The base implementation of `_.difference` which accepts a single array
  1601. * of values to exclude.
  1602. *
  1603. * @private
  1604. * @param {Array} array The array to inspect.
  1605. * @param {Array} values The values to exclude.
  1606. * @returns {Array} Returns the new array of filtered values.
  1607. */
  1608. function baseDifference(array, values) {
  1609. var length = array ? array.length : 0,
  1610. result = [];
  1611. if (!length) {
  1612. return result;
  1613. }
  1614. var index = -1,
  1615. indexOf = getIndexOf(),
  1616. isCommon = indexOf == baseIndexOf,
  1617. cache = (isCommon && values.length >= LARGE_ARRAY_SIZE) ? createCache(values) : null,
  1618. valuesLength = values.length;
  1619. if (cache) {
  1620. indexOf = cacheIndexOf;
  1621. isCommon = false;
  1622. values = cache;
  1623. }
  1624. outer:
  1625. while (++index < length) {
  1626. var value = array[index];
  1627. if (isCommon && value === value) {
  1628. var valuesIndex = valuesLength;
  1629. while (valuesIndex--) {
  1630. if (values[valuesIndex] === value) {
  1631. continue outer;
  1632. }
  1633. }
  1634. result.push(value);
  1635. }
  1636. else if (indexOf(values, value, 0) < 0) {
  1637. result.push(value);
  1638. }
  1639. }
  1640. return result;
  1641. }
  1642. /**
  1643. * The base implementation of `_.forEach` without support for callback
  1644. * shorthands and `this` binding.
  1645. *
  1646. * @private
  1647. * @param {Array|Object|string} collection The collection to iterate over.
  1648. * @param {Function} iteratee The function invoked per iteration.
  1649. * @returns {Array|Object|string} Returns `collection`.
  1650. */
  1651. var baseEach = createBaseEach(baseForOwn);
  1652. /**
  1653. * The base implementation of `_.forEachRight` without support for callback
  1654. * shorthands and `this` binding.
  1655. *
  1656. * @private
  1657. * @param {Array|Object|string} collection The collection to iterate over.
  1658. * @param {Function} iteratee The function invoked per iteration.
  1659. * @returns {Array|Object|string} Returns `collection`.
  1660. */
  1661. var baseEachRight = createBaseEach(baseForOwnRight, true);
  1662. /**
  1663. * The base implementation of `_.every` without support for callback
  1664. * shorthands and `this` binding.
  1665. *
  1666. * @private
  1667. * @param {Array|Object|string} collection The collection to iterate over.
  1668. * @param {Function} predicate The function invoked per iteration.
  1669. * @returns {boolean} Returns `true` if all elements pass the predicate check,
  1670. * else `false`
  1671. */
  1672. function baseEvery(collection, predicate) {
  1673. var result = true;
  1674. baseEach(collection, function(value, index, collection) {
  1675. result = !!predicate(value, index, collection);
  1676. return result;
  1677. });
  1678. return result;
  1679. }
  1680. /**
  1681. * Gets the extremum value of `collection` invoking `iteratee` for each value
  1682. * in `collection` to generate the criterion by which the value is ranked.
  1683. * The `iteratee` is invoked with three arguments: (value, index|key, collection).
  1684. *
  1685. * @private
  1686. * @param {Array|Object|string} collection The collection to iterate over.
  1687. * @param {Function} iteratee The function invoked per iteration.
  1688. * @param {Function} comparator The function used to compare values.
  1689. * @param {*} exValue The initial extremum value.
  1690. * @returns {*} Returns the extremum value.
  1691. */
  1692. function baseExtremum(collection, iteratee, comparator, exValue) {
  1693. var computed = exValue,
  1694. result = computed;
  1695. baseEach(collection, function(value, index, collection) {
  1696. var current = +iteratee(value, index, collection);
  1697. if (comparator(current, computed) || (current === exValue && current === result)) {
  1698. computed = current;
  1699. result = value;
  1700. }
  1701. });
  1702. return result;
  1703. }
  1704. /**
  1705. * The base implementation of `_.fill` without an iteratee call guard.
  1706. *
  1707. * @private
  1708. * @param {Array} array The array to fill.
  1709. * @param {*} value The value to fill `array` with.
  1710. * @param {number} [start=0] The start position.
  1711. * @param {number} [end=array.length] The end position.
  1712. * @returns {Array} Returns `array`.
  1713. */
  1714. function baseFill(array, value, start, end) {
  1715. var length = array.length;
  1716. start = start == null ? 0 : (+start || 0);
  1717. if (start < 0) {
  1718. start = -start > length ? 0 : (length + start);
  1719. }
  1720. end = (end === undefined || end > length) ? length : (+end || 0);
  1721. if (end < 0) {
  1722. end += length;
  1723. }
  1724. length = start > end ? 0 : (end >>> 0);
  1725. start >>>= 0;
  1726. while (start < length) {
  1727. array[start++] = value;
  1728. }
  1729. return array;
  1730. }
  1731. /**
  1732. * The base implementation of `_.filter` without support for callback
  1733. * shorthands and `this` binding.
  1734. *
  1735. * @private
  1736. * @param {Array|Object|string} collection The collection to iterate over.
  1737. * @param {Function} predicate The function invoked per iteration.
  1738. * @returns {Array} Returns the new filtered array.
  1739. */
  1740. function baseFilter(collection, predicate) {
  1741. var result = [];
  1742. baseEach(collection, function(value, index, collection) {
  1743. if (predicate(value, index, collection)) {
  1744. result.push(value);
  1745. }
  1746. });
  1747. return result;
  1748. }
  1749. /**
  1750. * The base implementation of `_.find`, `_.findLast`, `_.findKey`, and `_.findLastKey`,
  1751. * without support for callback shorthands and `this` binding, which iterates
  1752. * over `collection` using the provided `eachFunc`.
  1753. *
  1754. * @private
  1755. * @param {Array|Object|string} collection The collection to search.
  1756. * @param {Function} predicate The function invoked per iteration.
  1757. * @param {Function} eachFunc The function to iterate over `collection`.
  1758. * @param {boolean} [retKey] Specify returning the key of the found element
  1759. * instead of the element itself.
  1760. * @returns {*} Returns the found element or its key, else `undefined`.
  1761. */
  1762. function baseFind(collection, predicate, eachFunc, retKey) {
  1763. var result;
  1764. eachFunc(collection, function(value, key, collection) {
  1765. if (predicate(value, key, collection)) {
  1766. result = retKey ? key : value;
  1767. return false;
  1768. }
  1769. });
  1770. return result;
  1771. }
  1772. /**
  1773. * The base implementation of `_.flatten` with added support for restricting
  1774. * flattening and specifying the start index.
  1775. *
  1776. * @private
  1777. * @param {Array} array The array to flatten.
  1778. * @param {boolean} [isDeep] Specify a deep flatten.
  1779. * @param {boolean} [isStrict] Restrict flattening to arrays-like objects.
  1780. * @param {Array} [result=[]] The initial result value.
  1781. * @returns {Array} Returns the new flattened array.
  1782. */
  1783. function baseFlatten(array, isDeep, isStrict, result) {
  1784. result || (result = []);
  1785. var index = -1,
  1786. length = array.length;
  1787. while (++index < length) {
  1788. var value = array[index];
  1789. if (isObjectLike(value) && isArrayLike(value) &&
  1790. (isStrict || isArray(value) || isArguments(value))) {
  1791. if (isDeep) {
  1792. // Recursively flatten arrays (susceptible to call stack limits).
  1793. baseFlatten(value, isDeep, isStrict, result);
  1794. } else {
  1795. arrayPush(result, value);
  1796. }
  1797. } else if (!isStrict) {
  1798. result[result.length] = value;
  1799. }
  1800. }
  1801. return result;
  1802. }
  1803. /**
  1804. * The base implementation of `baseForIn` and `baseForOwn` which iterates
  1805. * over `object` properties returned by `keysFunc` invoking `iteratee` for
  1806. * each property. Iteratee functions may exit iteration early by explicitly
  1807. * returning `false`.
  1808. *
  1809. * @private
  1810. * @param {Object} object The object to iterate over.
  1811. * @param {Function} iteratee The function invoked per iteration.
  1812. * @param {Function} keysFunc The function to get the keys of `object`.
  1813. * @returns {Object} Returns `object`.
  1814. */
  1815. var baseFor = createBaseFor();
  1816. /**
  1817. * This function is like `baseFor` except that it iterates over properties
  1818. * in the opposite order.
  1819. *
  1820. * @private
  1821. * @param {Object} object The object to iterate over.
  1822. * @param {Function} iteratee The function invoked per iteration.
  1823. * @param {Function} keysFunc The function to get the keys of `object`.
  1824. * @returns {Object} Returns `object`.
  1825. */
  1826. var baseForRight = createBaseFor(true);
  1827. /**
  1828. * The base implementation of `_.forIn` without support for callback
  1829. * shorthands and `this` binding.
  1830. *
  1831. * @private
  1832. * @param {Object} object The object to iterate over.
  1833. * @param {Function} iteratee The function invoked per iteration.
  1834. * @returns {Object} Returns `object`.
  1835. */
  1836. function baseForIn(object, iteratee) {
  1837. return baseFor(object, iteratee, keysIn);
  1838. }
  1839. /**
  1840. * The base implementation of `_.forOwn` without support for callback
  1841. * shorthands and `this` binding.
  1842. *
  1843. * @private
  1844. * @param {Object} object The object to iterate over.
  1845. * @param {Function} iteratee The function invoked per iteration.
  1846. * @returns {Object} Returns `object`.
  1847. */
  1848. function baseForOwn(object, iteratee) {
  1849. return baseFor(object, iteratee, keys);
  1850. }
  1851. /**
  1852. * The base implementation of `_.forOwnRight` without support for callback
  1853. * shorthands and `this` binding.
  1854. *
  1855. * @private
  1856. * @param {Object} object The object to iterate over.
  1857. * @param {Function} iteratee The function invoked per iteration.
  1858. * @returns {Object} Returns `object`.
  1859. */
  1860. function baseForOwnRight(object, iteratee) {
  1861. return baseForRight(object, iteratee, keys);
  1862. }
  1863. /**
  1864. * The base implementation of `_.functions` which creates an array of
  1865. * `object` function property names filtered from those provided.
  1866. *
  1867. * @private
  1868. * @param {Object} object The object to inspect.
  1869. * @param {Array} props The property names to filter.
  1870. * @returns {Array} Returns the new array of filtered property names.
  1871. */
  1872. function baseFunctions(object, props) {
  1873. var index = -1,
  1874. length = props.length,
  1875. resIndex = -1,
  1876. result = [];
  1877. while (++index < length) {
  1878. var key = props[index];
  1879. if (isFunction(object[key])) {
  1880. result[++resIndex] = key;
  1881. }
  1882. }
  1883. return result;
  1884. }
  1885. /**
  1886. * The base implementation of `get` without support for string paths
  1887. * and default values.
  1888. *
  1889. * @private
  1890. * @param {Object} object The object to query.
  1891. * @param {Array} path The path of the property to get.
  1892. * @param {string} [pathKey] The key representation of path.
  1893. * @returns {*} Returns the resolved value.
  1894. */
  1895. function baseGet(object, path, pathKey) {
  1896. if (object == null) {
  1897. return;
  1898. }
  1899. if (pathKey !== undefined && pathKey in toObject(object)) {
  1900. path = [pathKey];
  1901. }
  1902. var index = 0,
  1903. length = path.length;
  1904. while (object != null && index < length) {
  1905. object = object[path[index++]];
  1906. }
  1907. return (index && index == length) ? object : undefined;
  1908. }
  1909. /**
  1910. * The base implementation of `_.isEqual` without support for `this` binding
  1911. * `customizer` functions.
  1912. *
  1913. * @private
  1914. * @param {*} value The value to compare.
  1915. * @param {*} other The other value to compare.
  1916. * @param {Function} [customizer] The function to customize comparing values.
  1917. * @param {boolean} [isLoose] Specify performing partial comparisons.
  1918. * @param {Array} [stackA] Tracks traversed `value` objects.
  1919. * @param {Array} [stackB] Tracks traversed `other` objects.
  1920. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  1921. */
  1922. function baseIsEqual(value, other, customizer, isLoose, stackA, stackB) {
  1923. if (value === other) {
  1924. return true;
  1925. }
  1926. if (value == null || other == null || (!isObject(value) && !isObjectLike(other))) {
  1927. return value !== value && other !== other;
  1928. }
  1929. return baseIsEqualDeep(value, other, baseIsEqual, customizer, isLoose, stackA, stackB);
  1930. }
  1931. /**
  1932. * A specialized version of `baseIsEqual` for arrays and objects which performs
  1933. * deep comparisons and tracks traversed objects enabling objects with circular
  1934. * references to be compared.
  1935. *
  1936. * @private
  1937. * @param {Object} object The object to compare.
  1938. * @param {Object} other The other object to compare.
  1939. * @param {Function} equalFunc The function to determine equivalents of values.
  1940. * @param {Function} [customizer] The function to customize comparing objects.
  1941. * @param {boolean} [isLoose] Specify performing partial comparisons.
  1942. * @param {Array} [stackA=[]] Tracks traversed `value` objects.
  1943. * @param {Array} [stackB=[]] Tracks traversed `other` objects.
  1944. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  1945. */
  1946. function baseIsEqualDeep(object, other, equalFunc, customizer, isLoose, stackA, stackB) {
  1947. var objIsArr = isArray(object),
  1948. othIsArr = isArray(other),
  1949. objTag = arrayTag,
  1950. othTag = arrayTag;
  1951. if (!objIsArr) {
  1952. objTag = objToString.call(object);
  1953. if (objTag == argsTag) {
  1954. objTag = objectTag;
  1955. } else if (objTag != objectTag) {
  1956. objIsArr = isTypedArray(object);
  1957. }
  1958. }
  1959. if (!othIsArr) {
  1960. othTag = objToString.call(other);
  1961. if (othTag == argsTag) {
  1962. othTag = objectTag;
  1963. } else if (othTag != objectTag) {
  1964. othIsArr = isTypedArray(other);
  1965. }
  1966. }
  1967. var objIsObj = objTag == objectTag,
  1968. othIsObj = othTag == objectTag,
  1969. isSameTag = objTag == othTag;
  1970. if (isSameTag && !(objIsArr || objIsObj)) {
  1971. return equalByTag(object, other, objTag);
  1972. }
  1973. if (!isLoose) {
  1974. var objIsWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'),
  1975. othIsWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__');
  1976. if (objIsWrapped || othIsWrapped) {
  1977. return equalFunc(objIsWrapped ? object.value() : object, othIsWrapped ? other.value() : other, customizer, isLoose, stackA, stackB);
  1978. }
  1979. }
  1980. if (!isSameTag) {
  1981. return false;
  1982. }
  1983. // Assume cyclic values are equal.
  1984. // For more information on detecting circular references see https://es5.github.io/#JO.
  1985. stackA || (stackA = []);
  1986. stackB || (stackB = []);
  1987. var length = stackA.length;
  1988. while (length--) {
  1989. if (stackA[length] == object) {
  1990. return stackB[length] == other;
  1991. }
  1992. }
  1993. // Add `object` and `other` to the stack of traversed objects.
  1994. stackA.push(object);
  1995. stackB.push(other);
  1996. var result = (objIsArr ? equalArrays : equalObjects)(object, other, equalFunc, customizer, isLoose, stackA, stackB);
  1997. stackA.pop();
  1998. stackB.pop();
  1999. return result;
  2000. }
  2001. /**
  2002. * The base implementation of `_.isMatch` without support for callback
  2003. * shorthands and `this` binding.
  2004. *
  2005. * @private
  2006. * @param {Object} object The object to inspect.
  2007. * @param {Array} matchData The propery names, values, and compare flags to match.
  2008. * @param {Function} [customizer] The function to customize comparing objects.
  2009. * @returns {boolean} Returns `true` if `object` is a match, else `false`.
  2010. */
  2011. function baseIsMatch(object, matchData, customizer) {
  2012. var index = matchData.length,
  2013. length = index,
  2014. noCustomizer = !customizer;
  2015. if (object == null) {
  2016. return !length;
  2017. }
  2018. object = toObject(object);
  2019. while (index--) {
  2020. var data = matchData[index];
  2021. if ((noCustomizer && data[2])
  2022. ? data[1] !== object[data[0]]
  2023. : !(data[0] in object)
  2024. ) {
  2025. return false;
  2026. }
  2027. }
  2028. while (++index < length) {
  2029. data = matchData[index];
  2030. var key = data[0],
  2031. objValue = object[key],
  2032. srcValue = data[1];
  2033. if (noCustomizer && data[2]) {
  2034. if (objValue === undefined && !(key in object)) {
  2035. return false;
  2036. }
  2037. } else {
  2038. var result = customizer ? customizer(objValue, srcValue, key) : undefined;
  2039. if (!(result === undefined ? baseIsEqual(srcValue, objValue, customizer, true) : result)) {
  2040. return false;
  2041. }
  2042. }
  2043. }
  2044. return true;
  2045. }
  2046. /**
  2047. * The base implementation of `_.map` without support for callback shorthands
  2048. * and `this` binding.
  2049. *
  2050. * @private
  2051. * @param {Array|Object|string} collection The collection to iterate over.
  2052. * @param {Function} iteratee The function invoked per iteration.
  2053. * @returns {Array} Returns the new mapped array.
  2054. */
  2055. function baseMap(collection, iteratee) {
  2056. var index = -1,
  2057. result = isArrayLike(collection) ? Array(collection.length) : [];
  2058. baseEach(collection, function(value, key, collection) {
  2059. result[++index] = iteratee(value, key, collection);
  2060. });
  2061. return result;
  2062. }
  2063. /**
  2064. * The base implementation of `_.matches` which does not clone `source`.
  2065. *
  2066. * @private
  2067. * @param {Object} source The object of property values to match.
  2068. * @returns {Function} Returns the new function.
  2069. */
  2070. function baseMatches(source) {
  2071. var matchData = getMatchData(source);
  2072. if (matchData.length == 1 && matchData[0][2]) {
  2073. var key = matchData[0][0],
  2074. value = matchData[0][1];
  2075. return function(object) {
  2076. if (object == null) {
  2077. return false;
  2078. }
  2079. return object[key] === value && (value !== undefined || (key in toObject(object)));
  2080. };
  2081. }
  2082. return function(object) {
  2083. return baseIsMatch(object, matchData);
  2084. };
  2085. }
  2086. /**
  2087. * The base implementation of `_.matchesProperty` which does not clone `srcValue`.
  2088. *
  2089. * @private
  2090. * @param {string} path The path of the property to get.
  2091. * @param {*} srcValue The value to compare.
  2092. * @returns {Function} Returns the new function.
  2093. */
  2094. function baseMatchesProperty(path, srcValue) {
  2095. var isArr = isArray(path),
  2096. isCommon = isKey(path) && isStrictComparable(srcValue),
  2097. pathKey = (path + '');
  2098. path = toPath(path);
  2099. return function(object) {
  2100. if (object == null) {
  2101. return false;
  2102. }
  2103. var key = pathKey;
  2104. object = toObject(object);
  2105. if ((isArr || !isCommon) && !(key in object)) {
  2106. object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  2107. if (object == null) {
  2108. return false;
  2109. }
  2110. key = last(path);
  2111. object = toObject(object);
  2112. }
  2113. return object[key] === srcValue
  2114. ? (srcValue !== undefined || (key in object))
  2115. : baseIsEqual(srcValue, object[key], undefined, true);
  2116. };
  2117. }
  2118. /**
  2119. * The base implementation of `_.merge` without support for argument juggling,
  2120. * multiple sources, and `this` binding `customizer` functions.
  2121. *
  2122. * @private
  2123. * @param {Object} object The destination object.
  2124. * @param {Object} source The source object.
  2125. * @param {Function} [customizer] The function to customize merged values.
  2126. * @param {Array} [stackA=[]] Tracks traversed source objects.
  2127. * @param {Array} [stackB=[]] Associates values with source counterparts.
  2128. * @returns {Object} Returns `object`.
  2129. */
  2130. function baseMerge(object, source, customizer, stackA, stackB) {
  2131. if (!isObject(object)) {
  2132. return object;
  2133. }
  2134. var isSrcArr = isArrayLike(source) && (isArray(source) || isTypedArray(source)),
  2135. props = isSrcArr ? undefined : keys(source);
  2136. arrayEach(props || source, function(srcValue, key) {
  2137. if (props) {
  2138. key = srcValue;
  2139. srcValue = source[key];
  2140. }
  2141. if (isObjectLike(srcValue)) {
  2142. stackA || (stackA = []);
  2143. stackB || (stackB = []);
  2144. baseMergeDeep(object, source, key, baseMerge, customizer, stackA, stackB);
  2145. }
  2146. else {
  2147. var value = object[key],
  2148. result = customizer ? customizer(value, srcValue, key, object, source) : undefined,
  2149. isCommon = result === undefined;
  2150. if (isCommon) {
  2151. result = srcValue;
  2152. }
  2153. if ((result !== undefined || (isSrcArr && !(key in object))) &&
  2154. (isCommon || (result === result ? (result !== value) : (value === value)))) {
  2155. object[key] = result;
  2156. }
  2157. }
  2158. });
  2159. return object;
  2160. }
  2161. /**
  2162. * A specialized version of `baseMerge` for arrays and objects which performs
  2163. * deep merges and tracks traversed objects enabling objects with circular
  2164. * references to be merged.
  2165. *
  2166. * @private
  2167. * @param {Object} object The destination object.
  2168. * @param {Object} source The source object.
  2169. * @param {string} key The key of the value to merge.
  2170. * @param {Function} mergeFunc The function to merge values.
  2171. * @param {Function} [customizer] The function to customize merged values.
  2172. * @param {Array} [stackA=[]] Tracks traversed source objects.
  2173. * @param {Array} [stackB=[]] Associates values with source counterparts.
  2174. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  2175. */
  2176. function baseMergeDeep(object, source, key, mergeFunc, customizer, stackA, stackB) {
  2177. var length = stackA.length,
  2178. srcValue = source[key];
  2179. while (length--) {
  2180. if (stackA[length] == srcValue) {
  2181. object[key] = stackB[length];
  2182. return;
  2183. }
  2184. }
  2185. var value = object[key],
  2186. result = customizer ? customizer(value, srcValue, key, object, source) : undefined,
  2187. isCommon = result === undefined;
  2188. if (isCommon) {
  2189. result = srcValue;
  2190. if (isArrayLike(srcValue) && (isArray(srcValue) || isTypedArray(srcValue))) {
  2191. result = isArray(value)
  2192. ? value
  2193. : (isArrayLike(value) ? arrayCopy(value) : []);
  2194. }
  2195. else if (isPlainObject(srcValue) || isArguments(srcValue)) {
  2196. result = isArguments(value)
  2197. ? toPlainObject(value)
  2198. : (isPlainObject(value) ? value : {});
  2199. }
  2200. else {
  2201. isCommon = false;
  2202. }
  2203. }
  2204. // Add the source value to the stack of traversed objects and associate
  2205. // it with its merged value.
  2206. stackA.push(srcValue);
  2207. stackB.push(result);
  2208. if (isCommon) {
  2209. // Recursively merge objects and arrays (susceptible to call stack limits).
  2210. object[key] = mergeFunc(result, srcValue, customizer, stackA, stackB);
  2211. } else if (result === result ? (result !== value) : (value === value)) {
  2212. object[key] = result;
  2213. }
  2214. }
  2215. /**
  2216. * The base implementation of `_.property` without support for deep paths.
  2217. *
  2218. * @private
  2219. * @param {string} key The key of the property to get.
  2220. * @returns {Function} Returns the new function.
  2221. */
  2222. function baseProperty(key) {
  2223. return function(object) {
  2224. return object == null ? undefined : object[key];
  2225. };
  2226. }
  2227. /**
  2228. * A specialized version of `baseProperty` which supports deep paths.
  2229. *
  2230. * @private
  2231. * @param {Array|string} path The path of the property to get.
  2232. * @returns {Function} Returns the new function.
  2233. */
  2234. function basePropertyDeep(path) {
  2235. var pathKey = (path + '');
  2236. path = toPath(path);
  2237. return function(object) {
  2238. return baseGet(object, path, pathKey);
  2239. };
  2240. }
  2241. /**
  2242. * The base implementation of `_.pullAt` without support for individual
  2243. * index arguments and capturing the removed elements.
  2244. *
  2245. * @private
  2246. * @param {Array} array The array to modify.
  2247. * @param {number[]} indexes The indexes of elements to remove.
  2248. * @returns {Array} Returns `array`.
  2249. */
  2250. function basePullAt(array, indexes) {
  2251. var length = array ? indexes.length : 0;
  2252. while (length--) {
  2253. var index = indexes[length];
  2254. if (index != previous && isIndex(index)) {
  2255. var previous = index;
  2256. splice.call(array, index, 1);
  2257. }
  2258. }
  2259. return array;
  2260. }
  2261. /**
  2262. * The base implementation of `_.random` without support for argument juggling
  2263. * and returning floating-point numbers.
  2264. *
  2265. * @private
  2266. * @param {number} min The minimum possible value.
  2267. * @param {number} max The maximum possible value.
  2268. * @returns {number} Returns the random number.
  2269. */
  2270. function baseRandom(min, max) {
  2271. return min + nativeFloor(nativeRandom() * (max - min + 1));
  2272. }
  2273. /**
  2274. * The base implementation of `_.reduce` and `_.reduceRight` without support
  2275. * for callback shorthands and `this` binding, which iterates over `collection`
  2276. * using the provided `eachFunc`.
  2277. *
  2278. * @private
  2279. * @param {Array|Object|string} collection The collection to iterate over.
  2280. * @param {Function} iteratee The function invoked per iteration.
  2281. * @param {*} accumulator The initial value.
  2282. * @param {boolean} initFromCollection Specify using the first or last element
  2283. * of `collection` as the initial value.
  2284. * @param {Function} eachFunc The function to iterate over `collection`.
  2285. * @returns {*} Returns the accumulated value.
  2286. */
  2287. function baseReduce(collection, iteratee, accumulator, initFromCollection, eachFunc) {
  2288. eachFunc(collection, function(value, index, collection) {
  2289. accumulator = initFromCollection
  2290. ? (initFromCollection = false, value)
  2291. : iteratee(accumulator, value, index, collection);
  2292. });
  2293. return accumulator;
  2294. }
  2295. /**
  2296. * The base implementation of `setData` without support for hot loop detection.
  2297. *
  2298. * @private
  2299. * @param {Function} func The function to associate metadata with.
  2300. * @param {*} data The metadata.
  2301. * @returns {Function} Returns `func`.
  2302. */
  2303. var baseSetData = !metaMap ? identity : function(func, data) {
  2304. metaMap.set(func, data);
  2305. return func;
  2306. };
  2307. /**
  2308. * The base implementation of `_.slice` without an iteratee call guard.
  2309. *
  2310. * @private
  2311. * @param {Array} array The array to slice.
  2312. * @param {number} [start=0] The start position.
  2313. * @param {number} [end=array.length] The end position.
  2314. * @returns {Array} Returns the slice of `array`.
  2315. */
  2316. function baseSlice(array, start, end) {
  2317. var index = -1,
  2318. length = array.length;
  2319. start = start == null ? 0 : (+start || 0);
  2320. if (start < 0) {
  2321. start = -start > length ? 0 : (length + start);
  2322. }
  2323. end = (end === undefined || end > length) ? length : (+end || 0);
  2324. if (end < 0) {
  2325. end += length;
  2326. }
  2327. length = start > end ? 0 : ((end - start) >>> 0);
  2328. start >>>= 0;
  2329. var result = Array(length);
  2330. while (++index < length) {
  2331. result[index] = array[index + start];
  2332. }
  2333. return result;
  2334. }
  2335. /**
  2336. * The base implementation of `_.some` without support for callback shorthands
  2337. * and `this` binding.
  2338. *
  2339. * @private
  2340. * @param {Array|Object|string} collection The collection to iterate over.
  2341. * @param {Function} predicate The function invoked per iteration.
  2342. * @returns {boolean} Returns `true` if any element passes the predicate check,
  2343. * else `false`.
  2344. */
  2345. function baseSome(collection, predicate) {
  2346. var result;
  2347. baseEach(collection, function(value, index, collection) {
  2348. result = predicate(value, index, collection);
  2349. return !result;
  2350. });
  2351. return !!result;
  2352. }
  2353. /**
  2354. * The base implementation of `_.sortBy` which uses `comparer` to define
  2355. * the sort order of `array` and replaces criteria objects with their
  2356. * corresponding values.
  2357. *
  2358. * @private
  2359. * @param {Array} array The array to sort.
  2360. * @param {Function} comparer The function to define sort order.
  2361. * @returns {Array} Returns `array`.
  2362. */
  2363. function baseSortBy(array, comparer) {
  2364. var length = array.length;
  2365. array.sort(comparer);
  2366. while (length--) {
  2367. array[length] = array[length].value;
  2368. }
  2369. return array;
  2370. }
  2371. /**
  2372. * The base implementation of `_.sortByOrder` without param guards.
  2373. *
  2374. * @private
  2375. * @param {Array|Object|string} collection The collection to iterate over.
  2376. * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by.
  2377. * @param {boolean[]} orders The sort orders of `iteratees`.
  2378. * @returns {Array} Returns the new sorted array.
  2379. */
  2380. function baseSortByOrder(collection, iteratees, orders) {
  2381. var callback = getCallback(),
  2382. index = -1;
  2383. iteratees = arrayMap(iteratees, function(iteratee) { return callback(iteratee); });
  2384. var result = baseMap(collection, function(value) {
  2385. var criteria = arrayMap(iteratees, function(iteratee) { return iteratee(value); });
  2386. return { 'criteria': criteria, 'index': ++index, 'value': value };
  2387. });
  2388. return baseSortBy(result, function(object, other) {
  2389. return compareMultiple(object, other, orders);
  2390. });
  2391. }
  2392. /**
  2393. * The base implementation of `_.sum` without support for callback shorthands
  2394. * and `this` binding.
  2395. *
  2396. * @private
  2397. * @param {Array|Object|string} collection The collection to iterate over.
  2398. * @param {Function} iteratee The function invoked per iteration.
  2399. * @returns {number} Returns the sum.
  2400. */
  2401. function baseSum(collection, iteratee) {
  2402. var result = 0;
  2403. baseEach(collection, function(value, index, collection) {
  2404. result += +iteratee(value, index, collection) || 0;
  2405. });
  2406. return result;
  2407. }
  2408. /**
  2409. * The base implementation of `_.uniq` without support for callback shorthands
  2410. * and `this` binding.
  2411. *
  2412. * @private
  2413. * @param {Array} array The array to inspect.
  2414. * @param {Function} [iteratee] The function invoked per iteration.
  2415. * @returns {Array} Returns the new duplicate-value-free array.
  2416. */
  2417. function baseUniq(array, iteratee) {
  2418. var index = -1,
  2419. indexOf = getIndexOf(),
  2420. length = array.length,
  2421. isCommon = indexOf == baseIndexOf,
  2422. isLarge = isCommon && length >= LARGE_ARRAY_SIZE,
  2423. seen = isLarge ? createCache() : null,
  2424. result = [];
  2425. if (seen) {
  2426. indexOf = cacheIndexOf;
  2427. isCommon = false;
  2428. } else {
  2429. isLarge = false;
  2430. seen = iteratee ? [] : result;
  2431. }
  2432. outer:
  2433. while (++index < length) {
  2434. var value = array[index],
  2435. computed = iteratee ? iteratee(value, index, array) : value;
  2436. if (isCommon && value === value) {
  2437. var seenIndex = seen.length;
  2438. while (seenIndex--) {
  2439. if (seen[seenIndex] === computed) {
  2440. continue outer;
  2441. }
  2442. }
  2443. if (iteratee) {
  2444. seen.push(computed);
  2445. }
  2446. result.push(value);
  2447. }
  2448. else if (indexOf(seen, computed, 0) < 0) {
  2449. if (iteratee || isLarge) {
  2450. seen.push(computed);
  2451. }
  2452. result.push(value);
  2453. }
  2454. }
  2455. return result;
  2456. }
  2457. /**
  2458. * The base implementation of `_.values` and `_.valuesIn` which creates an
  2459. * array of `object` property values corresponding to the property names
  2460. * of `props`.
  2461. *
  2462. * @private
  2463. * @param {Object} object The object to query.
  2464. * @param {Array} props The property names to get values for.
  2465. * @returns {Object} Returns the array of property values.
  2466. */
  2467. function baseValues(object, props) {
  2468. var index = -1,
  2469. length = props.length,
  2470. result = Array(length);
  2471. while (++index < length) {
  2472. result[index] = object[props[index]];
  2473. }
  2474. return result;
  2475. }
  2476. /**
  2477. * The base implementation of `_.dropRightWhile`, `_.dropWhile`, `_.takeRightWhile`,
  2478. * and `_.takeWhile` without support for callback shorthands and `this` binding.
  2479. *
  2480. * @private
  2481. * @param {Array} array The array to query.
  2482. * @param {Function} predicate The function invoked per iteration.
  2483. * @param {boolean} [isDrop] Specify dropping elements instead of taking them.
  2484. * @param {boolean} [fromRight] Specify iterating from right to left.
  2485. * @returns {Array} Returns the slice of `array`.
  2486. */
  2487. function baseWhile(array, predicate, isDrop, fromRight) {
  2488. var length = array.length,
  2489. index = fromRight ? length : -1;
  2490. while ((fromRight ? index-- : ++index < length) && predicate(array[index], index, array)) {}
  2491. return isDrop
  2492. ? baseSlice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length))
  2493. : baseSlice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index));
  2494. }
  2495. /**
  2496. * The base implementation of `wrapperValue` which returns the result of
  2497. * performing a sequence of actions on the unwrapped `value`, where each
  2498. * successive action is supplied the return value of the previous.
  2499. *
  2500. * @private
  2501. * @param {*} value The unwrapped value.
  2502. * @param {Array} actions Actions to peform to resolve the unwrapped value.
  2503. * @returns {*} Returns the resolved value.
  2504. */
  2505. function baseWrapperValue(value, actions) {
  2506. var result = value;
  2507. if (result instanceof LazyWrapper) {
  2508. result = result.value();
  2509. }
  2510. var index = -1,
  2511. length = actions.length;
  2512. while (++index < length) {
  2513. var action = actions[index];
  2514. result = action.func.apply(action.thisArg, arrayPush([result], action.args));
  2515. }
  2516. return result;
  2517. }
  2518. /**
  2519. * Performs a binary search of `array` to determine the index at which `value`
  2520. * should be inserted into `array` in order to maintain its sort order.
  2521. *
  2522. * @private
  2523. * @param {Array} array The sorted array to inspect.
  2524. * @param {*} value The value to evaluate.
  2525. * @param {boolean} [retHighest] Specify returning the highest qualified index.
  2526. * @returns {number} Returns the index at which `value` should be inserted
  2527. * into `array`.
  2528. */
  2529. function binaryIndex(array, value, retHighest) {
  2530. var low = 0,
  2531. high = array ? array.length : low;
  2532. if (typeof value == 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) {
  2533. while (low < high) {
  2534. var mid = (low + high) >>> 1,
  2535. computed = array[mid];
  2536. if ((retHighest ? (computed <= value) : (computed < value)) && computed !== null) {
  2537. low = mid + 1;
  2538. } else {
  2539. high = mid;
  2540. }
  2541. }
  2542. return high;
  2543. }
  2544. return binaryIndexBy(array, value, identity, retHighest);
  2545. }
  2546. /**
  2547. * This function is like `binaryIndex` except that it invokes `iteratee` for
  2548. * `value` and each element of `array` to compute their sort ranking. The
  2549. * iteratee is invoked with one argument; (value).
  2550. *
  2551. * @private
  2552. * @param {Array} array The sorted array to inspect.
  2553. * @param {*} value The value to evaluate.
  2554. * @param {Function} iteratee The function invoked per iteration.
  2555. * @param {boolean} [retHighest] Specify returning the highest qualified index.
  2556. * @returns {number} Returns the index at which `value` should be inserted
  2557. * into `array`.
  2558. */
  2559. function binaryIndexBy(array, value, iteratee, retHighest) {
  2560. value = iteratee(value);
  2561. var low = 0,
  2562. high = array ? array.length : 0,
  2563. valIsNaN = value !== value,
  2564. valIsNull = value === null,
  2565. valIsUndef = value === undefined;
  2566. while (low < high) {
  2567. var mid = nativeFloor((low + high) / 2),
  2568. computed = iteratee(array[mid]),
  2569. isDef = computed !== undefined,
  2570. isReflexive = computed === computed;
  2571. if (valIsNaN) {
  2572. var setLow = isReflexive || retHighest;
  2573. } else if (valIsNull) {
  2574. setLow = isReflexive && isDef && (retHighest || computed != null);
  2575. } else if (valIsUndef) {
  2576. setLow = isReflexive && (retHighest || isDef);
  2577. } else if (computed == null) {
  2578. setLow = false;
  2579. } else {
  2580. setLow = retHighest ? (computed <= value) : (computed < value);
  2581. }
  2582. if (setLow) {
  2583. low = mid + 1;
  2584. } else {
  2585. high = mid;
  2586. }
  2587. }
  2588. return nativeMin(high, MAX_ARRAY_INDEX);
  2589. }
  2590. /**
  2591. * A specialized version of `baseCallback` which only supports `this` binding
  2592. * and specifying the number of arguments to provide to `func`.
  2593. *
  2594. * @private
  2595. * @param {Function} func The function to bind.
  2596. * @param {*} thisArg The `this` binding of `func`.
  2597. * @param {number} [argCount] The number of arguments to provide to `func`.
  2598. * @returns {Function} Returns the callback.
  2599. */
  2600. function bindCallback(func, thisArg, argCount) {
  2601. if (typeof func != 'function') {
  2602. return identity;
  2603. }
  2604. if (thisArg === undefined) {
  2605. return func;
  2606. }
  2607. switch (argCount) {
  2608. case 1: return function(value) {
  2609. return func.call(thisArg, value);
  2610. };
  2611. case 3: return function(value, index, collection) {
  2612. return func.call(thisArg, value, index, collection);
  2613. };
  2614. case 4: return function(accumulator, value, index, collection) {
  2615. return func.call(thisArg, accumulator, value, index, collection);
  2616. };
  2617. case 5: return function(value, other, key, object, source) {
  2618. return func.call(thisArg, value, other, key, object, source);
  2619. };
  2620. }
  2621. return function() {
  2622. return func.apply(thisArg, arguments);
  2623. };
  2624. }
  2625. /**
  2626. * Creates a clone of the given array buffer.
  2627. *
  2628. * @private
  2629. * @param {ArrayBuffer} buffer The array buffer to clone.
  2630. * @returns {ArrayBuffer} Returns the cloned array buffer.
  2631. */
  2632. function bufferClone(buffer) {
  2633. var result = new ArrayBuffer(buffer.byteLength),
  2634. view = new Uint8Array(result);
  2635. view.set(new Uint8Array(buffer));
  2636. return result;
  2637. }
  2638. /**
  2639. * Creates an array that is the composition of partially applied arguments,
  2640. * placeholders, and provided arguments into a single array of arguments.
  2641. *
  2642. * @private
  2643. * @param {Array|Object} args The provided arguments.
  2644. * @param {Array} partials The arguments to prepend to those provided.
  2645. * @param {Array} holders The `partials` placeholder indexes.
  2646. * @returns {Array} Returns the new array of composed arguments.
  2647. */
  2648. function composeArgs(args, partials, holders) {
  2649. var holdersLength = holders.length,
  2650. argsIndex = -1,
  2651. argsLength = nativeMax(args.length - holdersLength, 0),
  2652. leftIndex = -1,
  2653. leftLength = partials.length,
  2654. result = Array(leftLength + argsLength);
  2655. while (++leftIndex < leftLength) {
  2656. result[leftIndex] = partials[leftIndex];
  2657. }
  2658. while (++argsIndex < holdersLength) {
  2659. result[holders[argsIndex]] = args[argsIndex];
  2660. }
  2661. while (argsLength--) {
  2662. result[leftIndex++] = args[argsIndex++];
  2663. }
  2664. return result;
  2665. }
  2666. /**
  2667. * This function is like `composeArgs` except that the arguments composition
  2668. * is tailored for `_.partialRight`.
  2669. *
  2670. * @private
  2671. * @param {Array|Object} args The provided arguments.
  2672. * @param {Array} partials The arguments to append to those provided.
  2673. * @param {Array} holders The `partials` placeholder indexes.
  2674. * @returns {Array} Returns the new array of composed arguments.
  2675. */
  2676. function composeArgsRight(args, partials, holders) {
  2677. var holdersIndex = -1,
  2678. holdersLength = holders.length,
  2679. argsIndex = -1,
  2680. argsLength = nativeMax(args.length - holdersLength, 0),
  2681. rightIndex = -1,
  2682. rightLength = partials.length,
  2683. result = Array(argsLength + rightLength);
  2684. while (++argsIndex < argsLength) {
  2685. result[argsIndex] = args[argsIndex];
  2686. }
  2687. var offset = argsIndex;
  2688. while (++rightIndex < rightLength) {
  2689. result[offset + rightIndex] = partials[rightIndex];
  2690. }
  2691. while (++holdersIndex < holdersLength) {
  2692. result[offset + holders[holdersIndex]] = args[argsIndex++];
  2693. }
  2694. return result;
  2695. }
  2696. /**
  2697. * Creates a `_.countBy`, `_.groupBy`, `_.indexBy`, or `_.partition` function.
  2698. *
  2699. * @private
  2700. * @param {Function} setter The function to set keys and values of the accumulator object.
  2701. * @param {Function} [initializer] The function to initialize the accumulator object.
  2702. * @returns {Function} Returns the new aggregator function.
  2703. */
  2704. function createAggregator(setter, initializer) {
  2705. return function(collection, iteratee, thisArg) {
  2706. var result = initializer ? initializer() : {};
  2707. iteratee = getCallback(iteratee, thisArg, 3);
  2708. if (isArray(collection)) {
  2709. var index = -1,
  2710. length = collection.length;
  2711. while (++index < length) {
  2712. var value = collection[index];
  2713. setter(result, value, iteratee(value, index, collection), collection);
  2714. }
  2715. } else {
  2716. baseEach(collection, function(value, key, collection) {
  2717. setter(result, value, iteratee(value, key, collection), collection);
  2718. });
  2719. }
  2720. return result;
  2721. };
  2722. }
  2723. /**
  2724. * Creates a `_.assign`, `_.defaults`, or `_.merge` function.
  2725. *
  2726. * @private
  2727. * @param {Function} assigner The function to assign values.
  2728. * @returns {Function} Returns the new assigner function.
  2729. */
  2730. function createAssigner(assigner) {
  2731. return restParam(function(object, sources) {
  2732. var index = -1,
  2733. length = object == null ? 0 : sources.length,
  2734. customizer = length > 2 ? sources[length - 2] : undefined,
  2735. guard = length > 2 ? sources[2] : undefined,
  2736. thisArg = length > 1 ? sources[length - 1] : undefined;
  2737. if (typeof customizer == 'function') {
  2738. customizer = bindCallback(customizer, thisArg, 5);
  2739. length -= 2;
  2740. } else {
  2741. customizer = typeof thisArg == 'function' ? thisArg : undefined;
  2742. length -= (customizer ? 1 : 0);
  2743. }
  2744. if (guard && isIterateeCall(sources[0], sources[1], guard)) {
  2745. customizer = length < 3 ? undefined : customizer;
  2746. length = 1;
  2747. }
  2748. while (++index < length) {
  2749. var source = sources[index];
  2750. if (source) {
  2751. assigner(object, source, customizer);
  2752. }
  2753. }
  2754. return object;
  2755. });
  2756. }
  2757. /**
  2758. * Creates a `baseEach` or `baseEachRight` function.
  2759. *
  2760. * @private
  2761. * @param {Function} eachFunc The function to iterate over a collection.
  2762. * @param {boolean} [fromRight] Specify iterating from right to left.
  2763. * @returns {Function} Returns the new base function.
  2764. */
  2765. function createBaseEach(eachFunc, fromRight) {
  2766. return function(collection, iteratee) {
  2767. var length = collection ? getLength(collection) : 0;
  2768. if (!isLength(length)) {
  2769. return eachFunc(collection, iteratee);
  2770. }
  2771. var index = fromRight ? length : -1,
  2772. iterable = toObject(collection);
  2773. while ((fromRight ? index-- : ++index < length)) {
  2774. if (iteratee(iterable[index], index, iterable) === false) {
  2775. break;
  2776. }
  2777. }
  2778. return collection;
  2779. };
  2780. }
  2781. /**
  2782. * Creates a base function for `_.forIn` or `_.forInRight`.
  2783. *
  2784. * @private
  2785. * @param {boolean} [fromRight] Specify iterating from right to left.
  2786. * @returns {Function} Returns the new base function.
  2787. */
  2788. function createBaseFor(fromRight) {
  2789. return function(object, iteratee, keysFunc) {
  2790. var iterable = toObject(object),
  2791. props = keysFunc(object),
  2792. length = props.length,
  2793. index = fromRight ? length : -1;
  2794. while ((fromRight ? index-- : ++index < length)) {
  2795. var key = props[index];
  2796. if (iteratee(iterable[key], key, iterable) === false) {
  2797. break;
  2798. }
  2799. }
  2800. return object;
  2801. };
  2802. }
  2803. /**
  2804. * Creates a function that wraps `func` and invokes it with the `this`
  2805. * binding of `thisArg`.
  2806. *
  2807. * @private
  2808. * @param {Function} func The function to bind.
  2809. * @param {*} [thisArg] The `this` binding of `func`.
  2810. * @returns {Function} Returns the new bound function.
  2811. */
  2812. function createBindWrapper(func, thisArg) {
  2813. var Ctor = createCtorWrapper(func);
  2814. function wrapper() {
  2815. var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  2816. return fn.apply(thisArg, arguments);
  2817. }
  2818. return wrapper;
  2819. }
  2820. /**
  2821. * Creates a `Set` cache object to optimize linear searches of large arrays.
  2822. *
  2823. * @private
  2824. * @param {Array} [values] The values to cache.
  2825. * @returns {null|Object} Returns the new cache object if `Set` is supported, else `null`.
  2826. */
  2827. function createCache(values) {
  2828. return (nativeCreate && Set) ? new SetCache(values) : null;
  2829. }
  2830. /**
  2831. * Creates a function that produces compound words out of the words in a
  2832. * given string.
  2833. *
  2834. * @private
  2835. * @param {Function} callback The function to combine each word.
  2836. * @returns {Function} Returns the new compounder function.
  2837. */
  2838. function createCompounder(callback) {
  2839. return function(string) {
  2840. var index = -1,
  2841. array = words(deburr(string)),
  2842. length = array.length,
  2843. result = '';
  2844. while (++index < length) {
  2845. result = callback(result, array[index], index);
  2846. }
  2847. return result;
  2848. };
  2849. }
  2850. /**
  2851. * Creates a function that produces an instance of `Ctor` regardless of
  2852. * whether it was invoked as part of a `new` expression or by `call` or `apply`.
  2853. *
  2854. * @private
  2855. * @param {Function} Ctor The constructor to wrap.
  2856. * @returns {Function} Returns the new wrapped function.
  2857. */
  2858. function createCtorWrapper(Ctor) {
  2859. return function() {
  2860. // Use a `switch` statement to work with class constructors.
  2861. // See http://ecma-international.org/ecma-262/6.0/#sec-ecmascript-function-objects-call-thisargument-argumentslist
  2862. // for more details.
  2863. var args = arguments;
  2864. switch (args.length) {
  2865. case 0: return new Ctor;
  2866. case 1: return new Ctor(args[0]);
  2867. case 2: return new Ctor(args[0], args[1]);
  2868. case 3: return new Ctor(args[0], args[1], args[2]);
  2869. case 4: return new Ctor(args[0], args[1], args[2], args[3]);
  2870. case 5: return new Ctor(args[0], args[1], args[2], args[3], args[4]);
  2871. case 6: return new Ctor(args[0], args[1], args[2], args[3], args[4], args[5]);
  2872. case 7: return new Ctor(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
  2873. }
  2874. var thisBinding = baseCreate(Ctor.prototype),
  2875. result = Ctor.apply(thisBinding, args);
  2876. // Mimic the constructor's `return` behavior.
  2877. // See https://es5.github.io/#x13.2.2 for more details.
  2878. return isObject(result) ? result : thisBinding;
  2879. };
  2880. }
  2881. /**
  2882. * Creates a `_.curry` or `_.curryRight` function.
  2883. *
  2884. * @private
  2885. * @param {boolean} flag The curry bit flag.
  2886. * @returns {Function} Returns the new curry function.
  2887. */
  2888. function createCurry(flag) {
  2889. function curryFunc(func, arity, guard) {
  2890. if (guard && isIterateeCall(func, arity, guard)) {
  2891. arity = undefined;
  2892. }
  2893. var result = createWrapper(func, flag, undefined, undefined, undefined, undefined, undefined, arity);
  2894. result.placeholder = curryFunc.placeholder;
  2895. return result;
  2896. }
  2897. return curryFunc;
  2898. }
  2899. /**
  2900. * Creates a `_.defaults` or `_.defaultsDeep` function.
  2901. *
  2902. * @private
  2903. * @param {Function} assigner The function to assign values.
  2904. * @param {Function} customizer The function to customize assigned values.
  2905. * @returns {Function} Returns the new defaults function.
  2906. */
  2907. function createDefaults(assigner, customizer) {
  2908. return restParam(function(args) {
  2909. var object = args[0];
  2910. if (object == null) {
  2911. return object;
  2912. }
  2913. args.push(customizer);
  2914. return assigner.apply(undefined, args);
  2915. });
  2916. }
  2917. /**
  2918. * Creates a `_.max` or `_.min` function.
  2919. *
  2920. * @private
  2921. * @param {Function} comparator The function used to compare values.
  2922. * @param {*} exValue The initial extremum value.
  2923. * @returns {Function} Returns the new extremum function.
  2924. */
  2925. function createExtremum(comparator, exValue) {
  2926. return function(collection, iteratee, thisArg) {
  2927. if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
  2928. iteratee = undefined;
  2929. }
  2930. iteratee = getCallback(iteratee, thisArg, 3);
  2931. if (iteratee.length == 1) {
  2932. collection = isArray(collection) ? collection : toIterable(collection);
  2933. var result = arrayExtremum(collection, iteratee, comparator, exValue);
  2934. if (!(collection.length && result === exValue)) {
  2935. return result;
  2936. }
  2937. }
  2938. return baseExtremum(collection, iteratee, comparator, exValue);
  2939. };
  2940. }
  2941. /**
  2942. * Creates a `_.find` or `_.findLast` function.
  2943. *
  2944. * @private
  2945. * @param {Function} eachFunc The function to iterate over a collection.
  2946. * @param {boolean} [fromRight] Specify iterating from right to left.
  2947. * @returns {Function} Returns the new find function.
  2948. */
  2949. function createFind(eachFunc, fromRight) {
  2950. return function(collection, predicate, thisArg) {
  2951. predicate = getCallback(predicate, thisArg, 3);
  2952. if (isArray(collection)) {
  2953. var index = baseFindIndex(collection, predicate, fromRight);
  2954. return index > -1 ? collection[index] : undefined;
  2955. }
  2956. return baseFind(collection, predicate, eachFunc);
  2957. };
  2958. }
  2959. /**
  2960. * Creates a `_.findIndex` or `_.findLastIndex` function.
  2961. *
  2962. * @private
  2963. * @param {boolean} [fromRight] Specify iterating from right to left.
  2964. * @returns {Function} Returns the new find function.
  2965. */
  2966. function createFindIndex(fromRight) {
  2967. return function(array, predicate, thisArg) {
  2968. if (!(array && array.length)) {
  2969. return -1;
  2970. }
  2971. predicate = getCallback(predicate, thisArg, 3);
  2972. return baseFindIndex(array, predicate, fromRight);
  2973. };
  2974. }
  2975. /**
  2976. * Creates a `_.findKey` or `_.findLastKey` function.
  2977. *
  2978. * @private
  2979. * @param {Function} objectFunc The function to iterate over an object.
  2980. * @returns {Function} Returns the new find function.
  2981. */
  2982. function createFindKey(objectFunc) {
  2983. return function(object, predicate, thisArg) {
  2984. predicate = getCallback(predicate, thisArg, 3);
  2985. return baseFind(object, predicate, objectFunc, true);
  2986. };
  2987. }
  2988. /**
  2989. * Creates a `_.flow` or `_.flowRight` function.
  2990. *
  2991. * @private
  2992. * @param {boolean} [fromRight] Specify iterating from right to left.
  2993. * @returns {Function} Returns the new flow function.
  2994. */
  2995. function createFlow(fromRight) {
  2996. return function() {
  2997. var wrapper,
  2998. length = arguments.length,
  2999. index = fromRight ? length : -1,
  3000. leftIndex = 0,
  3001. funcs = Array(length);
  3002. while ((fromRight ? index-- : ++index < length)) {
  3003. var func = funcs[leftIndex++] = arguments[index];
  3004. if (typeof func != 'function') {
  3005. throw new TypeError(FUNC_ERROR_TEXT);
  3006. }
  3007. if (!wrapper && LodashWrapper.prototype.thru && getFuncName(func) == 'wrapper') {
  3008. wrapper = new LodashWrapper([], true);
  3009. }
  3010. }
  3011. index = wrapper ? -1 : length;
  3012. while (++index < length) {
  3013. func = funcs[index];
  3014. var funcName = getFuncName(func),
  3015. data = funcName == 'wrapper' ? getData(func) : undefined;
  3016. if (data && isLaziable(data[0]) && data[1] == (ARY_FLAG | CURRY_FLAG | PARTIAL_FLAG | REARG_FLAG) && !data[4].length && data[9] == 1) {
  3017. wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]);
  3018. } else {
  3019. wrapper = (func.length == 1 && isLaziable(func)) ? wrapper[funcName]() : wrapper.thru(func);
  3020. }
  3021. }
  3022. return function() {
  3023. var args = arguments,
  3024. value = args[0];
  3025. if (wrapper && args.length == 1 && isArray(value) && value.length >= LARGE_ARRAY_SIZE) {
  3026. return wrapper.plant(value).value();
  3027. }
  3028. var index = 0,
  3029. result = length ? funcs[index].apply(this, args) : value;
  3030. while (++index < length) {
  3031. result = funcs[index].call(this, result);
  3032. }
  3033. return result;
  3034. };
  3035. };
  3036. }
  3037. /**
  3038. * Creates a function for `_.forEach` or `_.forEachRight`.
  3039. *
  3040. * @private
  3041. * @param {Function} arrayFunc The function to iterate over an array.
  3042. * @param {Function} eachFunc The function to iterate over a collection.
  3043. * @returns {Function} Returns the new each function.
  3044. */
  3045. function createForEach(arrayFunc, eachFunc) {
  3046. return function(collection, iteratee, thisArg) {
  3047. return (typeof iteratee == 'function' && thisArg === undefined && isArray(collection))
  3048. ? arrayFunc(collection, iteratee)
  3049. : eachFunc(collection, bindCallback(iteratee, thisArg, 3));
  3050. };
  3051. }
  3052. /**
  3053. * Creates a function for `_.forIn` or `_.forInRight`.
  3054. *
  3055. * @private
  3056. * @param {Function} objectFunc The function to iterate over an object.
  3057. * @returns {Function} Returns the new each function.
  3058. */
  3059. function createForIn(objectFunc) {
  3060. return function(object, iteratee, thisArg) {
  3061. if (typeof iteratee != 'function' || thisArg !== undefined) {
  3062. iteratee = bindCallback(iteratee, thisArg, 3);
  3063. }
  3064. return objectFunc(object, iteratee, keysIn);
  3065. };
  3066. }
  3067. /**
  3068. * Creates a function for `_.forOwn` or `_.forOwnRight`.
  3069. *
  3070. * @private
  3071. * @param {Function} objectFunc The function to iterate over an object.
  3072. * @returns {Function} Returns the new each function.
  3073. */
  3074. function createForOwn(objectFunc) {
  3075. return function(object, iteratee, thisArg) {
  3076. if (typeof iteratee != 'function' || thisArg !== undefined) {
  3077. iteratee = bindCallback(iteratee, thisArg, 3);
  3078. }
  3079. return objectFunc(object, iteratee);
  3080. };
  3081. }
  3082. /**
  3083. * Creates a function for `_.mapKeys` or `_.mapValues`.
  3084. *
  3085. * @private
  3086. * @param {boolean} [isMapKeys] Specify mapping keys instead of values.
  3087. * @returns {Function} Returns the new map function.
  3088. */
  3089. function createObjectMapper(isMapKeys) {
  3090. return function(object, iteratee, thisArg) {
  3091. var result = {};
  3092. iteratee = getCallback(iteratee, thisArg, 3);
  3093. baseForOwn(object, function(value, key, object) {
  3094. var mapped = iteratee(value, key, object);
  3095. key = isMapKeys ? mapped : key;
  3096. value = isMapKeys ? value : mapped;
  3097. result[key] = value;
  3098. });
  3099. return result;
  3100. };
  3101. }
  3102. /**
  3103. * Creates a function for `_.padLeft` or `_.padRight`.
  3104. *
  3105. * @private
  3106. * @param {boolean} [fromRight] Specify padding from the right.
  3107. * @returns {Function} Returns the new pad function.
  3108. */
  3109. function createPadDir(fromRight) {
  3110. return function(string, length, chars) {
  3111. string = baseToString(string);
  3112. return (fromRight ? string : '') + createPadding(string, length, chars) + (fromRight ? '' : string);
  3113. };
  3114. }
  3115. /**
  3116. * Creates a `_.partial` or `_.partialRight` function.
  3117. *
  3118. * @private
  3119. * @param {boolean} flag The partial bit flag.
  3120. * @returns {Function} Returns the new partial function.
  3121. */
  3122. function createPartial(flag) {
  3123. var partialFunc = restParam(function(func, partials) {
  3124. var holders = replaceHolders(partials, partialFunc.placeholder);
  3125. return createWrapper(func, flag, undefined, partials, holders);
  3126. });
  3127. return partialFunc;
  3128. }
  3129. /**
  3130. * Creates a function for `_.reduce` or `_.reduceRight`.
  3131. *
  3132. * @private
  3133. * @param {Function} arrayFunc The function to iterate over an array.
  3134. * @param {Function} eachFunc The function to iterate over a collection.
  3135. * @returns {Function} Returns the new each function.
  3136. */
  3137. function createReduce(arrayFunc, eachFunc) {
  3138. return function(collection, iteratee, accumulator, thisArg) {
  3139. var initFromArray = arguments.length < 3;
  3140. return (typeof iteratee == 'function' && thisArg === undefined && isArray(collection))
  3141. ? arrayFunc(collection, iteratee, accumulator, initFromArray)
  3142. : baseReduce(collection, getCallback(iteratee, thisArg, 4), accumulator, initFromArray, eachFunc);
  3143. };
  3144. }
  3145. /**
  3146. * Creates a function that wraps `func` and invokes it with optional `this`
  3147. * binding of, partial application, and currying.
  3148. *
  3149. * @private
  3150. * @param {Function|string} func The function or method name to reference.
  3151. * @param {number} bitmask The bitmask of flags. See `createWrapper` for more details.
  3152. * @param {*} [thisArg] The `this` binding of `func`.
  3153. * @param {Array} [partials] The arguments to prepend to those provided to the new function.
  3154. * @param {Array} [holders] The `partials` placeholder indexes.
  3155. * @param {Array} [partialsRight] The arguments to append to those provided to the new function.
  3156. * @param {Array} [holdersRight] The `partialsRight` placeholder indexes.
  3157. * @param {Array} [argPos] The argument positions of the new function.
  3158. * @param {number} [ary] The arity cap of `func`.
  3159. * @param {number} [arity] The arity of `func`.
  3160. * @returns {Function} Returns the new wrapped function.
  3161. */
  3162. function createHybridWrapper(func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity) {
  3163. var isAry = bitmask & ARY_FLAG,
  3164. isBind = bitmask & BIND_FLAG,
  3165. isBindKey = bitmask & BIND_KEY_FLAG,
  3166. isCurry = bitmask & CURRY_FLAG,
  3167. isCurryBound = bitmask & CURRY_BOUND_FLAG,
  3168. isCurryRight = bitmask & CURRY_RIGHT_FLAG,
  3169. Ctor = isBindKey ? undefined : createCtorWrapper(func);
  3170. function wrapper() {
  3171. // Avoid `arguments` object use disqualifying optimizations by
  3172. // converting it to an array before providing it to other functions.
  3173. var length = arguments.length,
  3174. index = length,
  3175. args = Array(length);
  3176. while (index--) {
  3177. args[index] = arguments[index];
  3178. }
  3179. if (partials) {
  3180. args = composeArgs(args, partials, holders);
  3181. }
  3182. if (partialsRight) {
  3183. args = composeArgsRight(args, partialsRight, holdersRight);
  3184. }
  3185. if (isCurry || isCurryRight) {
  3186. var placeholder = wrapper.placeholder,
  3187. argsHolders = replaceHolders(args, placeholder);
  3188. length -= argsHolders.length;
  3189. if (length < arity) {
  3190. var newArgPos = argPos ? arrayCopy(argPos) : undefined,
  3191. newArity = nativeMax(arity - length, 0),
  3192. newsHolders = isCurry ? argsHolders : undefined,
  3193. newHoldersRight = isCurry ? undefined : argsHolders,
  3194. newPartials = isCurry ? args : undefined,
  3195. newPartialsRight = isCurry ? undefined : args;
  3196. bitmask |= (isCurry ? PARTIAL_FLAG : PARTIAL_RIGHT_FLAG);
  3197. bitmask &= ~(isCurry ? PARTIAL_RIGHT_FLAG : PARTIAL_FLAG);
  3198. if (!isCurryBound) {
  3199. bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG);
  3200. }
  3201. var newData = [func, bitmask, thisArg, newPartials, newsHolders, newPartialsRight, newHoldersRight, newArgPos, ary, newArity],
  3202. result = createHybridWrapper.apply(undefined, newData);
  3203. if (isLaziable(func)) {
  3204. setData(result, newData);
  3205. }
  3206. result.placeholder = placeholder;
  3207. return result;
  3208. }
  3209. }
  3210. var thisBinding = isBind ? thisArg : this,
  3211. fn = isBindKey ? thisBinding[func] : func;
  3212. if (argPos) {
  3213. args = reorder(args, argPos);
  3214. }
  3215. if (isAry && ary < args.length) {
  3216. args.length = ary;
  3217. }
  3218. if (this && this !== root && this instanceof wrapper) {
  3219. fn = Ctor || createCtorWrapper(func);
  3220. }
  3221. return fn.apply(thisBinding, args);
  3222. }
  3223. return wrapper;
  3224. }
  3225. /**
  3226. * Creates the padding required for `string` based on the given `length`.
  3227. * The `chars` string is truncated if the number of characters exceeds `length`.
  3228. *
  3229. * @private
  3230. * @param {string} string The string to create padding for.
  3231. * @param {number} [length=0] The padding length.
  3232. * @param {string} [chars=' '] The string used as padding.
  3233. * @returns {string} Returns the pad for `string`.
  3234. */
  3235. function createPadding(string, length, chars) {
  3236. var strLength = string.length;
  3237. length = +length;
  3238. if (strLength >= length || !nativeIsFinite(length)) {
  3239. return '';
  3240. }
  3241. var padLength = length - strLength;
  3242. chars = chars == null ? ' ' : (chars + '');
  3243. return repeat(chars, nativeCeil(padLength / chars.length)).slice(0, padLength);
  3244. }
  3245. /**
  3246. * Creates a function that wraps `func` and invokes it with the optional `this`
  3247. * binding of `thisArg` and the `partials` prepended to those provided to
  3248. * the wrapper.
  3249. *
  3250. * @private
  3251. * @param {Function} func The function to partially apply arguments to.
  3252. * @param {number} bitmask The bitmask of flags. See `createWrapper` for more details.
  3253. * @param {*} thisArg The `this` binding of `func`.
  3254. * @param {Array} partials The arguments to prepend to those provided to the new function.
  3255. * @returns {Function} Returns the new bound function.
  3256. */
  3257. function createPartialWrapper(func, bitmask, thisArg, partials) {
  3258. var isBind = bitmask & BIND_FLAG,
  3259. Ctor = createCtorWrapper(func);
  3260. function wrapper() {
  3261. // Avoid `arguments` object use disqualifying optimizations by
  3262. // converting it to an array before providing it `func`.
  3263. var argsIndex = -1,
  3264. argsLength = arguments.length,
  3265. leftIndex = -1,
  3266. leftLength = partials.length,
  3267. args = Array(leftLength + argsLength);
  3268. while (++leftIndex < leftLength) {
  3269. args[leftIndex] = partials[leftIndex];
  3270. }
  3271. while (argsLength--) {
  3272. args[leftIndex++] = arguments[++argsIndex];
  3273. }
  3274. var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  3275. return fn.apply(isBind ? thisArg : this, args);
  3276. }
  3277. return wrapper;
  3278. }
  3279. /**
  3280. * Creates a `_.ceil`, `_.floor`, or `_.round` function.
  3281. *
  3282. * @private
  3283. * @param {string} methodName The name of the `Math` method to use when rounding.
  3284. * @returns {Function} Returns the new round function.
  3285. */
  3286. function createRound(methodName) {
  3287. var func = Math[methodName];
  3288. return function(number, precision) {
  3289. precision = precision === undefined ? 0 : (+precision || 0);
  3290. if (precision) {
  3291. precision = pow(10, precision);
  3292. return func(number * precision) / precision;
  3293. }
  3294. return func(number);
  3295. };
  3296. }
  3297. /**
  3298. * Creates a `_.sortedIndex` or `_.sortedLastIndex` function.
  3299. *
  3300. * @private
  3301. * @param {boolean} [retHighest] Specify returning the highest qualified index.
  3302. * @returns {Function} Returns the new index function.
  3303. */
  3304. function createSortedIndex(retHighest) {
  3305. return function(array, value, iteratee, thisArg) {
  3306. var callback = getCallback(iteratee);
  3307. return (iteratee == null && callback === baseCallback)
  3308. ? binaryIndex(array, value, retHighest)
  3309. : binaryIndexBy(array, value, callback(iteratee, thisArg, 1), retHighest);
  3310. };
  3311. }
  3312. /**
  3313. * Creates a function that either curries or invokes `func` with optional
  3314. * `this` binding and partially applied arguments.
  3315. *
  3316. * @private
  3317. * @param {Function|string} func The function or method name to reference.
  3318. * @param {number} bitmask The bitmask of flags.
  3319. * The bitmask may be composed of the following flags:
  3320. * 1 - `_.bind`
  3321. * 2 - `_.bindKey`
  3322. * 4 - `_.curry` or `_.curryRight` of a bound function
  3323. * 8 - `_.curry`
  3324. * 16 - `_.curryRight`
  3325. * 32 - `_.partial`
  3326. * 64 - `_.partialRight`
  3327. * 128 - `_.rearg`
  3328. * 256 - `_.ary`
  3329. * @param {*} [thisArg] The `this` binding of `func`.
  3330. * @param {Array} [partials] The arguments to be partially applied.
  3331. * @param {Array} [holders] The `partials` placeholder indexes.
  3332. * @param {Array} [argPos] The argument positions of the new function.
  3333. * @param {number} [ary] The arity cap of `func`.
  3334. * @param {number} [arity] The arity of `func`.
  3335. * @returns {Function} Returns the new wrapped function.
  3336. */
  3337. function createWrapper(func, bitmask, thisArg, partials, holders, argPos, ary, arity) {
  3338. var isBindKey = bitmask & BIND_KEY_FLAG;
  3339. if (!isBindKey && typeof func != 'function') {
  3340. throw new TypeError(FUNC_ERROR_TEXT);
  3341. }
  3342. var length = partials ? partials.length : 0;
  3343. if (!length) {
  3344. bitmask &= ~(PARTIAL_FLAG | PARTIAL_RIGHT_FLAG);
  3345. partials = holders = undefined;
  3346. }
  3347. length -= (holders ? holders.length : 0);
  3348. if (bitmask & PARTIAL_RIGHT_FLAG) {
  3349. var partialsRight = partials,
  3350. holdersRight = holders;
  3351. partials = holders = undefined;
  3352. }
  3353. var data = isBindKey ? undefined : getData(func),
  3354. newData = [func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity];
  3355. if (data) {
  3356. mergeData(newData, data);
  3357. bitmask = newData[1];
  3358. arity = newData[9];
  3359. }
  3360. newData[9] = arity == null
  3361. ? (isBindKey ? 0 : func.length)
  3362. : (nativeMax(arity - length, 0) || 0);
  3363. if (bitmask == BIND_FLAG) {
  3364. var result = createBindWrapper(newData[0], newData[2]);
  3365. } else if ((bitmask == PARTIAL_FLAG || bitmask == (BIND_FLAG | PARTIAL_FLAG)) && !newData[4].length) {
  3366. result = createPartialWrapper.apply(undefined, newData);
  3367. } else {
  3368. result = createHybridWrapper.apply(undefined, newData);
  3369. }
  3370. var setter = data ? baseSetData : setData;
  3371. return setter(result, newData);
  3372. }
  3373. /**
  3374. * A specialized version of `baseIsEqualDeep` for arrays with support for
  3375. * partial deep comparisons.
  3376. *
  3377. * @private
  3378. * @param {Array} array The array to compare.
  3379. * @param {Array} other The other array to compare.
  3380. * @param {Function} equalFunc The function to determine equivalents of values.
  3381. * @param {Function} [customizer] The function to customize comparing arrays.
  3382. * @param {boolean} [isLoose] Specify performing partial comparisons.
  3383. * @param {Array} [stackA] Tracks traversed `value` objects.
  3384. * @param {Array} [stackB] Tracks traversed `other` objects.
  3385. * @returns {boolean} Returns `true` if the arrays are equivalent, else `false`.
  3386. */
  3387. function equalArrays(array, other, equalFunc, customizer, isLoose, stackA, stackB) {
  3388. var index = -1,
  3389. arrLength = array.length,
  3390. othLength = other.length;
  3391. if (arrLength != othLength && !(isLoose && othLength > arrLength)) {
  3392. return false;
  3393. }
  3394. // Ignore non-index properties.
  3395. while (++index < arrLength) {
  3396. var arrValue = array[index],
  3397. othValue = other[index],
  3398. result = customizer ? customizer(isLoose ? othValue : arrValue, isLoose ? arrValue : othValue, index) : undefined;
  3399. if (result !== undefined) {
  3400. if (result) {
  3401. continue;
  3402. }
  3403. return false;
  3404. }
  3405. // Recursively compare arrays (susceptible to call stack limits).
  3406. if (isLoose) {
  3407. if (!arraySome(other, function(othValue) {
  3408. return arrValue === othValue || equalFunc(arrValue, othValue, customizer, isLoose, stackA, stackB);
  3409. })) {
  3410. return false;
  3411. }
  3412. } else if (!(arrValue === othValue || equalFunc(arrValue, othValue, customizer, isLoose, stackA, stackB))) {
  3413. return false;
  3414. }
  3415. }
  3416. return true;
  3417. }
  3418. /**
  3419. * A specialized version of `baseIsEqualDeep` for comparing objects of
  3420. * the same `toStringTag`.
  3421. *
  3422. * **Note:** This function only supports comparing values with tags of
  3423. * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
  3424. *
  3425. * @private
  3426. * @param {Object} object The object to compare.
  3427. * @param {Object} other The other object to compare.
  3428. * @param {string} tag The `toStringTag` of the objects to compare.
  3429. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  3430. */
  3431. function equalByTag(object, other, tag) {
  3432. switch (tag) {
  3433. case boolTag:
  3434. case dateTag:
  3435. // Coerce dates and booleans to numbers, dates to milliseconds and booleans
  3436. // to `1` or `0` treating invalid dates coerced to `NaN` as not equal.
  3437. return +object == +other;
  3438. case errorTag:
  3439. return object.name == other.name && object.message == other.message;
  3440. case numberTag:
  3441. // Treat `NaN` vs. `NaN` as equal.
  3442. return (object != +object)
  3443. ? other != +other
  3444. : object == +other;
  3445. case regexpTag:
  3446. case stringTag:
  3447. // Coerce regexes to strings and treat strings primitives and string
  3448. // objects as equal. See https://es5.github.io/#x15.10.6.4 for more details.
  3449. return object == (other + '');
  3450. }
  3451. return false;
  3452. }
  3453. /**
  3454. * A specialized version of `baseIsEqualDeep` for objects with support for
  3455. * partial deep comparisons.
  3456. *
  3457. * @private
  3458. * @param {Object} object The object to compare.
  3459. * @param {Object} other The other object to compare.
  3460. * @param {Function} equalFunc The function to determine equivalents of values.
  3461. * @param {Function} [customizer] The function to customize comparing values.
  3462. * @param {boolean} [isLoose] Specify performing partial comparisons.
  3463. * @param {Array} [stackA] Tracks traversed `value` objects.
  3464. * @param {Array} [stackB] Tracks traversed `other` objects.
  3465. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  3466. */
  3467. function equalObjects(object, other, equalFunc, customizer, isLoose, stackA, stackB) {
  3468. var objProps = keys(object),
  3469. objLength = objProps.length,
  3470. othProps = keys(other),
  3471. othLength = othProps.length;
  3472. if (objLength != othLength && !isLoose) {
  3473. return false;
  3474. }
  3475. var index = objLength;
  3476. while (index--) {
  3477. var key = objProps[index];
  3478. if (!(isLoose ? key in other : hasOwnProperty.call(other, key))) {
  3479. return false;
  3480. }
  3481. }
  3482. var skipCtor = isLoose;
  3483. while (++index < objLength) {
  3484. key = objProps[index];
  3485. var objValue = object[key],
  3486. othValue = other[key],
  3487. result = customizer ? customizer(isLoose ? othValue : objValue, isLoose? objValue : othValue, key) : undefined;
  3488. // Recursively compare objects (susceptible to call stack limits).
  3489. if (!(result === undefined ? equalFunc(objValue, othValue, customizer, isLoose, stackA, stackB) : result)) {
  3490. return false;
  3491. }
  3492. skipCtor || (skipCtor = key == 'constructor');
  3493. }
  3494. if (!skipCtor) {
  3495. var objCtor = object.constructor,
  3496. othCtor = other.constructor;
  3497. // Non `Object` object instances with different constructors are not equal.
  3498. if (objCtor != othCtor &&
  3499. ('constructor' in object && 'constructor' in other) &&
  3500. !(typeof objCtor == 'function' && objCtor instanceof objCtor &&
  3501. typeof othCtor == 'function' && othCtor instanceof othCtor)) {
  3502. return false;
  3503. }
  3504. }
  3505. return true;
  3506. }
  3507. /**
  3508. * Gets the appropriate "callback" function. If the `_.callback` method is
  3509. * customized this function returns the custom method, otherwise it returns
  3510. * the `baseCallback` function. If arguments are provided the chosen function
  3511. * is invoked with them and its result is returned.
  3512. *
  3513. * @private
  3514. * @returns {Function} Returns the chosen function or its result.
  3515. */
  3516. function getCallback(func, thisArg, argCount) {
  3517. var result = lodash.callback || callback;
  3518. result = result === callback ? baseCallback : result;
  3519. return argCount ? result(func, thisArg, argCount) : result;
  3520. }
  3521. /**
  3522. * Gets metadata for `func`.
  3523. *
  3524. * @private
  3525. * @param {Function} func The function to query.
  3526. * @returns {*} Returns the metadata for `func`.
  3527. */
  3528. var getData = !metaMap ? noop : function(func) {
  3529. return metaMap.get(func);
  3530. };
  3531. /**
  3532. * Gets the name of `func`.
  3533. *
  3534. * @private
  3535. * @param {Function} func The function to query.
  3536. * @returns {string} Returns the function name.
  3537. */
  3538. function getFuncName(func) {
  3539. var result = func.name,
  3540. array = realNames[result],
  3541. length = array ? array.length : 0;
  3542. while (length--) {
  3543. var data = array[length],
  3544. otherFunc = data.func;
  3545. if (otherFunc == null || otherFunc == func) {
  3546. return data.name;
  3547. }
  3548. }
  3549. return result;
  3550. }
  3551. /**
  3552. * Gets the appropriate "indexOf" function. If the `_.indexOf` method is
  3553. * customized this function returns the custom method, otherwise it returns
  3554. * the `baseIndexOf` function. If arguments are provided the chosen function
  3555. * is invoked with them and its result is returned.
  3556. *
  3557. * @private
  3558. * @returns {Function|number} Returns the chosen function or its result.
  3559. */
  3560. function getIndexOf(collection, target, fromIndex) {
  3561. var result = lodash.indexOf || indexOf;
  3562. result = result === indexOf ? baseIndexOf : result;
  3563. return collection ? result(collection, target, fromIndex) : result;
  3564. }
  3565. /**
  3566. * Gets the "length" property value of `object`.
  3567. *
  3568. * **Note:** This function is used to avoid a [JIT bug](https://bugs.webkit.org/show_bug.cgi?id=142792)
  3569. * that affects Safari on at least iOS 8.1-8.3 ARM64.
  3570. *
  3571. * @private
  3572. * @param {Object} object The object to query.
  3573. * @returns {*} Returns the "length" value.
  3574. */
  3575. var getLength = baseProperty('length');
  3576. /**
  3577. * Gets the propery names, values, and compare flags of `object`.
  3578. *
  3579. * @private
  3580. * @param {Object} object The object to query.
  3581. * @returns {Array} Returns the match data of `object`.
  3582. */
  3583. function getMatchData(object) {
  3584. var result = pairs(object),
  3585. length = result.length;
  3586. while (length--) {
  3587. result[length][2] = isStrictComparable(result[length][1]);
  3588. }
  3589. return result;
  3590. }
  3591. /**
  3592. * Gets the native function at `key` of `object`.
  3593. *
  3594. * @private
  3595. * @param {Object} object The object to query.
  3596. * @param {string} key The key of the method to get.
  3597. * @returns {*} Returns the function if it's native, else `undefined`.
  3598. */
  3599. function getNative(object, key) {
  3600. var value = object == null ? undefined : object[key];
  3601. return isNative(value) ? value : undefined;
  3602. }
  3603. /**
  3604. * Gets the view, applying any `transforms` to the `start` and `end` positions.
  3605. *
  3606. * @private
  3607. * @param {number} start The start of the view.
  3608. * @param {number} end The end of the view.
  3609. * @param {Array} transforms The transformations to apply to the view.
  3610. * @returns {Object} Returns an object containing the `start` and `end`
  3611. * positions of the view.
  3612. */
  3613. function getView(start, end, transforms) {
  3614. var index = -1,
  3615. length = transforms.length;
  3616. while (++index < length) {
  3617. var data = transforms[index],
  3618. size = data.size;
  3619. switch (data.type) {
  3620. case 'drop': start += size; break;
  3621. case 'dropRight': end -= size; break;
  3622. case 'take': end = nativeMin(end, start + size); break;
  3623. case 'takeRight': start = nativeMax(start, end - size); break;
  3624. }
  3625. }
  3626. return { 'start': start, 'end': end };
  3627. }
  3628. /**
  3629. * Initializes an array clone.
  3630. *
  3631. * @private
  3632. * @param {Array} array The array to clone.
  3633. * @returns {Array} Returns the initialized clone.
  3634. */
  3635. function initCloneArray(array) {
  3636. var length = array.length,
  3637. result = new array.constructor(length);
  3638. // Add array properties assigned by `RegExp#exec`.
  3639. if (length && typeof array[0] == 'string' && hasOwnProperty.call(array, 'index')) {
  3640. result.index = array.index;
  3641. result.input = array.input;
  3642. }
  3643. return result;
  3644. }
  3645. /**
  3646. * Initializes an object clone.
  3647. *
  3648. * @private
  3649. * @param {Object} object The object to clone.
  3650. * @returns {Object} Returns the initialized clone.
  3651. */
  3652. function initCloneObject(object) {
  3653. var Ctor = object.constructor;
  3654. if (!(typeof Ctor == 'function' && Ctor instanceof Ctor)) {
  3655. Ctor = Object;
  3656. }
  3657. return new Ctor;
  3658. }
  3659. /**
  3660. * Initializes an object clone based on its `toStringTag`.
  3661. *
  3662. * **Note:** This function only supports cloning values with tags of
  3663. * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
  3664. *
  3665. * @private
  3666. * @param {Object} object The object to clone.
  3667. * @param {string} tag The `toStringTag` of the object to clone.
  3668. * @param {boolean} [isDeep] Specify a deep clone.
  3669. * @returns {Object} Returns the initialized clone.
  3670. */
  3671. function initCloneByTag(object, tag, isDeep) {
  3672. var Ctor = object.constructor;
  3673. switch (tag) {
  3674. case arrayBufferTag:
  3675. return bufferClone(object);
  3676. case boolTag:
  3677. case dateTag:
  3678. return new Ctor(+object);
  3679. case float32Tag: case float64Tag:
  3680. case int8Tag: case int16Tag: case int32Tag:
  3681. case uint8Tag: case uint8ClampedTag: case uint16Tag: case uint32Tag:
  3682. var buffer = object.buffer;
  3683. return new Ctor(isDeep ? bufferClone(buffer) : buffer, object.byteOffset, object.length);
  3684. case numberTag:
  3685. case stringTag:
  3686. return new Ctor(object);
  3687. case regexpTag:
  3688. var result = new Ctor(object.source, reFlags.exec(object));
  3689. result.lastIndex = object.lastIndex;
  3690. }
  3691. return result;
  3692. }
  3693. /**
  3694. * Invokes the method at `path` on `object`.
  3695. *
  3696. * @private
  3697. * @param {Object} object The object to query.
  3698. * @param {Array|string} path The path of the method to invoke.
  3699. * @param {Array} args The arguments to invoke the method with.
  3700. * @returns {*} Returns the result of the invoked method.
  3701. */
  3702. function invokePath(object, path, args) {
  3703. if (object != null && !isKey(path, object)) {
  3704. path = toPath(path);
  3705. object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  3706. path = last(path);
  3707. }
  3708. var func = object == null ? object : object[path];
  3709. return func == null ? undefined : func.apply(object, args);
  3710. }
  3711. /**
  3712. * Checks if `value` is array-like.
  3713. *
  3714. * @private
  3715. * @param {*} value The value to check.
  3716. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  3717. */
  3718. function isArrayLike(value) {
  3719. return value != null && isLength(getLength(value));
  3720. }
  3721. /**
  3722. * Checks if `value` is a valid array-like index.
  3723. *
  3724. * @private
  3725. * @param {*} value The value to check.
  3726. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  3727. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  3728. */
  3729. function isIndex(value, length) {
  3730. value = (typeof value == 'number' || reIsUint.test(value)) ? +value : -1;
  3731. length = length == null ? MAX_SAFE_INTEGER : length;
  3732. return value > -1 && value % 1 == 0 && value < length;
  3733. }
  3734. /**
  3735. * Checks if the provided arguments are from an iteratee call.
  3736. *
  3737. * @private
  3738. * @param {*} value The potential iteratee value argument.
  3739. * @param {*} index The potential iteratee index or key argument.
  3740. * @param {*} object The potential iteratee object argument.
  3741. * @returns {boolean} Returns `true` if the arguments are from an iteratee call, else `false`.
  3742. */
  3743. function isIterateeCall(value, index, object) {
  3744. if (!isObject(object)) {
  3745. return false;
  3746. }
  3747. var type = typeof index;
  3748. if (type == 'number'
  3749. ? (isArrayLike(object) && isIndex(index, object.length))
  3750. : (type == 'string' && index in object)) {
  3751. var other = object[index];
  3752. return value === value ? (value === other) : (other !== other);
  3753. }
  3754. return false;
  3755. }
  3756. /**
  3757. * Checks if `value` is a property name and not a property path.
  3758. *
  3759. * @private
  3760. * @param {*} value The value to check.
  3761. * @param {Object} [object] The object to query keys on.
  3762. * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
  3763. */
  3764. function isKey(value, object) {
  3765. var type = typeof value;
  3766. if ((type == 'string' && reIsPlainProp.test(value)) || type == 'number') {
  3767. return true;
  3768. }
  3769. if (isArray(value)) {
  3770. return false;
  3771. }
  3772. var result = !reIsDeepProp.test(value);
  3773. return result || (object != null && value in toObject(object));
  3774. }
  3775. /**
  3776. * Checks if `func` has a lazy counterpart.
  3777. *
  3778. * @private
  3779. * @param {Function} func The function to check.
  3780. * @returns {boolean} Returns `true` if `func` has a lazy counterpart, else `false`.
  3781. */
  3782. function isLaziable(func) {
  3783. var funcName = getFuncName(func);
  3784. if (!(funcName in LazyWrapper.prototype)) {
  3785. return false;
  3786. }
  3787. var other = lodash[funcName];
  3788. if (func === other) {
  3789. return true;
  3790. }
  3791. var data = getData(other);
  3792. return !!data && func === data[0];
  3793. }
  3794. /**
  3795. * Checks if `value` is a valid array-like length.
  3796. *
  3797. * **Note:** This function is based on [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
  3798. *
  3799. * @private
  3800. * @param {*} value The value to check.
  3801. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  3802. */
  3803. function isLength(value) {
  3804. return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  3805. }
  3806. /**
  3807. * Checks if `value` is suitable for strict equality comparisons, i.e. `===`.
  3808. *
  3809. * @private
  3810. * @param {*} value The value to check.
  3811. * @returns {boolean} Returns `true` if `value` if suitable for strict
  3812. * equality comparisons, else `false`.
  3813. */
  3814. function isStrictComparable(value) {
  3815. return value === value && !isObject(value);
  3816. }
  3817. /**
  3818. * Merges the function metadata of `source` into `data`.
  3819. *
  3820. * Merging metadata reduces the number of wrappers required to invoke a function.
  3821. * This is possible because methods like `_.bind`, `_.curry`, and `_.partial`
  3822. * may be applied regardless of execution order. Methods like `_.ary` and `_.rearg`
  3823. * augment function arguments, making the order in which they are executed important,
  3824. * preventing the merging of metadata. However, we make an exception for a safe
  3825. * common case where curried functions have `_.ary` and or `_.rearg` applied.
  3826. *
  3827. * @private
  3828. * @param {Array} data The destination metadata.
  3829. * @param {Array} source The source metadata.
  3830. * @returns {Array} Returns `data`.
  3831. */
  3832. function mergeData(data, source) {
  3833. var bitmask = data[1],
  3834. srcBitmask = source[1],
  3835. newBitmask = bitmask | srcBitmask,
  3836. isCommon = newBitmask < ARY_FLAG;
  3837. var isCombo =
  3838. (srcBitmask == ARY_FLAG && bitmask == CURRY_FLAG) ||
  3839. (srcBitmask == ARY_FLAG && bitmask == REARG_FLAG && data[7].length <= source[8]) ||
  3840. (srcBitmask == (ARY_FLAG | REARG_FLAG) && bitmask == CURRY_FLAG);
  3841. // Exit early if metadata can't be merged.
  3842. if (!(isCommon || isCombo)) {
  3843. return data;
  3844. }
  3845. // Use source `thisArg` if available.
  3846. if (srcBitmask & BIND_FLAG) {
  3847. data[2] = source[2];
  3848. // Set when currying a bound function.
  3849. newBitmask |= (bitmask & BIND_FLAG) ? 0 : CURRY_BOUND_FLAG;
  3850. }
  3851. // Compose partial arguments.
  3852. var value = source[3];
  3853. if (value) {
  3854. var partials = data[3];
  3855. data[3] = partials ? composeArgs(partials, value, source[4]) : arrayCopy(value);
  3856. data[4] = partials ? replaceHolders(data[3], PLACEHOLDER) : arrayCopy(source[4]);
  3857. }
  3858. // Compose partial right arguments.
  3859. value = source[5];
  3860. if (value) {
  3861. partials = data[5];
  3862. data[5] = partials ? composeArgsRight(partials, value, source[6]) : arrayCopy(value);
  3863. data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : arrayCopy(source[6]);
  3864. }
  3865. // Use source `argPos` if available.
  3866. value = source[7];
  3867. if (value) {
  3868. data[7] = arrayCopy(value);
  3869. }
  3870. // Use source `ary` if it's smaller.
  3871. if (srcBitmask & ARY_FLAG) {
  3872. data[8] = data[8] == null ? source[8] : nativeMin(data[8], source[8]);
  3873. }
  3874. // Use source `arity` if one is not provided.
  3875. if (data[9] == null) {
  3876. data[9] = source[9];
  3877. }
  3878. // Use source `func` and merge bitmasks.
  3879. data[0] = source[0];
  3880. data[1] = newBitmask;
  3881. return data;
  3882. }
  3883. /**
  3884. * Used by `_.defaultsDeep` to customize its `_.merge` use.
  3885. *
  3886. * @private
  3887. * @param {*} objectValue The destination object property value.
  3888. * @param {*} sourceValue The source object property value.
  3889. * @returns {*} Returns the value to assign to the destination object.
  3890. */
  3891. function mergeDefaults(objectValue, sourceValue) {
  3892. return objectValue === undefined ? sourceValue : merge(objectValue, sourceValue, mergeDefaults);
  3893. }
  3894. /**
  3895. * A specialized version of `_.pick` which picks `object` properties specified
  3896. * by `props`.
  3897. *
  3898. * @private
  3899. * @param {Object} object The source object.
  3900. * @param {string[]} props The property names to pick.
  3901. * @returns {Object} Returns the new object.
  3902. */
  3903. function pickByArray(object, props) {
  3904. object = toObject(object);
  3905. var index = -1,
  3906. length = props.length,
  3907. result = {};
  3908. while (++index < length) {
  3909. var key = props[index];
  3910. if (key in object) {
  3911. result[key] = object[key];
  3912. }
  3913. }
  3914. return result;
  3915. }
  3916. /**
  3917. * A specialized version of `_.pick` which picks `object` properties `predicate`
  3918. * returns truthy for.
  3919. *
  3920. * @private
  3921. * @param {Object} object The source object.
  3922. * @param {Function} predicate The function invoked per iteration.
  3923. * @returns {Object} Returns the new object.
  3924. */
  3925. function pickByCallback(object, predicate) {
  3926. var result = {};
  3927. baseForIn(object, function(value, key, object) {
  3928. if (predicate(value, key, object)) {
  3929. result[key] = value;
  3930. }
  3931. });
  3932. return result;
  3933. }
  3934. /**
  3935. * Reorder `array` according to the specified indexes where the element at
  3936. * the first index is assigned as the first element, the element at
  3937. * the second index is assigned as the second element, and so on.
  3938. *
  3939. * @private
  3940. * @param {Array} array The array to reorder.
  3941. * @param {Array} indexes The arranged array indexes.
  3942. * @returns {Array} Returns `array`.
  3943. */
  3944. function reorder(array, indexes) {
  3945. var arrLength = array.length,
  3946. length = nativeMin(indexes.length, arrLength),
  3947. oldArray = arrayCopy(array);
  3948. while (length--) {
  3949. var index = indexes[length];
  3950. array[length] = isIndex(index, arrLength) ? oldArray[index] : undefined;
  3951. }
  3952. return array;
  3953. }
  3954. /**
  3955. * Sets metadata for `func`.
  3956. *
  3957. * **Note:** If this function becomes hot, i.e. is invoked a lot in a short
  3958. * period of time, it will trip its breaker and transition to an identity function
  3959. * to avoid garbage collection pauses in V8. See [V8 issue 2070](https://code.google.com/p/v8/issues/detail?id=2070)
  3960. * for more details.
  3961. *
  3962. * @private
  3963. * @param {Function} func The function to associate metadata with.
  3964. * @param {*} data The metadata.
  3965. * @returns {Function} Returns `func`.
  3966. */
  3967. var setData = (function() {
  3968. var count = 0,
  3969. lastCalled = 0;
  3970. return function(key, value) {
  3971. var stamp = now(),
  3972. remaining = HOT_SPAN - (stamp - lastCalled);
  3973. lastCalled = stamp;
  3974. if (remaining > 0) {
  3975. if (++count >= HOT_COUNT) {
  3976. return key;
  3977. }
  3978. } else {
  3979. count = 0;
  3980. }
  3981. return baseSetData(key, value);
  3982. };
  3983. }());
  3984. /**
  3985. * A fallback implementation of `Object.keys` which creates an array of the
  3986. * own enumerable property names of `object`.
  3987. *
  3988. * @private
  3989. * @param {Object} object The object to query.
  3990. * @returns {Array} Returns the array of property names.
  3991. */
  3992. function shimKeys(object) {
  3993. var props = keysIn(object),
  3994. propsLength = props.length,
  3995. length = propsLength && object.length;
  3996. var allowIndexes = !!length && isLength(length) &&
  3997. (isArray(object) || isArguments(object));
  3998. var index = -1,
  3999. result = [];
  4000. while (++index < propsLength) {
  4001. var key = props[index];
  4002. if ((allowIndexes && isIndex(key, length)) || hasOwnProperty.call(object, key)) {
  4003. result.push(key);
  4004. }
  4005. }
  4006. return result;
  4007. }
  4008. /**
  4009. * Converts `value` to an array-like object if it's not one.
  4010. *
  4011. * @private
  4012. * @param {*} value The value to process.
  4013. * @returns {Array|Object} Returns the array-like object.
  4014. */
  4015. function toIterable(value) {
  4016. if (value == null) {
  4017. return [];
  4018. }
  4019. if (!isArrayLike(value)) {
  4020. return values(value);
  4021. }
  4022. return isObject(value) ? value : Object(value);
  4023. }
  4024. /**
  4025. * Converts `value` to an object if it's not one.
  4026. *
  4027. * @private
  4028. * @param {*} value The value to process.
  4029. * @returns {Object} Returns the object.
  4030. */
  4031. function toObject(value) {
  4032. return isObject(value) ? value : Object(value);
  4033. }
  4034. /**
  4035. * Converts `value` to property path array if it's not one.
  4036. *
  4037. * @private
  4038. * @param {*} value The value to process.
  4039. * @returns {Array} Returns the property path array.
  4040. */
  4041. function toPath(value) {
  4042. if (isArray(value)) {
  4043. return value;
  4044. }
  4045. var result = [];
  4046. baseToString(value).replace(rePropName, function(match, number, quote, string) {
  4047. result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
  4048. });
  4049. return result;
  4050. }
  4051. /**
  4052. * Creates a clone of `wrapper`.
  4053. *
  4054. * @private
  4055. * @param {Object} wrapper The wrapper to clone.
  4056. * @returns {Object} Returns the cloned wrapper.
  4057. */
  4058. function wrapperClone(wrapper) {
  4059. return wrapper instanceof LazyWrapper
  4060. ? wrapper.clone()
  4061. : new LodashWrapper(wrapper.__wrapped__, wrapper.__chain__, arrayCopy(wrapper.__actions__));
  4062. }
  4063. /*------------------------------------------------------------------------*/
  4064. /**
  4065. * Creates an array of elements split into groups the length of `size`.
  4066. * If `collection` can't be split evenly, the final chunk will be the remaining
  4067. * elements.
  4068. *
  4069. * @static
  4070. * @memberOf _
  4071. * @category Array
  4072. * @param {Array} array The array to process.
  4073. * @param {number} [size=1] The length of each chunk.
  4074. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4075. * @returns {Array} Returns the new array containing chunks.
  4076. * @example
  4077. *
  4078. * _.chunk(['a', 'b', 'c', 'd'], 2);
  4079. * // => [['a', 'b'], ['c', 'd']]
  4080. *
  4081. * _.chunk(['a', 'b', 'c', 'd'], 3);
  4082. * // => [['a', 'b', 'c'], ['d']]
  4083. */
  4084. function chunk(array, size, guard) {
  4085. if (guard ? isIterateeCall(array, size, guard) : size == null) {
  4086. size = 1;
  4087. } else {
  4088. size = nativeMax(nativeFloor(size) || 1, 1);
  4089. }
  4090. var index = 0,
  4091. length = array ? array.length : 0,
  4092. resIndex = -1,
  4093. result = Array(nativeCeil(length / size));
  4094. while (index < length) {
  4095. result[++resIndex] = baseSlice(array, index, (index += size));
  4096. }
  4097. return result;
  4098. }
  4099. /**
  4100. * Creates an array with all falsey values removed. The values `false`, `null`,
  4101. * `0`, `""`, `undefined`, and `NaN` are falsey.
  4102. *
  4103. * @static
  4104. * @memberOf _
  4105. * @category Array
  4106. * @param {Array} array The array to compact.
  4107. * @returns {Array} Returns the new array of filtered values.
  4108. * @example
  4109. *
  4110. * _.compact([0, 1, false, 2, '', 3]);
  4111. * // => [1, 2, 3]
  4112. */
  4113. function compact(array) {
  4114. var index = -1,
  4115. length = array ? array.length : 0,
  4116. resIndex = -1,
  4117. result = [];
  4118. while (++index < length) {
  4119. var value = array[index];
  4120. if (value) {
  4121. result[++resIndex] = value;
  4122. }
  4123. }
  4124. return result;
  4125. }
  4126. /**
  4127. * Creates an array of unique `array` values not included in the other
  4128. * provided arrays using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  4129. * for equality comparisons.
  4130. *
  4131. * @static
  4132. * @memberOf _
  4133. * @category Array
  4134. * @param {Array} array The array to inspect.
  4135. * @param {...Array} [values] The arrays of values to exclude.
  4136. * @returns {Array} Returns the new array of filtered values.
  4137. * @example
  4138. *
  4139. * _.difference([1, 2, 3], [4, 2]);
  4140. * // => [1, 3]
  4141. */
  4142. var difference = restParam(function(array, values) {
  4143. return (isObjectLike(array) && isArrayLike(array))
  4144. ? baseDifference(array, baseFlatten(values, false, true))
  4145. : [];
  4146. });
  4147. /**
  4148. * Creates a slice of `array` with `n` elements dropped from the beginning.
  4149. *
  4150. * @static
  4151. * @memberOf _
  4152. * @category Array
  4153. * @param {Array} array The array to query.
  4154. * @param {number} [n=1] The number of elements to drop.
  4155. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4156. * @returns {Array} Returns the slice of `array`.
  4157. * @example
  4158. *
  4159. * _.drop([1, 2, 3]);
  4160. * // => [2, 3]
  4161. *
  4162. * _.drop([1, 2, 3], 2);
  4163. * // => [3]
  4164. *
  4165. * _.drop([1, 2, 3], 5);
  4166. * // => []
  4167. *
  4168. * _.drop([1, 2, 3], 0);
  4169. * // => [1, 2, 3]
  4170. */
  4171. function drop(array, n, guard) {
  4172. var length = array ? array.length : 0;
  4173. if (!length) {
  4174. return [];
  4175. }
  4176. if (guard ? isIterateeCall(array, n, guard) : n == null) {
  4177. n = 1;
  4178. }
  4179. return baseSlice(array, n < 0 ? 0 : n);
  4180. }
  4181. /**
  4182. * Creates a slice of `array` with `n` elements dropped from the end.
  4183. *
  4184. * @static
  4185. * @memberOf _
  4186. * @category Array
  4187. * @param {Array} array The array to query.
  4188. * @param {number} [n=1] The number of elements to drop.
  4189. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4190. * @returns {Array} Returns the slice of `array`.
  4191. * @example
  4192. *
  4193. * _.dropRight([1, 2, 3]);
  4194. * // => [1, 2]
  4195. *
  4196. * _.dropRight([1, 2, 3], 2);
  4197. * // => [1]
  4198. *
  4199. * _.dropRight([1, 2, 3], 5);
  4200. * // => []
  4201. *
  4202. * _.dropRight([1, 2, 3], 0);
  4203. * // => [1, 2, 3]
  4204. */
  4205. function dropRight(array, n, guard) {
  4206. var length = array ? array.length : 0;
  4207. if (!length) {
  4208. return [];
  4209. }
  4210. if (guard ? isIterateeCall(array, n, guard) : n == null) {
  4211. n = 1;
  4212. }
  4213. n = length - (+n || 0);
  4214. return baseSlice(array, 0, n < 0 ? 0 : n);
  4215. }
  4216. /**
  4217. * Creates a slice of `array` excluding elements dropped from the end.
  4218. * Elements are dropped until `predicate` returns falsey. The predicate is
  4219. * bound to `thisArg` and invoked with three arguments: (value, index, array).
  4220. *
  4221. * If a property name is provided for `predicate` the created `_.property`
  4222. * style callback returns the property value of the given element.
  4223. *
  4224. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4225. * style callback returns `true` for elements that have a matching property
  4226. * value, else `false`.
  4227. *
  4228. * If an object is provided for `predicate` the created `_.matches` style
  4229. * callback returns `true` for elements that match the properties of the given
  4230. * object, else `false`.
  4231. *
  4232. * @static
  4233. * @memberOf _
  4234. * @category Array
  4235. * @param {Array} array The array to query.
  4236. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  4237. * per iteration.
  4238. * @param {*} [thisArg] The `this` binding of `predicate`.
  4239. * @returns {Array} Returns the slice of `array`.
  4240. * @example
  4241. *
  4242. * _.dropRightWhile([1, 2, 3], function(n) {
  4243. * return n > 1;
  4244. * });
  4245. * // => [1]
  4246. *
  4247. * var users = [
  4248. * { 'user': 'barney', 'active': true },
  4249. * { 'user': 'fred', 'active': false },
  4250. * { 'user': 'pebbles', 'active': false }
  4251. * ];
  4252. *
  4253. * // using the `_.matches` callback shorthand
  4254. * _.pluck(_.dropRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user');
  4255. * // => ['barney', 'fred']
  4256. *
  4257. * // using the `_.matchesProperty` callback shorthand
  4258. * _.pluck(_.dropRightWhile(users, 'active', false), 'user');
  4259. * // => ['barney']
  4260. *
  4261. * // using the `_.property` callback shorthand
  4262. * _.pluck(_.dropRightWhile(users, 'active'), 'user');
  4263. * // => ['barney', 'fred', 'pebbles']
  4264. */
  4265. function dropRightWhile(array, predicate, thisArg) {
  4266. return (array && array.length)
  4267. ? baseWhile(array, getCallback(predicate, thisArg, 3), true, true)
  4268. : [];
  4269. }
  4270. /**
  4271. * Creates a slice of `array` excluding elements dropped from the beginning.
  4272. * Elements are dropped until `predicate` returns falsey. The predicate is
  4273. * bound to `thisArg` and invoked with three arguments: (value, index, array).
  4274. *
  4275. * If a property name is provided for `predicate` the created `_.property`
  4276. * style callback returns the property value of the given element.
  4277. *
  4278. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4279. * style callback returns `true` for elements that have a matching property
  4280. * value, else `false`.
  4281. *
  4282. * If an object is provided for `predicate` the created `_.matches` style
  4283. * callback returns `true` for elements that have the properties of the given
  4284. * object, else `false`.
  4285. *
  4286. * @static
  4287. * @memberOf _
  4288. * @category Array
  4289. * @param {Array} array The array to query.
  4290. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  4291. * per iteration.
  4292. * @param {*} [thisArg] The `this` binding of `predicate`.
  4293. * @returns {Array} Returns the slice of `array`.
  4294. * @example
  4295. *
  4296. * _.dropWhile([1, 2, 3], function(n) {
  4297. * return n < 3;
  4298. * });
  4299. * // => [3]
  4300. *
  4301. * var users = [
  4302. * { 'user': 'barney', 'active': false },
  4303. * { 'user': 'fred', 'active': false },
  4304. * { 'user': 'pebbles', 'active': true }
  4305. * ];
  4306. *
  4307. * // using the `_.matches` callback shorthand
  4308. * _.pluck(_.dropWhile(users, { 'user': 'barney', 'active': false }), 'user');
  4309. * // => ['fred', 'pebbles']
  4310. *
  4311. * // using the `_.matchesProperty` callback shorthand
  4312. * _.pluck(_.dropWhile(users, 'active', false), 'user');
  4313. * // => ['pebbles']
  4314. *
  4315. * // using the `_.property` callback shorthand
  4316. * _.pluck(_.dropWhile(users, 'active'), 'user');
  4317. * // => ['barney', 'fred', 'pebbles']
  4318. */
  4319. function dropWhile(array, predicate, thisArg) {
  4320. return (array && array.length)
  4321. ? baseWhile(array, getCallback(predicate, thisArg, 3), true)
  4322. : [];
  4323. }
  4324. /**
  4325. * Fills elements of `array` with `value` from `start` up to, but not
  4326. * including, `end`.
  4327. *
  4328. * **Note:** This method mutates `array`.
  4329. *
  4330. * @static
  4331. * @memberOf _
  4332. * @category Array
  4333. * @param {Array} array The array to fill.
  4334. * @param {*} value The value to fill `array` with.
  4335. * @param {number} [start=0] The start position.
  4336. * @param {number} [end=array.length] The end position.
  4337. * @returns {Array} Returns `array`.
  4338. * @example
  4339. *
  4340. * var array = [1, 2, 3];
  4341. *
  4342. * _.fill(array, 'a');
  4343. * console.log(array);
  4344. * // => ['a', 'a', 'a']
  4345. *
  4346. * _.fill(Array(3), 2);
  4347. * // => [2, 2, 2]
  4348. *
  4349. * _.fill([4, 6, 8], '*', 1, 2);
  4350. * // => [4, '*', 8]
  4351. */
  4352. function fill(array, value, start, end) {
  4353. var length = array ? array.length : 0;
  4354. if (!length) {
  4355. return [];
  4356. }
  4357. if (start && typeof start != 'number' && isIterateeCall(array, value, start)) {
  4358. start = 0;
  4359. end = length;
  4360. }
  4361. return baseFill(array, value, start, end);
  4362. }
  4363. /**
  4364. * This method is like `_.find` except that it returns the index of the first
  4365. * element `predicate` returns truthy for instead of the element itself.
  4366. *
  4367. * If a property name is provided for `predicate` the created `_.property`
  4368. * style callback returns the property value of the given element.
  4369. *
  4370. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4371. * style callback returns `true` for elements that have a matching property
  4372. * value, else `false`.
  4373. *
  4374. * If an object is provided for `predicate` the created `_.matches` style
  4375. * callback returns `true` for elements that have the properties of the given
  4376. * object, else `false`.
  4377. *
  4378. * @static
  4379. * @memberOf _
  4380. * @category Array
  4381. * @param {Array} array The array to search.
  4382. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  4383. * per iteration.
  4384. * @param {*} [thisArg] The `this` binding of `predicate`.
  4385. * @returns {number} Returns the index of the found element, else `-1`.
  4386. * @example
  4387. *
  4388. * var users = [
  4389. * { 'user': 'barney', 'active': false },
  4390. * { 'user': 'fred', 'active': false },
  4391. * { 'user': 'pebbles', 'active': true }
  4392. * ];
  4393. *
  4394. * _.findIndex(users, function(chr) {
  4395. * return chr.user == 'barney';
  4396. * });
  4397. * // => 0
  4398. *
  4399. * // using the `_.matches` callback shorthand
  4400. * _.findIndex(users, { 'user': 'fred', 'active': false });
  4401. * // => 1
  4402. *
  4403. * // using the `_.matchesProperty` callback shorthand
  4404. * _.findIndex(users, 'active', false);
  4405. * // => 0
  4406. *
  4407. * // using the `_.property` callback shorthand
  4408. * _.findIndex(users, 'active');
  4409. * // => 2
  4410. */
  4411. var findIndex = createFindIndex();
  4412. /**
  4413. * This method is like `_.findIndex` except that it iterates over elements
  4414. * of `collection` from right to left.
  4415. *
  4416. * If a property name is provided for `predicate` the created `_.property`
  4417. * style callback returns the property value of the given element.
  4418. *
  4419. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4420. * style callback returns `true` for elements that have a matching property
  4421. * value, else `false`.
  4422. *
  4423. * If an object is provided for `predicate` the created `_.matches` style
  4424. * callback returns `true` for elements that have the properties of the given
  4425. * object, else `false`.
  4426. *
  4427. * @static
  4428. * @memberOf _
  4429. * @category Array
  4430. * @param {Array} array The array to search.
  4431. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  4432. * per iteration.
  4433. * @param {*} [thisArg] The `this` binding of `predicate`.
  4434. * @returns {number} Returns the index of the found element, else `-1`.
  4435. * @example
  4436. *
  4437. * var users = [
  4438. * { 'user': 'barney', 'active': true },
  4439. * { 'user': 'fred', 'active': false },
  4440. * { 'user': 'pebbles', 'active': false }
  4441. * ];
  4442. *
  4443. * _.findLastIndex(users, function(chr) {
  4444. * return chr.user == 'pebbles';
  4445. * });
  4446. * // => 2
  4447. *
  4448. * // using the `_.matches` callback shorthand
  4449. * _.findLastIndex(users, { 'user': 'barney', 'active': true });
  4450. * // => 0
  4451. *
  4452. * // using the `_.matchesProperty` callback shorthand
  4453. * _.findLastIndex(users, 'active', false);
  4454. * // => 2
  4455. *
  4456. * // using the `_.property` callback shorthand
  4457. * _.findLastIndex(users, 'active');
  4458. * // => 0
  4459. */
  4460. var findLastIndex = createFindIndex(true);
  4461. /**
  4462. * Gets the first element of `array`.
  4463. *
  4464. * @static
  4465. * @memberOf _
  4466. * @alias head
  4467. * @category Array
  4468. * @param {Array} array The array to query.
  4469. * @returns {*} Returns the first element of `array`.
  4470. * @example
  4471. *
  4472. * _.first([1, 2, 3]);
  4473. * // => 1
  4474. *
  4475. * _.first([]);
  4476. * // => undefined
  4477. */
  4478. function first(array) {
  4479. return array ? array[0] : undefined;
  4480. }
  4481. /**
  4482. * Flattens a nested array. If `isDeep` is `true` the array is recursively
  4483. * flattened, otherwise it is only flattened a single level.
  4484. *
  4485. * @static
  4486. * @memberOf _
  4487. * @category Array
  4488. * @param {Array} array The array to flatten.
  4489. * @param {boolean} [isDeep] Specify a deep flatten.
  4490. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4491. * @returns {Array} Returns the new flattened array.
  4492. * @example
  4493. *
  4494. * _.flatten([1, [2, 3, [4]]]);
  4495. * // => [1, 2, 3, [4]]
  4496. *
  4497. * // using `isDeep`
  4498. * _.flatten([1, [2, 3, [4]]], true);
  4499. * // => [1, 2, 3, 4]
  4500. */
  4501. function flatten(array, isDeep, guard) {
  4502. var length = array ? array.length : 0;
  4503. if (guard && isIterateeCall(array, isDeep, guard)) {
  4504. isDeep = false;
  4505. }
  4506. return length ? baseFlatten(array, isDeep) : [];
  4507. }
  4508. /**
  4509. * Recursively flattens a nested array.
  4510. *
  4511. * @static
  4512. * @memberOf _
  4513. * @category Array
  4514. * @param {Array} array The array to recursively flatten.
  4515. * @returns {Array} Returns the new flattened array.
  4516. * @example
  4517. *
  4518. * _.flattenDeep([1, [2, 3, [4]]]);
  4519. * // => [1, 2, 3, 4]
  4520. */
  4521. function flattenDeep(array) {
  4522. var length = array ? array.length : 0;
  4523. return length ? baseFlatten(array, true) : [];
  4524. }
  4525. /**
  4526. * Gets the index at which the first occurrence of `value` is found in `array`
  4527. * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  4528. * for equality comparisons. If `fromIndex` is negative, it is used as the offset
  4529. * from the end of `array`. If `array` is sorted providing `true` for `fromIndex`
  4530. * performs a faster binary search.
  4531. *
  4532. * @static
  4533. * @memberOf _
  4534. * @category Array
  4535. * @param {Array} array The array to search.
  4536. * @param {*} value The value to search for.
  4537. * @param {boolean|number} [fromIndex=0] The index to search from or `true`
  4538. * to perform a binary search on a sorted array.
  4539. * @returns {number} Returns the index of the matched value, else `-1`.
  4540. * @example
  4541. *
  4542. * _.indexOf([1, 2, 1, 2], 2);
  4543. * // => 1
  4544. *
  4545. * // using `fromIndex`
  4546. * _.indexOf([1, 2, 1, 2], 2, 2);
  4547. * // => 3
  4548. *
  4549. * // performing a binary search
  4550. * _.indexOf([1, 1, 2, 2], 2, true);
  4551. * // => 2
  4552. */
  4553. function indexOf(array, value, fromIndex) {
  4554. var length = array ? array.length : 0;
  4555. if (!length) {
  4556. return -1;
  4557. }
  4558. if (typeof fromIndex == 'number') {
  4559. fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex;
  4560. } else if (fromIndex) {
  4561. var index = binaryIndex(array, value);
  4562. if (index < length &&
  4563. (value === value ? (value === array[index]) : (array[index] !== array[index]))) {
  4564. return index;
  4565. }
  4566. return -1;
  4567. }
  4568. return baseIndexOf(array, value, fromIndex || 0);
  4569. }
  4570. /**
  4571. * Gets all but the last element of `array`.
  4572. *
  4573. * @static
  4574. * @memberOf _
  4575. * @category Array
  4576. * @param {Array} array The array to query.
  4577. * @returns {Array} Returns the slice of `array`.
  4578. * @example
  4579. *
  4580. * _.initial([1, 2, 3]);
  4581. * // => [1, 2]
  4582. */
  4583. function initial(array) {
  4584. return dropRight(array, 1);
  4585. }
  4586. /**
  4587. * Creates an array of unique values that are included in all of the provided
  4588. * arrays using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  4589. * for equality comparisons.
  4590. *
  4591. * @static
  4592. * @memberOf _
  4593. * @category Array
  4594. * @param {...Array} [arrays] The arrays to inspect.
  4595. * @returns {Array} Returns the new array of shared values.
  4596. * @example
  4597. * _.intersection([1, 2], [4, 2], [2, 1]);
  4598. * // => [2]
  4599. */
  4600. var intersection = restParam(function(arrays) {
  4601. var othLength = arrays.length,
  4602. othIndex = othLength,
  4603. caches = Array(length),
  4604. indexOf = getIndexOf(),
  4605. isCommon = indexOf == baseIndexOf,
  4606. result = [];
  4607. while (othIndex--) {
  4608. var value = arrays[othIndex] = isArrayLike(value = arrays[othIndex]) ? value : [];
  4609. caches[othIndex] = (isCommon && value.length >= 120) ? createCache(othIndex && value) : null;
  4610. }
  4611. var array = arrays[0],
  4612. index = -1,
  4613. length = array ? array.length : 0,
  4614. seen = caches[0];
  4615. outer:
  4616. while (++index < length) {
  4617. value = array[index];
  4618. if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value, 0)) < 0) {
  4619. var othIndex = othLength;
  4620. while (--othIndex) {
  4621. var cache = caches[othIndex];
  4622. if ((cache ? cacheIndexOf(cache, value) : indexOf(arrays[othIndex], value, 0)) < 0) {
  4623. continue outer;
  4624. }
  4625. }
  4626. if (seen) {
  4627. seen.push(value);
  4628. }
  4629. result.push(value);
  4630. }
  4631. }
  4632. return result;
  4633. });
  4634. /**
  4635. * Gets the last element of `array`.
  4636. *
  4637. * @static
  4638. * @memberOf _
  4639. * @category Array
  4640. * @param {Array} array The array to query.
  4641. * @returns {*} Returns the last element of `array`.
  4642. * @example
  4643. *
  4644. * _.last([1, 2, 3]);
  4645. * // => 3
  4646. */
  4647. function last(array) {
  4648. var length = array ? array.length : 0;
  4649. return length ? array[length - 1] : undefined;
  4650. }
  4651. /**
  4652. * This method is like `_.indexOf` except that it iterates over elements of
  4653. * `array` from right to left.
  4654. *
  4655. * @static
  4656. * @memberOf _
  4657. * @category Array
  4658. * @param {Array} array The array to search.
  4659. * @param {*} value The value to search for.
  4660. * @param {boolean|number} [fromIndex=array.length-1] The index to search from
  4661. * or `true` to perform a binary search on a sorted array.
  4662. * @returns {number} Returns the index of the matched value, else `-1`.
  4663. * @example
  4664. *
  4665. * _.lastIndexOf([1, 2, 1, 2], 2);
  4666. * // => 3
  4667. *
  4668. * // using `fromIndex`
  4669. * _.lastIndexOf([1, 2, 1, 2], 2, 2);
  4670. * // => 1
  4671. *
  4672. * // performing a binary search
  4673. * _.lastIndexOf([1, 1, 2, 2], 2, true);
  4674. * // => 3
  4675. */
  4676. function lastIndexOf(array, value, fromIndex) {
  4677. var length = array ? array.length : 0;
  4678. if (!length) {
  4679. return -1;
  4680. }
  4681. var index = length;
  4682. if (typeof fromIndex == 'number') {
  4683. index = (fromIndex < 0 ? nativeMax(length + fromIndex, 0) : nativeMin(fromIndex || 0, length - 1)) + 1;
  4684. } else if (fromIndex) {
  4685. index = binaryIndex(array, value, true) - 1;
  4686. var other = array[index];
  4687. if (value === value ? (value === other) : (other !== other)) {
  4688. return index;
  4689. }
  4690. return -1;
  4691. }
  4692. if (value !== value) {
  4693. return indexOfNaN(array, index, true);
  4694. }
  4695. while (index--) {
  4696. if (array[index] === value) {
  4697. return index;
  4698. }
  4699. }
  4700. return -1;
  4701. }
  4702. /**
  4703. * Removes all provided values from `array` using
  4704. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  4705. * for equality comparisons.
  4706. *
  4707. * **Note:** Unlike `_.without`, this method mutates `array`.
  4708. *
  4709. * @static
  4710. * @memberOf _
  4711. * @category Array
  4712. * @param {Array} array The array to modify.
  4713. * @param {...*} [values] The values to remove.
  4714. * @returns {Array} Returns `array`.
  4715. * @example
  4716. *
  4717. * var array = [1, 2, 3, 1, 2, 3];
  4718. *
  4719. * _.pull(array, 2, 3);
  4720. * console.log(array);
  4721. * // => [1, 1]
  4722. */
  4723. function pull() {
  4724. var args = arguments,
  4725. array = args[0];
  4726. if (!(array && array.length)) {
  4727. return array;
  4728. }
  4729. var index = 0,
  4730. indexOf = getIndexOf(),
  4731. length = args.length;
  4732. while (++index < length) {
  4733. var fromIndex = 0,
  4734. value = args[index];
  4735. while ((fromIndex = indexOf(array, value, fromIndex)) > -1) {
  4736. splice.call(array, fromIndex, 1);
  4737. }
  4738. }
  4739. return array;
  4740. }
  4741. /**
  4742. * Removes elements from `array` corresponding to the given indexes and returns
  4743. * an array of the removed elements. Indexes may be specified as an array of
  4744. * indexes or as individual arguments.
  4745. *
  4746. * **Note:** Unlike `_.at`, this method mutates `array`.
  4747. *
  4748. * @static
  4749. * @memberOf _
  4750. * @category Array
  4751. * @param {Array} array The array to modify.
  4752. * @param {...(number|number[])} [indexes] The indexes of elements to remove,
  4753. * specified as individual indexes or arrays of indexes.
  4754. * @returns {Array} Returns the new array of removed elements.
  4755. * @example
  4756. *
  4757. * var array = [5, 10, 15, 20];
  4758. * var evens = _.pullAt(array, 1, 3);
  4759. *
  4760. * console.log(array);
  4761. * // => [5, 15]
  4762. *
  4763. * console.log(evens);
  4764. * // => [10, 20]
  4765. */
  4766. var pullAt = restParam(function(array, indexes) {
  4767. indexes = baseFlatten(indexes);
  4768. var result = baseAt(array, indexes);
  4769. basePullAt(array, indexes.sort(baseCompareAscending));
  4770. return result;
  4771. });
  4772. /**
  4773. * Removes all elements from `array` that `predicate` returns truthy for
  4774. * and returns an array of the removed elements. The predicate is bound to
  4775. * `thisArg` and invoked with three arguments: (value, index, array).
  4776. *
  4777. * If a property name is provided for `predicate` the created `_.property`
  4778. * style callback returns the property value of the given element.
  4779. *
  4780. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4781. * style callback returns `true` for elements that have a matching property
  4782. * value, else `false`.
  4783. *
  4784. * If an object is provided for `predicate` the created `_.matches` style
  4785. * callback returns `true` for elements that have the properties of the given
  4786. * object, else `false`.
  4787. *
  4788. * **Note:** Unlike `_.filter`, this method mutates `array`.
  4789. *
  4790. * @static
  4791. * @memberOf _
  4792. * @category Array
  4793. * @param {Array} array The array to modify.
  4794. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  4795. * per iteration.
  4796. * @param {*} [thisArg] The `this` binding of `predicate`.
  4797. * @returns {Array} Returns the new array of removed elements.
  4798. * @example
  4799. *
  4800. * var array = [1, 2, 3, 4];
  4801. * var evens = _.remove(array, function(n) {
  4802. * return n % 2 == 0;
  4803. * });
  4804. *
  4805. * console.log(array);
  4806. * // => [1, 3]
  4807. *
  4808. * console.log(evens);
  4809. * // => [2, 4]
  4810. */
  4811. function remove(array, predicate, thisArg) {
  4812. var result = [];
  4813. if (!(array && array.length)) {
  4814. return result;
  4815. }
  4816. var index = -1,
  4817. indexes = [],
  4818. length = array.length;
  4819. predicate = getCallback(predicate, thisArg, 3);
  4820. while (++index < length) {
  4821. var value = array[index];
  4822. if (predicate(value, index, array)) {
  4823. result.push(value);
  4824. indexes.push(index);
  4825. }
  4826. }
  4827. basePullAt(array, indexes);
  4828. return result;
  4829. }
  4830. /**
  4831. * Gets all but the first element of `array`.
  4832. *
  4833. * @static
  4834. * @memberOf _
  4835. * @alias tail
  4836. * @category Array
  4837. * @param {Array} array The array to query.
  4838. * @returns {Array} Returns the slice of `array`.
  4839. * @example
  4840. *
  4841. * _.rest([1, 2, 3]);
  4842. * // => [2, 3]
  4843. */
  4844. function rest(array) {
  4845. return drop(array, 1);
  4846. }
  4847. /**
  4848. * Creates a slice of `array` from `start` up to, but not including, `end`.
  4849. *
  4850. * **Note:** This method is used instead of `Array#slice` to support node
  4851. * lists in IE < 9 and to ensure dense arrays are returned.
  4852. *
  4853. * @static
  4854. * @memberOf _
  4855. * @category Array
  4856. * @param {Array} array The array to slice.
  4857. * @param {number} [start=0] The start position.
  4858. * @param {number} [end=array.length] The end position.
  4859. * @returns {Array} Returns the slice of `array`.
  4860. */
  4861. function slice(array, start, end) {
  4862. var length = array ? array.length : 0;
  4863. if (!length) {
  4864. return [];
  4865. }
  4866. if (end && typeof end != 'number' && isIterateeCall(array, start, end)) {
  4867. start = 0;
  4868. end = length;
  4869. }
  4870. return baseSlice(array, start, end);
  4871. }
  4872. /**
  4873. * Uses a binary search to determine the lowest index at which `value` should
  4874. * be inserted into `array` in order to maintain its sort order. If an iteratee
  4875. * function is provided it is invoked for `value` and each element of `array`
  4876. * to compute their sort ranking. The iteratee is bound to `thisArg` and
  4877. * invoked with one argument; (value).
  4878. *
  4879. * If a property name is provided for `iteratee` the created `_.property`
  4880. * style callback returns the property value of the given element.
  4881. *
  4882. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4883. * style callback returns `true` for elements that have a matching property
  4884. * value, else `false`.
  4885. *
  4886. * If an object is provided for `iteratee` the created `_.matches` style
  4887. * callback returns `true` for elements that have the properties of the given
  4888. * object, else `false`.
  4889. *
  4890. * @static
  4891. * @memberOf _
  4892. * @category Array
  4893. * @param {Array} array The sorted array to inspect.
  4894. * @param {*} value The value to evaluate.
  4895. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  4896. * per iteration.
  4897. * @param {*} [thisArg] The `this` binding of `iteratee`.
  4898. * @returns {number} Returns the index at which `value` should be inserted
  4899. * into `array`.
  4900. * @example
  4901. *
  4902. * _.sortedIndex([30, 50], 40);
  4903. * // => 1
  4904. *
  4905. * _.sortedIndex([4, 4, 5, 5], 5);
  4906. * // => 2
  4907. *
  4908. * var dict = { 'data': { 'thirty': 30, 'forty': 40, 'fifty': 50 } };
  4909. *
  4910. * // using an iteratee function
  4911. * _.sortedIndex(['thirty', 'fifty'], 'forty', function(word) {
  4912. * return this.data[word];
  4913. * }, dict);
  4914. * // => 1
  4915. *
  4916. * // using the `_.property` callback shorthand
  4917. * _.sortedIndex([{ 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
  4918. * // => 1
  4919. */
  4920. var sortedIndex = createSortedIndex();
  4921. /**
  4922. * This method is like `_.sortedIndex` except that it returns the highest
  4923. * index at which `value` should be inserted into `array` in order to
  4924. * maintain its sort order.
  4925. *
  4926. * @static
  4927. * @memberOf _
  4928. * @category Array
  4929. * @param {Array} array The sorted array to inspect.
  4930. * @param {*} value The value to evaluate.
  4931. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  4932. * per iteration.
  4933. * @param {*} [thisArg] The `this` binding of `iteratee`.
  4934. * @returns {number} Returns the index at which `value` should be inserted
  4935. * into `array`.
  4936. * @example
  4937. *
  4938. * _.sortedLastIndex([4, 4, 5, 5], 5);
  4939. * // => 4
  4940. */
  4941. var sortedLastIndex = createSortedIndex(true);
  4942. /**
  4943. * Creates a slice of `array` with `n` elements taken from the beginning.
  4944. *
  4945. * @static
  4946. * @memberOf _
  4947. * @category Array
  4948. * @param {Array} array The array to query.
  4949. * @param {number} [n=1] The number of elements to take.
  4950. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4951. * @returns {Array} Returns the slice of `array`.
  4952. * @example
  4953. *
  4954. * _.take([1, 2, 3]);
  4955. * // => [1]
  4956. *
  4957. * _.take([1, 2, 3], 2);
  4958. * // => [1, 2]
  4959. *
  4960. * _.take([1, 2, 3], 5);
  4961. * // => [1, 2, 3]
  4962. *
  4963. * _.take([1, 2, 3], 0);
  4964. * // => []
  4965. */
  4966. function take(array, n, guard) {
  4967. var length = array ? array.length : 0;
  4968. if (!length) {
  4969. return [];
  4970. }
  4971. if (guard ? isIterateeCall(array, n, guard) : n == null) {
  4972. n = 1;
  4973. }
  4974. return baseSlice(array, 0, n < 0 ? 0 : n);
  4975. }
  4976. /**
  4977. * Creates a slice of `array` with `n` elements taken from the end.
  4978. *
  4979. * @static
  4980. * @memberOf _
  4981. * @category Array
  4982. * @param {Array} array The array to query.
  4983. * @param {number} [n=1] The number of elements to take.
  4984. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4985. * @returns {Array} Returns the slice of `array`.
  4986. * @example
  4987. *
  4988. * _.takeRight([1, 2, 3]);
  4989. * // => [3]
  4990. *
  4991. * _.takeRight([1, 2, 3], 2);
  4992. * // => [2, 3]
  4993. *
  4994. * _.takeRight([1, 2, 3], 5);
  4995. * // => [1, 2, 3]
  4996. *
  4997. * _.takeRight([1, 2, 3], 0);
  4998. * // => []
  4999. */
  5000. function takeRight(array, n, guard) {
  5001. var length = array ? array.length : 0;
  5002. if (!length) {
  5003. return [];
  5004. }
  5005. if (guard ? isIterateeCall(array, n, guard) : n == null) {
  5006. n = 1;
  5007. }
  5008. n = length - (+n || 0);
  5009. return baseSlice(array, n < 0 ? 0 : n);
  5010. }
  5011. /**
  5012. * Creates a slice of `array` with elements taken from the end. Elements are
  5013. * taken until `predicate` returns falsey. The predicate is bound to `thisArg`
  5014. * and invoked with three arguments: (value, index, array).
  5015. *
  5016. * If a property name is provided for `predicate` the created `_.property`
  5017. * style callback returns the property value of the given element.
  5018. *
  5019. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5020. * style callback returns `true` for elements that have a matching property
  5021. * value, else `false`.
  5022. *
  5023. * If an object is provided for `predicate` the created `_.matches` style
  5024. * callback returns `true` for elements that have the properties of the given
  5025. * object, else `false`.
  5026. *
  5027. * @static
  5028. * @memberOf _
  5029. * @category Array
  5030. * @param {Array} array The array to query.
  5031. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5032. * per iteration.
  5033. * @param {*} [thisArg] The `this` binding of `predicate`.
  5034. * @returns {Array} Returns the slice of `array`.
  5035. * @example
  5036. *
  5037. * _.takeRightWhile([1, 2, 3], function(n) {
  5038. * return n > 1;
  5039. * });
  5040. * // => [2, 3]
  5041. *
  5042. * var users = [
  5043. * { 'user': 'barney', 'active': true },
  5044. * { 'user': 'fred', 'active': false },
  5045. * { 'user': 'pebbles', 'active': false }
  5046. * ];
  5047. *
  5048. * // using the `_.matches` callback shorthand
  5049. * _.pluck(_.takeRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user');
  5050. * // => ['pebbles']
  5051. *
  5052. * // using the `_.matchesProperty` callback shorthand
  5053. * _.pluck(_.takeRightWhile(users, 'active', false), 'user');
  5054. * // => ['fred', 'pebbles']
  5055. *
  5056. * // using the `_.property` callback shorthand
  5057. * _.pluck(_.takeRightWhile(users, 'active'), 'user');
  5058. * // => []
  5059. */
  5060. function takeRightWhile(array, predicate, thisArg) {
  5061. return (array && array.length)
  5062. ? baseWhile(array, getCallback(predicate, thisArg, 3), false, true)
  5063. : [];
  5064. }
  5065. /**
  5066. * Creates a slice of `array` with elements taken from the beginning. Elements
  5067. * are taken until `predicate` returns falsey. The predicate is bound to
  5068. * `thisArg` and invoked with three arguments: (value, index, array).
  5069. *
  5070. * If a property name is provided for `predicate` the created `_.property`
  5071. * style callback returns the property value of the given element.
  5072. *
  5073. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5074. * style callback returns `true` for elements that have a matching property
  5075. * value, else `false`.
  5076. *
  5077. * If an object is provided for `predicate` the created `_.matches` style
  5078. * callback returns `true` for elements that have the properties of the given
  5079. * object, else `false`.
  5080. *
  5081. * @static
  5082. * @memberOf _
  5083. * @category Array
  5084. * @param {Array} array The array to query.
  5085. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5086. * per iteration.
  5087. * @param {*} [thisArg] The `this` binding of `predicate`.
  5088. * @returns {Array} Returns the slice of `array`.
  5089. * @example
  5090. *
  5091. * _.takeWhile([1, 2, 3], function(n) {
  5092. * return n < 3;
  5093. * });
  5094. * // => [1, 2]
  5095. *
  5096. * var users = [
  5097. * { 'user': 'barney', 'active': false },
  5098. * { 'user': 'fred', 'active': false},
  5099. * { 'user': 'pebbles', 'active': true }
  5100. * ];
  5101. *
  5102. * // using the `_.matches` callback shorthand
  5103. * _.pluck(_.takeWhile(users, { 'user': 'barney', 'active': false }), 'user');
  5104. * // => ['barney']
  5105. *
  5106. * // using the `_.matchesProperty` callback shorthand
  5107. * _.pluck(_.takeWhile(users, 'active', false), 'user');
  5108. * // => ['barney', 'fred']
  5109. *
  5110. * // using the `_.property` callback shorthand
  5111. * _.pluck(_.takeWhile(users, 'active'), 'user');
  5112. * // => []
  5113. */
  5114. function takeWhile(array, predicate, thisArg) {
  5115. return (array && array.length)
  5116. ? baseWhile(array, getCallback(predicate, thisArg, 3))
  5117. : [];
  5118. }
  5119. /**
  5120. * Creates an array of unique values, in order, from all of the provided arrays
  5121. * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  5122. * for equality comparisons.
  5123. *
  5124. * @static
  5125. * @memberOf _
  5126. * @category Array
  5127. * @param {...Array} [arrays] The arrays to inspect.
  5128. * @returns {Array} Returns the new array of combined values.
  5129. * @example
  5130. *
  5131. * _.union([1, 2], [4, 2], [2, 1]);
  5132. * // => [1, 2, 4]
  5133. */
  5134. var union = restParam(function(arrays) {
  5135. return baseUniq(baseFlatten(arrays, false, true));
  5136. });
  5137. /**
  5138. * Creates a duplicate-free version of an array, using
  5139. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  5140. * for equality comparisons, in which only the first occurence of each element
  5141. * is kept. Providing `true` for `isSorted` performs a faster search algorithm
  5142. * for sorted arrays. If an iteratee function is provided it is invoked for
  5143. * each element in the array to generate the criterion by which uniqueness
  5144. * is computed. The `iteratee` is bound to `thisArg` and invoked with three
  5145. * arguments: (value, index, array).
  5146. *
  5147. * If a property name is provided for `iteratee` the created `_.property`
  5148. * style callback returns the property value of the given element.
  5149. *
  5150. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5151. * style callback returns `true` for elements that have a matching property
  5152. * value, else `false`.
  5153. *
  5154. * If an object is provided for `iteratee` the created `_.matches` style
  5155. * callback returns `true` for elements that have the properties of the given
  5156. * object, else `false`.
  5157. *
  5158. * @static
  5159. * @memberOf _
  5160. * @alias unique
  5161. * @category Array
  5162. * @param {Array} array The array to inspect.
  5163. * @param {boolean} [isSorted] Specify the array is sorted.
  5164. * @param {Function|Object|string} [iteratee] The function invoked per iteration.
  5165. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5166. * @returns {Array} Returns the new duplicate-value-free array.
  5167. * @example
  5168. *
  5169. * _.uniq([2, 1, 2]);
  5170. * // => [2, 1]
  5171. *
  5172. * // using `isSorted`
  5173. * _.uniq([1, 1, 2], true);
  5174. * // => [1, 2]
  5175. *
  5176. * // using an iteratee function
  5177. * _.uniq([1, 2.5, 1.5, 2], function(n) {
  5178. * return this.floor(n);
  5179. * }, Math);
  5180. * // => [1, 2.5]
  5181. *
  5182. * // using the `_.property` callback shorthand
  5183. * _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
  5184. * // => [{ 'x': 1 }, { 'x': 2 }]
  5185. */
  5186. function uniq(array, isSorted, iteratee, thisArg) {
  5187. var length = array ? array.length : 0;
  5188. if (!length) {
  5189. return [];
  5190. }
  5191. if (isSorted != null && typeof isSorted != 'boolean') {
  5192. thisArg = iteratee;
  5193. iteratee = isIterateeCall(array, isSorted, thisArg) ? undefined : isSorted;
  5194. isSorted = false;
  5195. }
  5196. var callback = getCallback();
  5197. if (!(iteratee == null && callback === baseCallback)) {
  5198. iteratee = callback(iteratee, thisArg, 3);
  5199. }
  5200. return (isSorted && getIndexOf() == baseIndexOf)
  5201. ? sortedUniq(array, iteratee)
  5202. : baseUniq(array, iteratee);
  5203. }
  5204. /**
  5205. * This method is like `_.zip` except that it accepts an array of grouped
  5206. * elements and creates an array regrouping the elements to their pre-zip
  5207. * configuration.
  5208. *
  5209. * @static
  5210. * @memberOf _
  5211. * @category Array
  5212. * @param {Array} array The array of grouped elements to process.
  5213. * @returns {Array} Returns the new array of regrouped elements.
  5214. * @example
  5215. *
  5216. * var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
  5217. * // => [['fred', 30, true], ['barney', 40, false]]
  5218. *
  5219. * _.unzip(zipped);
  5220. * // => [['fred', 'barney'], [30, 40], [true, false]]
  5221. */
  5222. function unzip(array) {
  5223. if (!(array && array.length)) {
  5224. return [];
  5225. }
  5226. var index = -1,
  5227. length = 0;
  5228. array = arrayFilter(array, function(group) {
  5229. if (isArrayLike(group)) {
  5230. length = nativeMax(group.length, length);
  5231. return true;
  5232. }
  5233. });
  5234. var result = Array(length);
  5235. while (++index < length) {
  5236. result[index] = arrayMap(array, baseProperty(index));
  5237. }
  5238. return result;
  5239. }
  5240. /**
  5241. * This method is like `_.unzip` except that it accepts an iteratee to specify
  5242. * how regrouped values should be combined. The `iteratee` is bound to `thisArg`
  5243. * and invoked with four arguments: (accumulator, value, index, group).
  5244. *
  5245. * @static
  5246. * @memberOf _
  5247. * @category Array
  5248. * @param {Array} array The array of grouped elements to process.
  5249. * @param {Function} [iteratee] The function to combine regrouped values.
  5250. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5251. * @returns {Array} Returns the new array of regrouped elements.
  5252. * @example
  5253. *
  5254. * var zipped = _.zip([1, 2], [10, 20], [100, 200]);
  5255. * // => [[1, 10, 100], [2, 20, 200]]
  5256. *
  5257. * _.unzipWith(zipped, _.add);
  5258. * // => [3, 30, 300]
  5259. */
  5260. function unzipWith(array, iteratee, thisArg) {
  5261. var length = array ? array.length : 0;
  5262. if (!length) {
  5263. return [];
  5264. }
  5265. var result = unzip(array);
  5266. if (iteratee == null) {
  5267. return result;
  5268. }
  5269. iteratee = bindCallback(iteratee, thisArg, 4);
  5270. return arrayMap(result, function(group) {
  5271. return arrayReduce(group, iteratee, undefined, true);
  5272. });
  5273. }
  5274. /**
  5275. * Creates an array excluding all provided values using
  5276. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  5277. * for equality comparisons.
  5278. *
  5279. * @static
  5280. * @memberOf _
  5281. * @category Array
  5282. * @param {Array} array The array to filter.
  5283. * @param {...*} [values] The values to exclude.
  5284. * @returns {Array} Returns the new array of filtered values.
  5285. * @example
  5286. *
  5287. * _.without([1, 2, 1, 3], 1, 2);
  5288. * // => [3]
  5289. */
  5290. var without = restParam(function(array, values) {
  5291. return isArrayLike(array)
  5292. ? baseDifference(array, values)
  5293. : [];
  5294. });
  5295. /**
  5296. * Creates an array of unique values that is the [symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference)
  5297. * of the provided arrays.
  5298. *
  5299. * @static
  5300. * @memberOf _
  5301. * @category Array
  5302. * @param {...Array} [arrays] The arrays to inspect.
  5303. * @returns {Array} Returns the new array of values.
  5304. * @example
  5305. *
  5306. * _.xor([1, 2], [4, 2]);
  5307. * // => [1, 4]
  5308. */
  5309. function xor() {
  5310. var index = -1,
  5311. length = arguments.length;
  5312. while (++index < length) {
  5313. var array = arguments[index];
  5314. if (isArrayLike(array)) {
  5315. var result = result
  5316. ? arrayPush(baseDifference(result, array), baseDifference(array, result))
  5317. : array;
  5318. }
  5319. }
  5320. return result ? baseUniq(result) : [];
  5321. }
  5322. /**
  5323. * Creates an array of grouped elements, the first of which contains the first
  5324. * elements of the given arrays, the second of which contains the second elements
  5325. * of the given arrays, and so on.
  5326. *
  5327. * @static
  5328. * @memberOf _
  5329. * @category Array
  5330. * @param {...Array} [arrays] The arrays to process.
  5331. * @returns {Array} Returns the new array of grouped elements.
  5332. * @example
  5333. *
  5334. * _.zip(['fred', 'barney'], [30, 40], [true, false]);
  5335. * // => [['fred', 30, true], ['barney', 40, false]]
  5336. */
  5337. var zip = restParam(unzip);
  5338. /**
  5339. * The inverse of `_.pairs`; this method returns an object composed from arrays
  5340. * of property names and values. Provide either a single two dimensional array,
  5341. * e.g. `[[key1, value1], [key2, value2]]` or two arrays, one of property names
  5342. * and one of corresponding values.
  5343. *
  5344. * @static
  5345. * @memberOf _
  5346. * @alias object
  5347. * @category Array
  5348. * @param {Array} props The property names.
  5349. * @param {Array} [values=[]] The property values.
  5350. * @returns {Object} Returns the new object.
  5351. * @example
  5352. *
  5353. * _.zipObject([['fred', 30], ['barney', 40]]);
  5354. * // => { 'fred': 30, 'barney': 40 }
  5355. *
  5356. * _.zipObject(['fred', 'barney'], [30, 40]);
  5357. * // => { 'fred': 30, 'barney': 40 }
  5358. */
  5359. function zipObject(props, values) {
  5360. var index = -1,
  5361. length = props ? props.length : 0,
  5362. result = {};
  5363. if (length && !values && !isArray(props[0])) {
  5364. values = [];
  5365. }
  5366. while (++index < length) {
  5367. var key = props[index];
  5368. if (values) {
  5369. result[key] = values[index];
  5370. } else if (key) {
  5371. result[key[0]] = key[1];
  5372. }
  5373. }
  5374. return result;
  5375. }
  5376. /**
  5377. * This method is like `_.zip` except that it accepts an iteratee to specify
  5378. * how grouped values should be combined. The `iteratee` is bound to `thisArg`
  5379. * and invoked with four arguments: (accumulator, value, index, group).
  5380. *
  5381. * @static
  5382. * @memberOf _
  5383. * @category Array
  5384. * @param {...Array} [arrays] The arrays to process.
  5385. * @param {Function} [iteratee] The function to combine grouped values.
  5386. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5387. * @returns {Array} Returns the new array of grouped elements.
  5388. * @example
  5389. *
  5390. * _.zipWith([1, 2], [10, 20], [100, 200], _.add);
  5391. * // => [111, 222]
  5392. */
  5393. var zipWith = restParam(function(arrays) {
  5394. var length = arrays.length,
  5395. iteratee = length > 2 ? arrays[length - 2] : undefined,
  5396. thisArg = length > 1 ? arrays[length - 1] : undefined;
  5397. if (length > 2 && typeof iteratee == 'function') {
  5398. length -= 2;
  5399. } else {
  5400. iteratee = (length > 1 && typeof thisArg == 'function') ? (--length, thisArg) : undefined;
  5401. thisArg = undefined;
  5402. }
  5403. arrays.length = length;
  5404. return unzipWith(arrays, iteratee, thisArg);
  5405. });
  5406. /*------------------------------------------------------------------------*/
  5407. /**
  5408. * Creates a `lodash` object that wraps `value` with explicit method
  5409. * chaining enabled.
  5410. *
  5411. * @static
  5412. * @memberOf _
  5413. * @category Chain
  5414. * @param {*} value The value to wrap.
  5415. * @returns {Object} Returns the new `lodash` wrapper instance.
  5416. * @example
  5417. *
  5418. * var users = [
  5419. * { 'user': 'barney', 'age': 36 },
  5420. * { 'user': 'fred', 'age': 40 },
  5421. * { 'user': 'pebbles', 'age': 1 }
  5422. * ];
  5423. *
  5424. * var youngest = _.chain(users)
  5425. * .sortBy('age')
  5426. * .map(function(chr) {
  5427. * return chr.user + ' is ' + chr.age;
  5428. * })
  5429. * .first()
  5430. * .value();
  5431. * // => 'pebbles is 1'
  5432. */
  5433. function chain(value) {
  5434. var result = lodash(value);
  5435. result.__chain__ = true;
  5436. return result;
  5437. }
  5438. /**
  5439. * This method invokes `interceptor` and returns `value`. The interceptor is
  5440. * bound to `thisArg` and invoked with one argument; (value). The purpose of
  5441. * this method is to "tap into" a method chain in order to perform operations
  5442. * on intermediate results within the chain.
  5443. *
  5444. * @static
  5445. * @memberOf _
  5446. * @category Chain
  5447. * @param {*} value The value to provide to `interceptor`.
  5448. * @param {Function} interceptor The function to invoke.
  5449. * @param {*} [thisArg] The `this` binding of `interceptor`.
  5450. * @returns {*} Returns `value`.
  5451. * @example
  5452. *
  5453. * _([1, 2, 3])
  5454. * .tap(function(array) {
  5455. * array.pop();
  5456. * })
  5457. * .reverse()
  5458. * .value();
  5459. * // => [2, 1]
  5460. */
  5461. function tap(value, interceptor, thisArg) {
  5462. interceptor.call(thisArg, value);
  5463. return value;
  5464. }
  5465. /**
  5466. * This method is like `_.tap` except that it returns the result of `interceptor`.
  5467. *
  5468. * @static
  5469. * @memberOf _
  5470. * @category Chain
  5471. * @param {*} value The value to provide to `interceptor`.
  5472. * @param {Function} interceptor The function to invoke.
  5473. * @param {*} [thisArg] The `this` binding of `interceptor`.
  5474. * @returns {*} Returns the result of `interceptor`.
  5475. * @example
  5476. *
  5477. * _(' abc ')
  5478. * .chain()
  5479. * .trim()
  5480. * .thru(function(value) {
  5481. * return [value];
  5482. * })
  5483. * .value();
  5484. * // => ['abc']
  5485. */
  5486. function thru(value, interceptor, thisArg) {
  5487. return interceptor.call(thisArg, value);
  5488. }
  5489. /**
  5490. * Enables explicit method chaining on the wrapper object.
  5491. *
  5492. * @name chain
  5493. * @memberOf _
  5494. * @category Chain
  5495. * @returns {Object} Returns the new `lodash` wrapper instance.
  5496. * @example
  5497. *
  5498. * var users = [
  5499. * { 'user': 'barney', 'age': 36 },
  5500. * { 'user': 'fred', 'age': 40 }
  5501. * ];
  5502. *
  5503. * // without explicit chaining
  5504. * _(users).first();
  5505. * // => { 'user': 'barney', 'age': 36 }
  5506. *
  5507. * // with explicit chaining
  5508. * _(users).chain()
  5509. * .first()
  5510. * .pick('user')
  5511. * .value();
  5512. * // => { 'user': 'barney' }
  5513. */
  5514. function wrapperChain() {
  5515. return chain(this);
  5516. }
  5517. /**
  5518. * Executes the chained sequence and returns the wrapped result.
  5519. *
  5520. * @name commit
  5521. * @memberOf _
  5522. * @category Chain
  5523. * @returns {Object} Returns the new `lodash` wrapper instance.
  5524. * @example
  5525. *
  5526. * var array = [1, 2];
  5527. * var wrapped = _(array).push(3);
  5528. *
  5529. * console.log(array);
  5530. * // => [1, 2]
  5531. *
  5532. * wrapped = wrapped.commit();
  5533. * console.log(array);
  5534. * // => [1, 2, 3]
  5535. *
  5536. * wrapped.last();
  5537. * // => 3
  5538. *
  5539. * console.log(array);
  5540. * // => [1, 2, 3]
  5541. */
  5542. function wrapperCommit() {
  5543. return new LodashWrapper(this.value(), this.__chain__);
  5544. }
  5545. /**
  5546. * Creates a new array joining a wrapped array with any additional arrays
  5547. * and/or values.
  5548. *
  5549. * @name concat
  5550. * @memberOf _
  5551. * @category Chain
  5552. * @param {...*} [values] The values to concatenate.
  5553. * @returns {Array} Returns the new concatenated array.
  5554. * @example
  5555. *
  5556. * var array = [1];
  5557. * var wrapped = _(array).concat(2, [3], [[4]]);
  5558. *
  5559. * console.log(wrapped.value());
  5560. * // => [1, 2, 3, [4]]
  5561. *
  5562. * console.log(array);
  5563. * // => [1]
  5564. */
  5565. var wrapperConcat = restParam(function(values) {
  5566. values = baseFlatten(values);
  5567. return this.thru(function(array) {
  5568. return arrayConcat(isArray(array) ? array : [toObject(array)], values);
  5569. });
  5570. });
  5571. /**
  5572. * Creates a clone of the chained sequence planting `value` as the wrapped value.
  5573. *
  5574. * @name plant
  5575. * @memberOf _
  5576. * @category Chain
  5577. * @returns {Object} Returns the new `lodash` wrapper instance.
  5578. * @example
  5579. *
  5580. * var array = [1, 2];
  5581. * var wrapped = _(array).map(function(value) {
  5582. * return Math.pow(value, 2);
  5583. * });
  5584. *
  5585. * var other = [3, 4];
  5586. * var otherWrapped = wrapped.plant(other);
  5587. *
  5588. * otherWrapped.value();
  5589. * // => [9, 16]
  5590. *
  5591. * wrapped.value();
  5592. * // => [1, 4]
  5593. */
  5594. function wrapperPlant(value) {
  5595. var result,
  5596. parent = this;
  5597. while (parent instanceof baseLodash) {
  5598. var clone = wrapperClone(parent);
  5599. if (result) {
  5600. previous.__wrapped__ = clone;
  5601. } else {
  5602. result = clone;
  5603. }
  5604. var previous = clone;
  5605. parent = parent.__wrapped__;
  5606. }
  5607. previous.__wrapped__ = value;
  5608. return result;
  5609. }
  5610. /**
  5611. * Reverses the wrapped array so the first element becomes the last, the
  5612. * second element becomes the second to last, and so on.
  5613. *
  5614. * **Note:** This method mutates the wrapped array.
  5615. *
  5616. * @name reverse
  5617. * @memberOf _
  5618. * @category Chain
  5619. * @returns {Object} Returns the new reversed `lodash` wrapper instance.
  5620. * @example
  5621. *
  5622. * var array = [1, 2, 3];
  5623. *
  5624. * _(array).reverse().value()
  5625. * // => [3, 2, 1]
  5626. *
  5627. * console.log(array);
  5628. * // => [3, 2, 1]
  5629. */
  5630. function wrapperReverse() {
  5631. var value = this.__wrapped__;
  5632. var interceptor = function(value) {
  5633. return (wrapped && wrapped.__dir__ < 0) ? value : value.reverse();
  5634. };
  5635. if (value instanceof LazyWrapper) {
  5636. var wrapped = value;
  5637. if (this.__actions__.length) {
  5638. wrapped = new LazyWrapper(this);
  5639. }
  5640. wrapped = wrapped.reverse();
  5641. wrapped.__actions__.push({ 'func': thru, 'args': [interceptor], 'thisArg': undefined });
  5642. return new LodashWrapper(wrapped, this.__chain__);
  5643. }
  5644. return this.thru(interceptor);
  5645. }
  5646. /**
  5647. * Produces the result of coercing the unwrapped value to a string.
  5648. *
  5649. * @name toString
  5650. * @memberOf _
  5651. * @category Chain
  5652. * @returns {string} Returns the coerced string value.
  5653. * @example
  5654. *
  5655. * _([1, 2, 3]).toString();
  5656. * // => '1,2,3'
  5657. */
  5658. function wrapperToString() {
  5659. return (this.value() + '');
  5660. }
  5661. /**
  5662. * Executes the chained sequence to extract the unwrapped value.
  5663. *
  5664. * @name value
  5665. * @memberOf _
  5666. * @alias run, toJSON, valueOf
  5667. * @category Chain
  5668. * @returns {*} Returns the resolved unwrapped value.
  5669. * @example
  5670. *
  5671. * _([1, 2, 3]).value();
  5672. * // => [1, 2, 3]
  5673. */
  5674. function wrapperValue() {
  5675. return baseWrapperValue(this.__wrapped__, this.__actions__);
  5676. }
  5677. /*------------------------------------------------------------------------*/
  5678. /**
  5679. * Creates an array of elements corresponding to the given keys, or indexes,
  5680. * of `collection`. Keys may be specified as individual arguments or as arrays
  5681. * of keys.
  5682. *
  5683. * @static
  5684. * @memberOf _
  5685. * @category Collection
  5686. * @param {Array|Object|string} collection The collection to iterate over.
  5687. * @param {...(number|number[]|string|string[])} [props] The property names
  5688. * or indexes of elements to pick, specified individually or in arrays.
  5689. * @returns {Array} Returns the new array of picked elements.
  5690. * @example
  5691. *
  5692. * _.at(['a', 'b', 'c'], [0, 2]);
  5693. * // => ['a', 'c']
  5694. *
  5695. * _.at(['barney', 'fred', 'pebbles'], 0, 2);
  5696. * // => ['barney', 'pebbles']
  5697. */
  5698. var at = restParam(function(collection, props) {
  5699. return baseAt(collection, baseFlatten(props));
  5700. });
  5701. /**
  5702. * Creates an object composed of keys generated from the results of running
  5703. * each element of `collection` through `iteratee`. The corresponding value
  5704. * of each key is the number of times the key was returned by `iteratee`.
  5705. * The `iteratee` is bound to `thisArg` and invoked with three arguments:
  5706. * (value, index|key, collection).
  5707. *
  5708. * If a property name is provided for `iteratee` the created `_.property`
  5709. * style callback returns the property value of the given element.
  5710. *
  5711. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5712. * style callback returns `true` for elements that have a matching property
  5713. * value, else `false`.
  5714. *
  5715. * If an object is provided for `iteratee` the created `_.matches` style
  5716. * callback returns `true` for elements that have the properties of the given
  5717. * object, else `false`.
  5718. *
  5719. * @static
  5720. * @memberOf _
  5721. * @category Collection
  5722. * @param {Array|Object|string} collection The collection to iterate over.
  5723. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  5724. * per iteration.
  5725. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5726. * @returns {Object} Returns the composed aggregate object.
  5727. * @example
  5728. *
  5729. * _.countBy([4.3, 6.1, 6.4], function(n) {
  5730. * return Math.floor(n);
  5731. * });
  5732. * // => { '4': 1, '6': 2 }
  5733. *
  5734. * _.countBy([4.3, 6.1, 6.4], function(n) {
  5735. * return this.floor(n);
  5736. * }, Math);
  5737. * // => { '4': 1, '6': 2 }
  5738. *
  5739. * _.countBy(['one', 'two', 'three'], 'length');
  5740. * // => { '3': 2, '5': 1 }
  5741. */
  5742. var countBy = createAggregator(function(result, value, key) {
  5743. hasOwnProperty.call(result, key) ? ++result[key] : (result[key] = 1);
  5744. });
  5745. /**
  5746. * Checks if `predicate` returns truthy for **all** elements of `collection`.
  5747. * The predicate is bound to `thisArg` and invoked with three arguments:
  5748. * (value, index|key, collection).
  5749. *
  5750. * If a property name is provided for `predicate` the created `_.property`
  5751. * style callback returns the property value of the given element.
  5752. *
  5753. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5754. * style callback returns `true` for elements that have a matching property
  5755. * value, else `false`.
  5756. *
  5757. * If an object is provided for `predicate` the created `_.matches` style
  5758. * callback returns `true` for elements that have the properties of the given
  5759. * object, else `false`.
  5760. *
  5761. * @static
  5762. * @memberOf _
  5763. * @alias all
  5764. * @category Collection
  5765. * @param {Array|Object|string} collection The collection to iterate over.
  5766. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5767. * per iteration.
  5768. * @param {*} [thisArg] The `this` binding of `predicate`.
  5769. * @returns {boolean} Returns `true` if all elements pass the predicate check,
  5770. * else `false`.
  5771. * @example
  5772. *
  5773. * _.every([true, 1, null, 'yes'], Boolean);
  5774. * // => false
  5775. *
  5776. * var users = [
  5777. * { 'user': 'barney', 'active': false },
  5778. * { 'user': 'fred', 'active': false }
  5779. * ];
  5780. *
  5781. * // using the `_.matches` callback shorthand
  5782. * _.every(users, { 'user': 'barney', 'active': false });
  5783. * // => false
  5784. *
  5785. * // using the `_.matchesProperty` callback shorthand
  5786. * _.every(users, 'active', false);
  5787. * // => true
  5788. *
  5789. * // using the `_.property` callback shorthand
  5790. * _.every(users, 'active');
  5791. * // => false
  5792. */
  5793. function every(collection, predicate, thisArg) {
  5794. var func = isArray(collection) ? arrayEvery : baseEvery;
  5795. if (thisArg && isIterateeCall(collection, predicate, thisArg)) {
  5796. predicate = undefined;
  5797. }
  5798. if (typeof predicate != 'function' || thisArg !== undefined) {
  5799. predicate = getCallback(predicate, thisArg, 3);
  5800. }
  5801. return func(collection, predicate);
  5802. }
  5803. /**
  5804. * Iterates over elements of `collection`, returning an array of all elements
  5805. * `predicate` returns truthy for. The predicate is bound to `thisArg` and
  5806. * invoked with three arguments: (value, index|key, collection).
  5807. *
  5808. * If a property name is provided for `predicate` the created `_.property`
  5809. * style callback returns the property value of the given element.
  5810. *
  5811. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5812. * style callback returns `true` for elements that have a matching property
  5813. * value, else `false`.
  5814. *
  5815. * If an object is provided for `predicate` the created `_.matches` style
  5816. * callback returns `true` for elements that have the properties of the given
  5817. * object, else `false`.
  5818. *
  5819. * @static
  5820. * @memberOf _
  5821. * @alias select
  5822. * @category Collection
  5823. * @param {Array|Object|string} collection The collection to iterate over.
  5824. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5825. * per iteration.
  5826. * @param {*} [thisArg] The `this` binding of `predicate`.
  5827. * @returns {Array} Returns the new filtered array.
  5828. * @example
  5829. *
  5830. * _.filter([4, 5, 6], function(n) {
  5831. * return n % 2 == 0;
  5832. * });
  5833. * // => [4, 6]
  5834. *
  5835. * var users = [
  5836. * { 'user': 'barney', 'age': 36, 'active': true },
  5837. * { 'user': 'fred', 'age': 40, 'active': false }
  5838. * ];
  5839. *
  5840. * // using the `_.matches` callback shorthand
  5841. * _.pluck(_.filter(users, { 'age': 36, 'active': true }), 'user');
  5842. * // => ['barney']
  5843. *
  5844. * // using the `_.matchesProperty` callback shorthand
  5845. * _.pluck(_.filter(users, 'active', false), 'user');
  5846. * // => ['fred']
  5847. *
  5848. * // using the `_.property` callback shorthand
  5849. * _.pluck(_.filter(users, 'active'), 'user');
  5850. * // => ['barney']
  5851. */
  5852. function filter(collection, predicate, thisArg) {
  5853. var func = isArray(collection) ? arrayFilter : baseFilter;
  5854. predicate = getCallback(predicate, thisArg, 3);
  5855. return func(collection, predicate);
  5856. }
  5857. /**
  5858. * Iterates over elements of `collection`, returning the first element
  5859. * `predicate` returns truthy for. The predicate is bound to `thisArg` and
  5860. * invoked with three arguments: (value, index|key, collection).
  5861. *
  5862. * If a property name is provided for `predicate` the created `_.property`
  5863. * style callback returns the property value of the given element.
  5864. *
  5865. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5866. * style callback returns `true` for elements that have a matching property
  5867. * value, else `false`.
  5868. *
  5869. * If an object is provided for `predicate` the created `_.matches` style
  5870. * callback returns `true` for elements that have the properties of the given
  5871. * object, else `false`.
  5872. *
  5873. * @static
  5874. * @memberOf _
  5875. * @alias detect
  5876. * @category Collection
  5877. * @param {Array|Object|string} collection The collection to search.
  5878. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5879. * per iteration.
  5880. * @param {*} [thisArg] The `this` binding of `predicate`.
  5881. * @returns {*} Returns the matched element, else `undefined`.
  5882. * @example
  5883. *
  5884. * var users = [
  5885. * { 'user': 'barney', 'age': 36, 'active': true },
  5886. * { 'user': 'fred', 'age': 40, 'active': false },
  5887. * { 'user': 'pebbles', 'age': 1, 'active': true }
  5888. * ];
  5889. *
  5890. * _.result(_.find(users, function(chr) {
  5891. * return chr.age < 40;
  5892. * }), 'user');
  5893. * // => 'barney'
  5894. *
  5895. * // using the `_.matches` callback shorthand
  5896. * _.result(_.find(users, { 'age': 1, 'active': true }), 'user');
  5897. * // => 'pebbles'
  5898. *
  5899. * // using the `_.matchesProperty` callback shorthand
  5900. * _.result(_.find(users, 'active', false), 'user');
  5901. * // => 'fred'
  5902. *
  5903. * // using the `_.property` callback shorthand
  5904. * _.result(_.find(users, 'active'), 'user');
  5905. * // => 'barney'
  5906. */
  5907. var find = createFind(baseEach);
  5908. /**
  5909. * This method is like `_.find` except that it iterates over elements of
  5910. * `collection` from right to left.
  5911. *
  5912. * @static
  5913. * @memberOf _
  5914. * @category Collection
  5915. * @param {Array|Object|string} collection The collection to search.
  5916. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5917. * per iteration.
  5918. * @param {*} [thisArg] The `this` binding of `predicate`.
  5919. * @returns {*} Returns the matched element, else `undefined`.
  5920. * @example
  5921. *
  5922. * _.findLast([1, 2, 3, 4], function(n) {
  5923. * return n % 2 == 1;
  5924. * });
  5925. * // => 3
  5926. */
  5927. var findLast = createFind(baseEachRight, true);
  5928. /**
  5929. * Performs a deep comparison between each element in `collection` and the
  5930. * source object, returning the first element that has equivalent property
  5931. * values.
  5932. *
  5933. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  5934. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  5935. * their own, not inherited, enumerable properties. For comparing a single
  5936. * own or inherited property value see `_.matchesProperty`.
  5937. *
  5938. * @static
  5939. * @memberOf _
  5940. * @category Collection
  5941. * @param {Array|Object|string} collection The collection to search.
  5942. * @param {Object} source The object of property values to match.
  5943. * @returns {*} Returns the matched element, else `undefined`.
  5944. * @example
  5945. *
  5946. * var users = [
  5947. * { 'user': 'barney', 'age': 36, 'active': true },
  5948. * { 'user': 'fred', 'age': 40, 'active': false }
  5949. * ];
  5950. *
  5951. * _.result(_.findWhere(users, { 'age': 36, 'active': true }), 'user');
  5952. * // => 'barney'
  5953. *
  5954. * _.result(_.findWhere(users, { 'age': 40, 'active': false }), 'user');
  5955. * // => 'fred'
  5956. */
  5957. function findWhere(collection, source) {
  5958. return find(collection, baseMatches(source));
  5959. }
  5960. /**
  5961. * Iterates over elements of `collection` invoking `iteratee` for each element.
  5962. * The `iteratee` is bound to `thisArg` and invoked with three arguments:
  5963. * (value, index|key, collection). Iteratee functions may exit iteration early
  5964. * by explicitly returning `false`.
  5965. *
  5966. * **Note:** As with other "Collections" methods, objects with a "length" property
  5967. * are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn`
  5968. * may be used for object iteration.
  5969. *
  5970. * @static
  5971. * @memberOf _
  5972. * @alias each
  5973. * @category Collection
  5974. * @param {Array|Object|string} collection The collection to iterate over.
  5975. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  5976. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5977. * @returns {Array|Object|string} Returns `collection`.
  5978. * @example
  5979. *
  5980. * _([1, 2]).forEach(function(n) {
  5981. * console.log(n);
  5982. * }).value();
  5983. * // => logs each value from left to right and returns the array
  5984. *
  5985. * _.forEach({ 'a': 1, 'b': 2 }, function(n, key) {
  5986. * console.log(n, key);
  5987. * });
  5988. * // => logs each value-key pair and returns the object (iteration order is not guaranteed)
  5989. */
  5990. var forEach = createForEach(arrayEach, baseEach);
  5991. /**
  5992. * This method is like `_.forEach` except that it iterates over elements of
  5993. * `collection` from right to left.
  5994. *
  5995. * @static
  5996. * @memberOf _
  5997. * @alias eachRight
  5998. * @category Collection
  5999. * @param {Array|Object|string} collection The collection to iterate over.
  6000. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  6001. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6002. * @returns {Array|Object|string} Returns `collection`.
  6003. * @example
  6004. *
  6005. * _([1, 2]).forEachRight(function(n) {
  6006. * console.log(n);
  6007. * }).value();
  6008. * // => logs each value from right to left and returns the array
  6009. */
  6010. var forEachRight = createForEach(arrayEachRight, baseEachRight);
  6011. /**
  6012. * Creates an object composed of keys generated from the results of running
  6013. * each element of `collection` through `iteratee`. The corresponding value
  6014. * of each key is an array of the elements responsible for generating the key.
  6015. * The `iteratee` is bound to `thisArg` and invoked with three arguments:
  6016. * (value, index|key, collection).
  6017. *
  6018. * If a property name is provided for `iteratee` the created `_.property`
  6019. * style callback returns the property value of the given element.
  6020. *
  6021. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6022. * style callback returns `true` for elements that have a matching property
  6023. * value, else `false`.
  6024. *
  6025. * If an object is provided for `iteratee` the created `_.matches` style
  6026. * callback returns `true` for elements that have the properties of the given
  6027. * object, else `false`.
  6028. *
  6029. * @static
  6030. * @memberOf _
  6031. * @category Collection
  6032. * @param {Array|Object|string} collection The collection to iterate over.
  6033. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  6034. * per iteration.
  6035. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6036. * @returns {Object} Returns the composed aggregate object.
  6037. * @example
  6038. *
  6039. * _.groupBy([4.2, 6.1, 6.4], function(n) {
  6040. * return Math.floor(n);
  6041. * });
  6042. * // => { '4': [4.2], '6': [6.1, 6.4] }
  6043. *
  6044. * _.groupBy([4.2, 6.1, 6.4], function(n) {
  6045. * return this.floor(n);
  6046. * }, Math);
  6047. * // => { '4': [4.2], '6': [6.1, 6.4] }
  6048. *
  6049. * // using the `_.property` callback shorthand
  6050. * _.groupBy(['one', 'two', 'three'], 'length');
  6051. * // => { '3': ['one', 'two'], '5': ['three'] }
  6052. */
  6053. var groupBy = createAggregator(function(result, value, key) {
  6054. if (hasOwnProperty.call(result, key)) {
  6055. result[key].push(value);
  6056. } else {
  6057. result[key] = [value];
  6058. }
  6059. });
  6060. /**
  6061. * Checks if `value` is in `collection` using
  6062. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  6063. * for equality comparisons. If `fromIndex` is negative, it is used as the offset
  6064. * from the end of `collection`.
  6065. *
  6066. * @static
  6067. * @memberOf _
  6068. * @alias contains, include
  6069. * @category Collection
  6070. * @param {Array|Object|string} collection The collection to search.
  6071. * @param {*} target The value to search for.
  6072. * @param {number} [fromIndex=0] The index to search from.
  6073. * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
  6074. * @returns {boolean} Returns `true` if a matching element is found, else `false`.
  6075. * @example
  6076. *
  6077. * _.includes([1, 2, 3], 1);
  6078. * // => true
  6079. *
  6080. * _.includes([1, 2, 3], 1, 2);
  6081. * // => false
  6082. *
  6083. * _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
  6084. * // => true
  6085. *
  6086. * _.includes('pebbles', 'eb');
  6087. * // => true
  6088. */
  6089. function includes(collection, target, fromIndex, guard) {
  6090. var length = collection ? getLength(collection) : 0;
  6091. if (!isLength(length)) {
  6092. collection = values(collection);
  6093. length = collection.length;
  6094. }
  6095. if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
  6096. fromIndex = 0;
  6097. } else {
  6098. fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
  6099. }
  6100. return (typeof collection == 'string' || !isArray(collection) && isString(collection))
  6101. ? (fromIndex <= length && collection.indexOf(target, fromIndex) > -1)
  6102. : (!!length && getIndexOf(collection, target, fromIndex) > -1);
  6103. }
  6104. /**
  6105. * Creates an object composed of keys generated from the results of running
  6106. * each element of `collection` through `iteratee`. The corresponding value
  6107. * of each key is the last element responsible for generating the key. The
  6108. * iteratee function is bound to `thisArg` and invoked with three arguments:
  6109. * (value, index|key, collection).
  6110. *
  6111. * If a property name is provided for `iteratee` the created `_.property`
  6112. * style callback returns the property value of the given element.
  6113. *
  6114. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6115. * style callback returns `true` for elements that have a matching property
  6116. * value, else `false`.
  6117. *
  6118. * If an object is provided for `iteratee` the created `_.matches` style
  6119. * callback returns `true` for elements that have the properties of the given
  6120. * object, else `false`.
  6121. *
  6122. * @static
  6123. * @memberOf _
  6124. * @category Collection
  6125. * @param {Array|Object|string} collection The collection to iterate over.
  6126. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  6127. * per iteration.
  6128. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6129. * @returns {Object} Returns the composed aggregate object.
  6130. * @example
  6131. *
  6132. * var keyData = [
  6133. * { 'dir': 'left', 'code': 97 },
  6134. * { 'dir': 'right', 'code': 100 }
  6135. * ];
  6136. *
  6137. * _.indexBy(keyData, 'dir');
  6138. * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
  6139. *
  6140. * _.indexBy(keyData, function(object) {
  6141. * return String.fromCharCode(object.code);
  6142. * });
  6143. * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  6144. *
  6145. * _.indexBy(keyData, function(object) {
  6146. * return this.fromCharCode(object.code);
  6147. * }, String);
  6148. * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  6149. */
  6150. var indexBy = createAggregator(function(result, value, key) {
  6151. result[key] = value;
  6152. });
  6153. /**
  6154. * Invokes the method at `path` of each element in `collection`, returning
  6155. * an array of the results of each invoked method. Any additional arguments
  6156. * are provided to each invoked method. If `methodName` is a function it is
  6157. * invoked for, and `this` bound to, each element in `collection`.
  6158. *
  6159. * @static
  6160. * @memberOf _
  6161. * @category Collection
  6162. * @param {Array|Object|string} collection The collection to iterate over.
  6163. * @param {Array|Function|string} path The path of the method to invoke or
  6164. * the function invoked per iteration.
  6165. * @param {...*} [args] The arguments to invoke the method with.
  6166. * @returns {Array} Returns the array of results.
  6167. * @example
  6168. *
  6169. * _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  6170. * // => [[1, 5, 7], [1, 2, 3]]
  6171. *
  6172. * _.invoke([123, 456], String.prototype.split, '');
  6173. * // => [['1', '2', '3'], ['4', '5', '6']]
  6174. */
  6175. var invoke = restParam(function(collection, path, args) {
  6176. var index = -1,
  6177. isFunc = typeof path == 'function',
  6178. isProp = isKey(path),
  6179. result = isArrayLike(collection) ? Array(collection.length) : [];
  6180. baseEach(collection, function(value) {
  6181. var func = isFunc ? path : ((isProp && value != null) ? value[path] : undefined);
  6182. result[++index] = func ? func.apply(value, args) : invokePath(value, path, args);
  6183. });
  6184. return result;
  6185. });
  6186. /**
  6187. * Creates an array of values by running each element in `collection` through
  6188. * `iteratee`. The `iteratee` is bound to `thisArg` and invoked with three
  6189. * arguments: (value, index|key, collection).
  6190. *
  6191. * If a property name is provided for `iteratee` the created `_.property`
  6192. * style callback returns the property value of the given element.
  6193. *
  6194. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6195. * style callback returns `true` for elements that have a matching property
  6196. * value, else `false`.
  6197. *
  6198. * If an object is provided for `iteratee` the created `_.matches` style
  6199. * callback returns `true` for elements that have the properties of the given
  6200. * object, else `false`.
  6201. *
  6202. * Many lodash methods are guarded to work as iteratees for methods like
  6203. * `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`.
  6204. *
  6205. * The guarded methods are:
  6206. * `ary`, `callback`, `chunk`, `clone`, `create`, `curry`, `curryRight`,
  6207. * `drop`, `dropRight`, `every`, `fill`, `flatten`, `invert`, `max`, `min`,
  6208. * `parseInt`, `slice`, `sortBy`, `take`, `takeRight`, `template`, `trim`,
  6209. * `trimLeft`, `trimRight`, `trunc`, `random`, `range`, `sample`, `some`,
  6210. * `sum`, `uniq`, and `words`
  6211. *
  6212. * @static
  6213. * @memberOf _
  6214. * @alias collect
  6215. * @category Collection
  6216. * @param {Array|Object|string} collection The collection to iterate over.
  6217. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  6218. * per iteration.
  6219. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6220. * @returns {Array} Returns the new mapped array.
  6221. * @example
  6222. *
  6223. * function timesThree(n) {
  6224. * return n * 3;
  6225. * }
  6226. *
  6227. * _.map([1, 2], timesThree);
  6228. * // => [3, 6]
  6229. *
  6230. * _.map({ 'a': 1, 'b': 2 }, timesThree);
  6231. * // => [3, 6] (iteration order is not guaranteed)
  6232. *
  6233. * var users = [
  6234. * { 'user': 'barney' },
  6235. * { 'user': 'fred' }
  6236. * ];
  6237. *
  6238. * // using the `_.property` callback shorthand
  6239. * _.map(users, 'user');
  6240. * // => ['barney', 'fred']
  6241. */
  6242. function map(collection, iteratee, thisArg) {
  6243. var func = isArray(collection) ? arrayMap : baseMap;
  6244. iteratee = getCallback(iteratee, thisArg, 3);
  6245. return func(collection, iteratee);
  6246. }
  6247. /**
  6248. * Creates an array of elements split into two groups, the first of which
  6249. * contains elements `predicate` returns truthy for, while the second of which
  6250. * contains elements `predicate` returns falsey for. The predicate is bound
  6251. * to `thisArg` and invoked with three arguments: (value, index|key, collection).
  6252. *
  6253. * If a property name is provided for `predicate` the created `_.property`
  6254. * style callback returns the property value of the given element.
  6255. *
  6256. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6257. * style callback returns `true` for elements that have a matching property
  6258. * value, else `false`.
  6259. *
  6260. * If an object is provided for `predicate` the created `_.matches` style
  6261. * callback returns `true` for elements that have the properties of the given
  6262. * object, else `false`.
  6263. *
  6264. * @static
  6265. * @memberOf _
  6266. * @category Collection
  6267. * @param {Array|Object|string} collection The collection to iterate over.
  6268. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  6269. * per iteration.
  6270. * @param {*} [thisArg] The `this` binding of `predicate`.
  6271. * @returns {Array} Returns the array of grouped elements.
  6272. * @example
  6273. *
  6274. * _.partition([1, 2, 3], function(n) {
  6275. * return n % 2;
  6276. * });
  6277. * // => [[1, 3], [2]]
  6278. *
  6279. * _.partition([1.2, 2.3, 3.4], function(n) {
  6280. * return this.floor(n) % 2;
  6281. * }, Math);
  6282. * // => [[1.2, 3.4], [2.3]]
  6283. *
  6284. * var users = [
  6285. * { 'user': 'barney', 'age': 36, 'active': false },
  6286. * { 'user': 'fred', 'age': 40, 'active': true },
  6287. * { 'user': 'pebbles', 'age': 1, 'active': false }
  6288. * ];
  6289. *
  6290. * var mapper = function(array) {
  6291. * return _.pluck(array, 'user');
  6292. * };
  6293. *
  6294. * // using the `_.matches` callback shorthand
  6295. * _.map(_.partition(users, { 'age': 1, 'active': false }), mapper);
  6296. * // => [['pebbles'], ['barney', 'fred']]
  6297. *
  6298. * // using the `_.matchesProperty` callback shorthand
  6299. * _.map(_.partition(users, 'active', false), mapper);
  6300. * // => [['barney', 'pebbles'], ['fred']]
  6301. *
  6302. * // using the `_.property` callback shorthand
  6303. * _.map(_.partition(users, 'active'), mapper);
  6304. * // => [['fred'], ['barney', 'pebbles']]
  6305. */
  6306. var partition = createAggregator(function(result, value, key) {
  6307. result[key ? 0 : 1].push(value);
  6308. }, function() { return [[], []]; });
  6309. /**
  6310. * Gets the property value of `path` from all elements in `collection`.
  6311. *
  6312. * @static
  6313. * @memberOf _
  6314. * @category Collection
  6315. * @param {Array|Object|string} collection The collection to iterate over.
  6316. * @param {Array|string} path The path of the property to pluck.
  6317. * @returns {Array} Returns the property values.
  6318. * @example
  6319. *
  6320. * var users = [
  6321. * { 'user': 'barney', 'age': 36 },
  6322. * { 'user': 'fred', 'age': 40 }
  6323. * ];
  6324. *
  6325. * _.pluck(users, 'user');
  6326. * // => ['barney', 'fred']
  6327. *
  6328. * var userIndex = _.indexBy(users, 'user');
  6329. * _.pluck(userIndex, 'age');
  6330. * // => [36, 40] (iteration order is not guaranteed)
  6331. */
  6332. function pluck(collection, path) {
  6333. return map(collection, property(path));
  6334. }
  6335. /**
  6336. * Reduces `collection` to a value which is the accumulated result of running
  6337. * each element in `collection` through `iteratee`, where each successive
  6338. * invocation is supplied the return value of the previous. If `accumulator`
  6339. * is not provided the first element of `collection` is used as the initial
  6340. * value. The `iteratee` is bound to `thisArg` and invoked with four arguments:
  6341. * (accumulator, value, index|key, collection).
  6342. *
  6343. * Many lodash methods are guarded to work as iteratees for methods like
  6344. * `_.reduce`, `_.reduceRight`, and `_.transform`.
  6345. *
  6346. * The guarded methods are:
  6347. * `assign`, `defaults`, `defaultsDeep`, `includes`, `merge`, `sortByAll`,
  6348. * and `sortByOrder`
  6349. *
  6350. * @static
  6351. * @memberOf _
  6352. * @alias foldl, inject
  6353. * @category Collection
  6354. * @param {Array|Object|string} collection The collection to iterate over.
  6355. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  6356. * @param {*} [accumulator] The initial value.
  6357. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6358. * @returns {*} Returns the accumulated value.
  6359. * @example
  6360. *
  6361. * _.reduce([1, 2], function(total, n) {
  6362. * return total + n;
  6363. * });
  6364. * // => 3
  6365. *
  6366. * _.reduce({ 'a': 1, 'b': 2 }, function(result, n, key) {
  6367. * result[key] = n * 3;
  6368. * return result;
  6369. * }, {});
  6370. * // => { 'a': 3, 'b': 6 } (iteration order is not guaranteed)
  6371. */
  6372. var reduce = createReduce(arrayReduce, baseEach);
  6373. /**
  6374. * This method is like `_.reduce` except that it iterates over elements of
  6375. * `collection` from right to left.
  6376. *
  6377. * @static
  6378. * @memberOf _
  6379. * @alias foldr
  6380. * @category Collection
  6381. * @param {Array|Object|string} collection The collection to iterate over.
  6382. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  6383. * @param {*} [accumulator] The initial value.
  6384. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6385. * @returns {*} Returns the accumulated value.
  6386. * @example
  6387. *
  6388. * var array = [[0, 1], [2, 3], [4, 5]];
  6389. *
  6390. * _.reduceRight(array, function(flattened, other) {
  6391. * return flattened.concat(other);
  6392. * }, []);
  6393. * // => [4, 5, 2, 3, 0, 1]
  6394. */
  6395. var reduceRight = createReduce(arrayReduceRight, baseEachRight);
  6396. /**
  6397. * The opposite of `_.filter`; this method returns the elements of `collection`
  6398. * that `predicate` does **not** return truthy for.
  6399. *
  6400. * @static
  6401. * @memberOf _
  6402. * @category Collection
  6403. * @param {Array|Object|string} collection The collection to iterate over.
  6404. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  6405. * per iteration.
  6406. * @param {*} [thisArg] The `this` binding of `predicate`.
  6407. * @returns {Array} Returns the new filtered array.
  6408. * @example
  6409. *
  6410. * _.reject([1, 2, 3, 4], function(n) {
  6411. * return n % 2 == 0;
  6412. * });
  6413. * // => [1, 3]
  6414. *
  6415. * var users = [
  6416. * { 'user': 'barney', 'age': 36, 'active': false },
  6417. * { 'user': 'fred', 'age': 40, 'active': true }
  6418. * ];
  6419. *
  6420. * // using the `_.matches` callback shorthand
  6421. * _.pluck(_.reject(users, { 'age': 40, 'active': true }), 'user');
  6422. * // => ['barney']
  6423. *
  6424. * // using the `_.matchesProperty` callback shorthand
  6425. * _.pluck(_.reject(users, 'active', false), 'user');
  6426. * // => ['fred']
  6427. *
  6428. * // using the `_.property` callback shorthand
  6429. * _.pluck(_.reject(users, 'active'), 'user');
  6430. * // => ['barney']
  6431. */
  6432. function reject(collection, predicate, thisArg) {
  6433. var func = isArray(collection) ? arrayFilter : baseFilter;
  6434. predicate = getCallback(predicate, thisArg, 3);
  6435. return func(collection, function(value, index, collection) {
  6436. return !predicate(value, index, collection);
  6437. });
  6438. }
  6439. /**
  6440. * Gets a random element or `n` random elements from a collection.
  6441. *
  6442. * @static
  6443. * @memberOf _
  6444. * @category Collection
  6445. * @param {Array|Object|string} collection The collection to sample.
  6446. * @param {number} [n] The number of elements to sample.
  6447. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  6448. * @returns {*} Returns the random sample(s).
  6449. * @example
  6450. *
  6451. * _.sample([1, 2, 3, 4]);
  6452. * // => 2
  6453. *
  6454. * _.sample([1, 2, 3, 4], 2);
  6455. * // => [3, 1]
  6456. */
  6457. function sample(collection, n, guard) {
  6458. if (guard ? isIterateeCall(collection, n, guard) : n == null) {
  6459. collection = toIterable(collection);
  6460. var length = collection.length;
  6461. return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
  6462. }
  6463. var index = -1,
  6464. result = toArray(collection),
  6465. length = result.length,
  6466. lastIndex = length - 1;
  6467. n = nativeMin(n < 0 ? 0 : (+n || 0), length);
  6468. while (++index < n) {
  6469. var rand = baseRandom(index, lastIndex),
  6470. value = result[rand];
  6471. result[rand] = result[index];
  6472. result[index] = value;
  6473. }
  6474. result.length = n;
  6475. return result;
  6476. }
  6477. /**
  6478. * Creates an array of shuffled values, using a version of the
  6479. * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
  6480. *
  6481. * @static
  6482. * @memberOf _
  6483. * @category Collection
  6484. * @param {Array|Object|string} collection The collection to shuffle.
  6485. * @returns {Array} Returns the new shuffled array.
  6486. * @example
  6487. *
  6488. * _.shuffle([1, 2, 3, 4]);
  6489. * // => [4, 1, 3, 2]
  6490. */
  6491. function shuffle(collection) {
  6492. return sample(collection, POSITIVE_INFINITY);
  6493. }
  6494. /**
  6495. * Gets the size of `collection` by returning its length for array-like
  6496. * values or the number of own enumerable properties for objects.
  6497. *
  6498. * @static
  6499. * @memberOf _
  6500. * @category Collection
  6501. * @param {Array|Object|string} collection The collection to inspect.
  6502. * @returns {number} Returns the size of `collection`.
  6503. * @example
  6504. *
  6505. * _.size([1, 2, 3]);
  6506. * // => 3
  6507. *
  6508. * _.size({ 'a': 1, 'b': 2 });
  6509. * // => 2
  6510. *
  6511. * _.size('pebbles');
  6512. * // => 7
  6513. */
  6514. function size(collection) {
  6515. var length = collection ? getLength(collection) : 0;
  6516. return isLength(length) ? length : keys(collection).length;
  6517. }
  6518. /**
  6519. * Checks if `predicate` returns truthy for **any** element of `collection`.
  6520. * The function returns as soon as it finds a passing value and does not iterate
  6521. * over the entire collection. The predicate is bound to `thisArg` and invoked
  6522. * with three arguments: (value, index|key, collection).
  6523. *
  6524. * If a property name is provided for `predicate` the created `_.property`
  6525. * style callback returns the property value of the given element.
  6526. *
  6527. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6528. * style callback returns `true` for elements that have a matching property
  6529. * value, else `false`.
  6530. *
  6531. * If an object is provided for `predicate` the created `_.matches` style
  6532. * callback returns `true` for elements that have the properties of the given
  6533. * object, else `false`.
  6534. *
  6535. * @static
  6536. * @memberOf _
  6537. * @alias any
  6538. * @category Collection
  6539. * @param {Array|Object|string} collection The collection to iterate over.
  6540. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  6541. * per iteration.
  6542. * @param {*} [thisArg] The `this` binding of `predicate`.
  6543. * @returns {boolean} Returns `true` if any element passes the predicate check,
  6544. * else `false`.
  6545. * @example
  6546. *
  6547. * _.some([null, 0, 'yes', false], Boolean);
  6548. * // => true
  6549. *
  6550. * var users = [
  6551. * { 'user': 'barney', 'active': true },
  6552. * { 'user': 'fred', 'active': false }
  6553. * ];
  6554. *
  6555. * // using the `_.matches` callback shorthand
  6556. * _.some(users, { 'user': 'barney', 'active': false });
  6557. * // => false
  6558. *
  6559. * // using the `_.matchesProperty` callback shorthand
  6560. * _.some(users, 'active', false);
  6561. * // => true
  6562. *
  6563. * // using the `_.property` callback shorthand
  6564. * _.some(users, 'active');
  6565. * // => true
  6566. */
  6567. function some(collection, predicate, thisArg) {
  6568. var func = isArray(collection) ? arraySome : baseSome;
  6569. if (thisArg && isIterateeCall(collection, predicate, thisArg)) {
  6570. predicate = undefined;
  6571. }
  6572. if (typeof predicate != 'function' || thisArg !== undefined) {
  6573. predicate = getCallback(predicate, thisArg, 3);
  6574. }
  6575. return func(collection, predicate);
  6576. }
  6577. /**
  6578. * Creates an array of elements, sorted in ascending order by the results of
  6579. * running each element in a collection through `iteratee`. This method performs
  6580. * a stable sort, that is, it preserves the original sort order of equal elements.
  6581. * The `iteratee` is bound to `thisArg` and invoked with three arguments:
  6582. * (value, index|key, collection).
  6583. *
  6584. * If a property name is provided for `iteratee` the created `_.property`
  6585. * style callback returns the property value of the given element.
  6586. *
  6587. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6588. * style callback returns `true` for elements that have a matching property
  6589. * value, else `false`.
  6590. *
  6591. * If an object is provided for `iteratee` the created `_.matches` style
  6592. * callback returns `true` for elements that have the properties of the given
  6593. * object, else `false`.
  6594. *
  6595. * @static
  6596. * @memberOf _
  6597. * @category Collection
  6598. * @param {Array|Object|string} collection The collection to iterate over.
  6599. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  6600. * per iteration.
  6601. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6602. * @returns {Array} Returns the new sorted array.
  6603. * @example
  6604. *
  6605. * _.sortBy([1, 2, 3], function(n) {
  6606. * return Math.sin(n);
  6607. * });
  6608. * // => [3, 1, 2]
  6609. *
  6610. * _.sortBy([1, 2, 3], function(n) {
  6611. * return this.sin(n);
  6612. * }, Math);
  6613. * // => [3, 1, 2]
  6614. *
  6615. * var users = [
  6616. * { 'user': 'fred' },
  6617. * { 'user': 'pebbles' },
  6618. * { 'user': 'barney' }
  6619. * ];
  6620. *
  6621. * // using the `_.property` callback shorthand
  6622. * _.pluck(_.sortBy(users, 'user'), 'user');
  6623. * // => ['barney', 'fred', 'pebbles']
  6624. */
  6625. function sortBy(collection, iteratee, thisArg) {
  6626. if (collection == null) {
  6627. return [];
  6628. }
  6629. if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
  6630. iteratee = undefined;
  6631. }
  6632. var index = -1;
  6633. iteratee = getCallback(iteratee, thisArg, 3);
  6634. var result = baseMap(collection, function(value, key, collection) {
  6635. return { 'criteria': iteratee(value, key, collection), 'index': ++index, 'value': value };
  6636. });
  6637. return baseSortBy(result, compareAscending);
  6638. }
  6639. /**
  6640. * This method is like `_.sortBy` except that it can sort by multiple iteratees
  6641. * or property names.
  6642. *
  6643. * If a property name is provided for an iteratee the created `_.property`
  6644. * style callback returns the property value of the given element.
  6645. *
  6646. * If an object is provided for an iteratee the created `_.matches` style
  6647. * callback returns `true` for elements that have the properties of the given
  6648. * object, else `false`.
  6649. *
  6650. * @static
  6651. * @memberOf _
  6652. * @category Collection
  6653. * @param {Array|Object|string} collection The collection to iterate over.
  6654. * @param {...(Function|Function[]|Object|Object[]|string|string[])} iteratees
  6655. * The iteratees to sort by, specified as individual values or arrays of values.
  6656. * @returns {Array} Returns the new sorted array.
  6657. * @example
  6658. *
  6659. * var users = [
  6660. * { 'user': 'fred', 'age': 48 },
  6661. * { 'user': 'barney', 'age': 36 },
  6662. * { 'user': 'fred', 'age': 42 },
  6663. * { 'user': 'barney', 'age': 34 }
  6664. * ];
  6665. *
  6666. * _.map(_.sortByAll(users, ['user', 'age']), _.values);
  6667. * // => [['barney', 34], ['barney', 36], ['fred', 42], ['fred', 48]]
  6668. *
  6669. * _.map(_.sortByAll(users, 'user', function(chr) {
  6670. * return Math.floor(chr.age / 10);
  6671. * }), _.values);
  6672. * // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
  6673. */
  6674. var sortByAll = restParam(function(collection, iteratees) {
  6675. if (collection == null) {
  6676. return [];
  6677. }
  6678. var guard = iteratees[2];
  6679. if (guard && isIterateeCall(iteratees[0], iteratees[1], guard)) {
  6680. iteratees.length = 1;
  6681. }
  6682. return baseSortByOrder(collection, baseFlatten(iteratees), []);
  6683. });
  6684. /**
  6685. * This method is like `_.sortByAll` except that it allows specifying the
  6686. * sort orders of the iteratees to sort by. If `orders` is unspecified, all
  6687. * values are sorted in ascending order. Otherwise, a value is sorted in
  6688. * ascending order if its corresponding order is "asc", and descending if "desc".
  6689. *
  6690. * If a property name is provided for an iteratee the created `_.property`
  6691. * style callback returns the property value of the given element.
  6692. *
  6693. * If an object is provided for an iteratee the created `_.matches` style
  6694. * callback returns `true` for elements that have the properties of the given
  6695. * object, else `false`.
  6696. *
  6697. * @static
  6698. * @memberOf _
  6699. * @category Collection
  6700. * @param {Array|Object|string} collection The collection to iterate over.
  6701. * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by.
  6702. * @param {boolean[]} [orders] The sort orders of `iteratees`.
  6703. * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
  6704. * @returns {Array} Returns the new sorted array.
  6705. * @example
  6706. *
  6707. * var users = [
  6708. * { 'user': 'fred', 'age': 48 },
  6709. * { 'user': 'barney', 'age': 34 },
  6710. * { 'user': 'fred', 'age': 42 },
  6711. * { 'user': 'barney', 'age': 36 }
  6712. * ];
  6713. *
  6714. * // sort by `user` in ascending order and by `age` in descending order
  6715. * _.map(_.sortByOrder(users, ['user', 'age'], ['asc', 'desc']), _.values);
  6716. * // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
  6717. */
  6718. function sortByOrder(collection, iteratees, orders, guard) {
  6719. if (collection == null) {
  6720. return [];
  6721. }
  6722. if (guard && isIterateeCall(iteratees, orders, guard)) {
  6723. orders = undefined;
  6724. }
  6725. if (!isArray(iteratees)) {
  6726. iteratees = iteratees == null ? [] : [iteratees];
  6727. }
  6728. if (!isArray(orders)) {
  6729. orders = orders == null ? [] : [orders];
  6730. }
  6731. return baseSortByOrder(collection, iteratees, orders);
  6732. }
  6733. /**
  6734. * Performs a deep comparison between each element in `collection` and the
  6735. * source object, returning an array of all elements that have equivalent
  6736. * property values.
  6737. *
  6738. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  6739. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  6740. * their own, not inherited, enumerable properties. For comparing a single
  6741. * own or inherited property value see `_.matchesProperty`.
  6742. *
  6743. * @static
  6744. * @memberOf _
  6745. * @category Collection
  6746. * @param {Array|Object|string} collection The collection to search.
  6747. * @param {Object} source The object of property values to match.
  6748. * @returns {Array} Returns the new filtered array.
  6749. * @example
  6750. *
  6751. * var users = [
  6752. * { 'user': 'barney', 'age': 36, 'active': false, 'pets': ['hoppy'] },
  6753. * { 'user': 'fred', 'age': 40, 'active': true, 'pets': ['baby puss', 'dino'] }
  6754. * ];
  6755. *
  6756. * _.pluck(_.where(users, { 'age': 36, 'active': false }), 'user');
  6757. * // => ['barney']
  6758. *
  6759. * _.pluck(_.where(users, { 'pets': ['dino'] }), 'user');
  6760. * // => ['fred']
  6761. */
  6762. function where(collection, source) {
  6763. return filter(collection, baseMatches(source));
  6764. }
  6765. /*------------------------------------------------------------------------*/
  6766. /**
  6767. * Gets the number of milliseconds that have elapsed since the Unix epoch
  6768. * (1 January 1970 00:00:00 UTC).
  6769. *
  6770. * @static
  6771. * @memberOf _
  6772. * @category Date
  6773. * @example
  6774. *
  6775. * _.defer(function(stamp) {
  6776. * console.log(_.now() - stamp);
  6777. * }, _.now());
  6778. * // => logs the number of milliseconds it took for the deferred function to be invoked
  6779. */
  6780. var now = nativeNow || function() {
  6781. return new Date().getTime();
  6782. };
  6783. /*------------------------------------------------------------------------*/
  6784. /**
  6785. * The opposite of `_.before`; this method creates a function that invokes
  6786. * `func` once it is called `n` or more times.
  6787. *
  6788. * @static
  6789. * @memberOf _
  6790. * @category Function
  6791. * @param {number} n The number of calls before `func` is invoked.
  6792. * @param {Function} func The function to restrict.
  6793. * @returns {Function} Returns the new restricted function.
  6794. * @example
  6795. *
  6796. * var saves = ['profile', 'settings'];
  6797. *
  6798. * var done = _.after(saves.length, function() {
  6799. * console.log('done saving!');
  6800. * });
  6801. *
  6802. * _.forEach(saves, function(type) {
  6803. * asyncSave({ 'type': type, 'complete': done });
  6804. * });
  6805. * // => logs 'done saving!' after the two async saves have completed
  6806. */
  6807. function after(n, func) {
  6808. if (typeof func != 'function') {
  6809. if (typeof n == 'function') {
  6810. var temp = n;
  6811. n = func;
  6812. func = temp;
  6813. } else {
  6814. throw new TypeError(FUNC_ERROR_TEXT);
  6815. }
  6816. }
  6817. n = nativeIsFinite(n = +n) ? n : 0;
  6818. return function() {
  6819. if (--n < 1) {
  6820. return func.apply(this, arguments);
  6821. }
  6822. };
  6823. }
  6824. /**
  6825. * Creates a function that accepts up to `n` arguments ignoring any
  6826. * additional arguments.
  6827. *
  6828. * @static
  6829. * @memberOf _
  6830. * @category Function
  6831. * @param {Function} func The function to cap arguments for.
  6832. * @param {number} [n=func.length] The arity cap.
  6833. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  6834. * @returns {Function} Returns the new function.
  6835. * @example
  6836. *
  6837. * _.map(['6', '8', '10'], _.ary(parseInt, 1));
  6838. * // => [6, 8, 10]
  6839. */
  6840. function ary(func, n, guard) {
  6841. if (guard && isIterateeCall(func, n, guard)) {
  6842. n = undefined;
  6843. }
  6844. n = (func && n == null) ? func.length : nativeMax(+n || 0, 0);
  6845. return createWrapper(func, ARY_FLAG, undefined, undefined, undefined, undefined, n);
  6846. }
  6847. /**
  6848. * Creates a function that invokes `func`, with the `this` binding and arguments
  6849. * of the created function, while it is called less than `n` times. Subsequent
  6850. * calls to the created function return the result of the last `func` invocation.
  6851. *
  6852. * @static
  6853. * @memberOf _
  6854. * @category Function
  6855. * @param {number} n The number of calls at which `func` is no longer invoked.
  6856. * @param {Function} func The function to restrict.
  6857. * @returns {Function} Returns the new restricted function.
  6858. * @example
  6859. *
  6860. * jQuery('#add').on('click', _.before(5, addContactToList));
  6861. * // => allows adding up to 4 contacts to the list
  6862. */
  6863. function before(n, func) {
  6864. var result;
  6865. if (typeof func != 'function') {
  6866. if (typeof n == 'function') {
  6867. var temp = n;
  6868. n = func;
  6869. func = temp;
  6870. } else {
  6871. throw new TypeError(FUNC_ERROR_TEXT);
  6872. }
  6873. }
  6874. return function() {
  6875. if (--n > 0) {
  6876. result = func.apply(this, arguments);
  6877. }
  6878. if (n <= 1) {
  6879. func = undefined;
  6880. }
  6881. return result;
  6882. };
  6883. }
  6884. /**
  6885. * Creates a function that invokes `func` with the `this` binding of `thisArg`
  6886. * and prepends any additional `_.bind` arguments to those provided to the
  6887. * bound function.
  6888. *
  6889. * The `_.bind.placeholder` value, which defaults to `_` in monolithic builds,
  6890. * may be used as a placeholder for partially applied arguments.
  6891. *
  6892. * **Note:** Unlike native `Function#bind` this method does not set the "length"
  6893. * property of bound functions.
  6894. *
  6895. * @static
  6896. * @memberOf _
  6897. * @category Function
  6898. * @param {Function} func The function to bind.
  6899. * @param {*} thisArg The `this` binding of `func`.
  6900. * @param {...*} [partials] The arguments to be partially applied.
  6901. * @returns {Function} Returns the new bound function.
  6902. * @example
  6903. *
  6904. * var greet = function(greeting, punctuation) {
  6905. * return greeting + ' ' + this.user + punctuation;
  6906. * };
  6907. *
  6908. * var object = { 'user': 'fred' };
  6909. *
  6910. * var bound = _.bind(greet, object, 'hi');
  6911. * bound('!');
  6912. * // => 'hi fred!'
  6913. *
  6914. * // using placeholders
  6915. * var bound = _.bind(greet, object, _, '!');
  6916. * bound('hi');
  6917. * // => 'hi fred!'
  6918. */
  6919. var bind = restParam(function(func, thisArg, partials) {
  6920. var bitmask = BIND_FLAG;
  6921. if (partials.length) {
  6922. var holders = replaceHolders(partials, bind.placeholder);
  6923. bitmask |= PARTIAL_FLAG;
  6924. }
  6925. return createWrapper(func, bitmask, thisArg, partials, holders);
  6926. });
  6927. /**
  6928. * Binds methods of an object to the object itself, overwriting the existing
  6929. * method. Method names may be specified as individual arguments or as arrays
  6930. * of method names. If no method names are provided all enumerable function
  6931. * properties, own and inherited, of `object` are bound.
  6932. *
  6933. * **Note:** This method does not set the "length" property of bound functions.
  6934. *
  6935. * @static
  6936. * @memberOf _
  6937. * @category Function
  6938. * @param {Object} object The object to bind and assign the bound methods to.
  6939. * @param {...(string|string[])} [methodNames] The object method names to bind,
  6940. * specified as individual method names or arrays of method names.
  6941. * @returns {Object} Returns `object`.
  6942. * @example
  6943. *
  6944. * var view = {
  6945. * 'label': 'docs',
  6946. * 'onClick': function() {
  6947. * console.log('clicked ' + this.label);
  6948. * }
  6949. * };
  6950. *
  6951. * _.bindAll(view);
  6952. * jQuery('#docs').on('click', view.onClick);
  6953. * // => logs 'clicked docs' when the element is clicked
  6954. */
  6955. var bindAll = restParam(function(object, methodNames) {
  6956. methodNames = methodNames.length ? baseFlatten(methodNames) : functions(object);
  6957. var index = -1,
  6958. length = methodNames.length;
  6959. while (++index < length) {
  6960. var key = methodNames[index];
  6961. object[key] = createWrapper(object[key], BIND_FLAG, object);
  6962. }
  6963. return object;
  6964. });
  6965. /**
  6966. * Creates a function that invokes the method at `object[key]` and prepends
  6967. * any additional `_.bindKey` arguments to those provided to the bound function.
  6968. *
  6969. * This method differs from `_.bind` by allowing bound functions to reference
  6970. * methods that may be redefined or don't yet exist.
  6971. * See [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern)
  6972. * for more details.
  6973. *
  6974. * The `_.bindKey.placeholder` value, which defaults to `_` in monolithic
  6975. * builds, may be used as a placeholder for partially applied arguments.
  6976. *
  6977. * @static
  6978. * @memberOf _
  6979. * @category Function
  6980. * @param {Object} object The object the method belongs to.
  6981. * @param {string} key The key of the method.
  6982. * @param {...*} [partials] The arguments to be partially applied.
  6983. * @returns {Function} Returns the new bound function.
  6984. * @example
  6985. *
  6986. * var object = {
  6987. * 'user': 'fred',
  6988. * 'greet': function(greeting, punctuation) {
  6989. * return greeting + ' ' + this.user + punctuation;
  6990. * }
  6991. * };
  6992. *
  6993. * var bound = _.bindKey(object, 'greet', 'hi');
  6994. * bound('!');
  6995. * // => 'hi fred!'
  6996. *
  6997. * object.greet = function(greeting, punctuation) {
  6998. * return greeting + 'ya ' + this.user + punctuation;
  6999. * };
  7000. *
  7001. * bound('!');
  7002. * // => 'hiya fred!'
  7003. *
  7004. * // using placeholders
  7005. * var bound = _.bindKey(object, 'greet', _, '!');
  7006. * bound('hi');
  7007. * // => 'hiya fred!'
  7008. */
  7009. var bindKey = restParam(function(object, key, partials) {
  7010. var bitmask = BIND_FLAG | BIND_KEY_FLAG;
  7011. if (partials.length) {
  7012. var holders = replaceHolders(partials, bindKey.placeholder);
  7013. bitmask |= PARTIAL_FLAG;
  7014. }
  7015. return createWrapper(key, bitmask, object, partials, holders);
  7016. });
  7017. /**
  7018. * Creates a function that accepts one or more arguments of `func` that when
  7019. * called either invokes `func` returning its result, if all `func` arguments
  7020. * have been provided, or returns a function that accepts one or more of the
  7021. * remaining `func` arguments, and so on. The arity of `func` may be specified
  7022. * if `func.length` is not sufficient.
  7023. *
  7024. * The `_.curry.placeholder` value, which defaults to `_` in monolithic builds,
  7025. * may be used as a placeholder for provided arguments.
  7026. *
  7027. * **Note:** This method does not set the "length" property of curried functions.
  7028. *
  7029. * @static
  7030. * @memberOf _
  7031. * @category Function
  7032. * @param {Function} func The function to curry.
  7033. * @param {number} [arity=func.length] The arity of `func`.
  7034. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  7035. * @returns {Function} Returns the new curried function.
  7036. * @example
  7037. *
  7038. * var abc = function(a, b, c) {
  7039. * return [a, b, c];
  7040. * };
  7041. *
  7042. * var curried = _.curry(abc);
  7043. *
  7044. * curried(1)(2)(3);
  7045. * // => [1, 2, 3]
  7046. *
  7047. * curried(1, 2)(3);
  7048. * // => [1, 2, 3]
  7049. *
  7050. * curried(1, 2, 3);
  7051. * // => [1, 2, 3]
  7052. *
  7053. * // using placeholders
  7054. * curried(1)(_, 3)(2);
  7055. * // => [1, 2, 3]
  7056. */
  7057. var curry = createCurry(CURRY_FLAG);
  7058. /**
  7059. * This method is like `_.curry` except that arguments are applied to `func`
  7060. * in the manner of `_.partialRight` instead of `_.partial`.
  7061. *
  7062. * The `_.curryRight.placeholder` value, which defaults to `_` in monolithic
  7063. * builds, may be used as a placeholder for provided arguments.
  7064. *
  7065. * **Note:** This method does not set the "length" property of curried functions.
  7066. *
  7067. * @static
  7068. * @memberOf _
  7069. * @category Function
  7070. * @param {Function} func The function to curry.
  7071. * @param {number} [arity=func.length] The arity of `func`.
  7072. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  7073. * @returns {Function} Returns the new curried function.
  7074. * @example
  7075. *
  7076. * var abc = function(a, b, c) {
  7077. * return [a, b, c];
  7078. * };
  7079. *
  7080. * var curried = _.curryRight(abc);
  7081. *
  7082. * curried(3)(2)(1);
  7083. * // => [1, 2, 3]
  7084. *
  7085. * curried(2, 3)(1);
  7086. * // => [1, 2, 3]
  7087. *
  7088. * curried(1, 2, 3);
  7089. * // => [1, 2, 3]
  7090. *
  7091. * // using placeholders
  7092. * curried(3)(1, _)(2);
  7093. * // => [1, 2, 3]
  7094. */
  7095. var curryRight = createCurry(CURRY_RIGHT_FLAG);
  7096. /**
  7097. * Creates a debounced function that delays invoking `func` until after `wait`
  7098. * milliseconds have elapsed since the last time the debounced function was
  7099. * invoked. The debounced function comes with a `cancel` method to cancel
  7100. * delayed invocations. Provide an options object to indicate that `func`
  7101. * should be invoked on the leading and/or trailing edge of the `wait` timeout.
  7102. * Subsequent calls to the debounced function return the result of the last
  7103. * `func` invocation.
  7104. *
  7105. * **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
  7106. * on the trailing edge of the timeout only if the the debounced function is
  7107. * invoked more than once during the `wait` timeout.
  7108. *
  7109. * See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation)
  7110. * for details over the differences between `_.debounce` and `_.throttle`.
  7111. *
  7112. * @static
  7113. * @memberOf _
  7114. * @category Function
  7115. * @param {Function} func The function to debounce.
  7116. * @param {number} [wait=0] The number of milliseconds to delay.
  7117. * @param {Object} [options] The options object.
  7118. * @param {boolean} [options.leading=false] Specify invoking on the leading
  7119. * edge of the timeout.
  7120. * @param {number} [options.maxWait] The maximum time `func` is allowed to be
  7121. * delayed before it is invoked.
  7122. * @param {boolean} [options.trailing=true] Specify invoking on the trailing
  7123. * edge of the timeout.
  7124. * @returns {Function} Returns the new debounced function.
  7125. * @example
  7126. *
  7127. * // avoid costly calculations while the window size is in flux
  7128. * jQuery(window).on('resize', _.debounce(calculateLayout, 150));
  7129. *
  7130. * // invoke `sendMail` when the click event is fired, debouncing subsequent calls
  7131. * jQuery('#postbox').on('click', _.debounce(sendMail, 300, {
  7132. * 'leading': true,
  7133. * 'trailing': false
  7134. * }));
  7135. *
  7136. * // ensure `batchLog` is invoked once after 1 second of debounced calls
  7137. * var source = new EventSource('/stream');
  7138. * jQuery(source).on('message', _.debounce(batchLog, 250, {
  7139. * 'maxWait': 1000
  7140. * }));
  7141. *
  7142. * // cancel a debounced call
  7143. * var todoChanges = _.debounce(batchLog, 1000);
  7144. * Object.observe(models.todo, todoChanges);
  7145. *
  7146. * Object.observe(models, function(changes) {
  7147. * if (_.find(changes, { 'user': 'todo', 'type': 'delete'})) {
  7148. * todoChanges.cancel();
  7149. * }
  7150. * }, ['delete']);
  7151. *
  7152. * // ...at some point `models.todo` is changed
  7153. * models.todo.completed = true;
  7154. *
  7155. * // ...before 1 second has passed `models.todo` is deleted
  7156. * // which cancels the debounced `todoChanges` call
  7157. * delete models.todo;
  7158. */
  7159. function debounce(func, wait, options) {
  7160. var args,
  7161. maxTimeoutId,
  7162. result,
  7163. stamp,
  7164. thisArg,
  7165. timeoutId,
  7166. trailingCall,
  7167. lastCalled = 0,
  7168. maxWait = false,
  7169. trailing = true;
  7170. if (typeof func != 'function') {
  7171. throw new TypeError(FUNC_ERROR_TEXT);
  7172. }
  7173. wait = wait < 0 ? 0 : (+wait || 0);
  7174. if (options === true) {
  7175. var leading = true;
  7176. trailing = false;
  7177. } else if (isObject(options)) {
  7178. leading = !!options.leading;
  7179. maxWait = 'maxWait' in options && nativeMax(+options.maxWait || 0, wait);
  7180. trailing = 'trailing' in options ? !!options.trailing : trailing;
  7181. }
  7182. function cancel() {
  7183. if (timeoutId) {
  7184. clearTimeout(timeoutId);
  7185. }
  7186. if (maxTimeoutId) {
  7187. clearTimeout(maxTimeoutId);
  7188. }
  7189. lastCalled = 0;
  7190. maxTimeoutId = timeoutId = trailingCall = undefined;
  7191. }
  7192. function complete(isCalled, id) {
  7193. if (id) {
  7194. clearTimeout(id);
  7195. }
  7196. maxTimeoutId = timeoutId = trailingCall = undefined;
  7197. if (isCalled) {
  7198. lastCalled = now();
  7199. result = func.apply(thisArg, args);
  7200. if (!timeoutId && !maxTimeoutId) {
  7201. args = thisArg = undefined;
  7202. }
  7203. }
  7204. }
  7205. function delayed() {
  7206. var remaining = wait - (now() - stamp);
  7207. if (remaining <= 0 || remaining > wait) {
  7208. complete(trailingCall, maxTimeoutId);
  7209. } else {
  7210. timeoutId = setTimeout(delayed, remaining);
  7211. }
  7212. }
  7213. function maxDelayed() {
  7214. complete(trailing, timeoutId);
  7215. }
  7216. function debounced() {
  7217. args = arguments;
  7218. stamp = now();
  7219. thisArg = this;
  7220. trailingCall = trailing && (timeoutId || !leading);
  7221. if (maxWait === false) {
  7222. var leadingCall = leading && !timeoutId;
  7223. } else {
  7224. if (!maxTimeoutId && !leading) {
  7225. lastCalled = stamp;
  7226. }
  7227. var remaining = maxWait - (stamp - lastCalled),
  7228. isCalled = remaining <= 0 || remaining > maxWait;
  7229. if (isCalled) {
  7230. if (maxTimeoutId) {
  7231. maxTimeoutId = clearTimeout(maxTimeoutId);
  7232. }
  7233. lastCalled = stamp;
  7234. result = func.apply(thisArg, args);
  7235. }
  7236. else if (!maxTimeoutId) {
  7237. maxTimeoutId = setTimeout(maxDelayed, remaining);
  7238. }
  7239. }
  7240. if (isCalled && timeoutId) {
  7241. timeoutId = clearTimeout(timeoutId);
  7242. }
  7243. else if (!timeoutId && wait !== maxWait) {
  7244. timeoutId = setTimeout(delayed, wait);
  7245. }
  7246. if (leadingCall) {
  7247. isCalled = true;
  7248. result = func.apply(thisArg, args);
  7249. }
  7250. if (isCalled && !timeoutId && !maxTimeoutId) {
  7251. args = thisArg = undefined;
  7252. }
  7253. return result;
  7254. }
  7255. debounced.cancel = cancel;
  7256. return debounced;
  7257. }
  7258. /**
  7259. * Defers invoking the `func` until the current call stack has cleared. Any
  7260. * additional arguments are provided to `func` when it is invoked.
  7261. *
  7262. * @static
  7263. * @memberOf _
  7264. * @category Function
  7265. * @param {Function} func The function to defer.
  7266. * @param {...*} [args] The arguments to invoke the function with.
  7267. * @returns {number} Returns the timer id.
  7268. * @example
  7269. *
  7270. * _.defer(function(text) {
  7271. * console.log(text);
  7272. * }, 'deferred');
  7273. * // logs 'deferred' after one or more milliseconds
  7274. */
  7275. var defer = restParam(function(func, args) {
  7276. return baseDelay(func, 1, args);
  7277. });
  7278. /**
  7279. * Invokes `func` after `wait` milliseconds. Any additional arguments are
  7280. * provided to `func` when it is invoked.
  7281. *
  7282. * @static
  7283. * @memberOf _
  7284. * @category Function
  7285. * @param {Function} func The function to delay.
  7286. * @param {number} wait The number of milliseconds to delay invocation.
  7287. * @param {...*} [args] The arguments to invoke the function with.
  7288. * @returns {number} Returns the timer id.
  7289. * @example
  7290. *
  7291. * _.delay(function(text) {
  7292. * console.log(text);
  7293. * }, 1000, 'later');
  7294. * // => logs 'later' after one second
  7295. */
  7296. var delay = restParam(function(func, wait, args) {
  7297. return baseDelay(func, wait, args);
  7298. });
  7299. /**
  7300. * Creates a function that returns the result of invoking the provided
  7301. * functions with the `this` binding of the created function, where each
  7302. * successive invocation is supplied the return value of the previous.
  7303. *
  7304. * @static
  7305. * @memberOf _
  7306. * @category Function
  7307. * @param {...Function} [funcs] Functions to invoke.
  7308. * @returns {Function} Returns the new function.
  7309. * @example
  7310. *
  7311. * function square(n) {
  7312. * return n * n;
  7313. * }
  7314. *
  7315. * var addSquare = _.flow(_.add, square);
  7316. * addSquare(1, 2);
  7317. * // => 9
  7318. */
  7319. var flow = createFlow();
  7320. /**
  7321. * This method is like `_.flow` except that it creates a function that
  7322. * invokes the provided functions from right to left.
  7323. *
  7324. * @static
  7325. * @memberOf _
  7326. * @alias backflow, compose
  7327. * @category Function
  7328. * @param {...Function} [funcs] Functions to invoke.
  7329. * @returns {Function} Returns the new function.
  7330. * @example
  7331. *
  7332. * function square(n) {
  7333. * return n * n;
  7334. * }
  7335. *
  7336. * var addSquare = _.flowRight(square, _.add);
  7337. * addSquare(1, 2);
  7338. * // => 9
  7339. */
  7340. var flowRight = createFlow(true);
  7341. /**
  7342. * Creates a function that memoizes the result of `func`. If `resolver` is
  7343. * provided it determines the cache key for storing the result based on the
  7344. * arguments provided to the memoized function. By default, the first argument
  7345. * provided to the memoized function is coerced to a string and used as the
  7346. * cache key. The `func` is invoked with the `this` binding of the memoized
  7347. * function.
  7348. *
  7349. * **Note:** The cache is exposed as the `cache` property on the memoized
  7350. * function. Its creation may be customized by replacing the `_.memoize.Cache`
  7351. * constructor with one whose instances implement the [`Map`](http://ecma-international.org/ecma-262/6.0/#sec-properties-of-the-map-prototype-object)
  7352. * method interface of `get`, `has`, and `set`.
  7353. *
  7354. * @static
  7355. * @memberOf _
  7356. * @category Function
  7357. * @param {Function} func The function to have its output memoized.
  7358. * @param {Function} [resolver] The function to resolve the cache key.
  7359. * @returns {Function} Returns the new memoizing function.
  7360. * @example
  7361. *
  7362. * var upperCase = _.memoize(function(string) {
  7363. * return string.toUpperCase();
  7364. * });
  7365. *
  7366. * upperCase('fred');
  7367. * // => 'FRED'
  7368. *
  7369. * // modifying the result cache
  7370. * upperCase.cache.set('fred', 'BARNEY');
  7371. * upperCase('fred');
  7372. * // => 'BARNEY'
  7373. *
  7374. * // replacing `_.memoize.Cache`
  7375. * var object = { 'user': 'fred' };
  7376. * var other = { 'user': 'barney' };
  7377. * var identity = _.memoize(_.identity);
  7378. *
  7379. * identity(object);
  7380. * // => { 'user': 'fred' }
  7381. * identity(other);
  7382. * // => { 'user': 'fred' }
  7383. *
  7384. * _.memoize.Cache = WeakMap;
  7385. * var identity = _.memoize(_.identity);
  7386. *
  7387. * identity(object);
  7388. * // => { 'user': 'fred' }
  7389. * identity(other);
  7390. * // => { 'user': 'barney' }
  7391. */
  7392. function memoize(func, resolver) {
  7393. if (typeof func != 'function' || (resolver && typeof resolver != 'function')) {
  7394. throw new TypeError(FUNC_ERROR_TEXT);
  7395. }
  7396. var memoized = function() {
  7397. var args = arguments,
  7398. key = resolver ? resolver.apply(this, args) : args[0],
  7399. cache = memoized.cache;
  7400. if (cache.has(key)) {
  7401. return cache.get(key);
  7402. }
  7403. var result = func.apply(this, args);
  7404. memoized.cache = cache.set(key, result);
  7405. return result;
  7406. };
  7407. memoized.cache = new memoize.Cache;
  7408. return memoized;
  7409. }
  7410. /**
  7411. * Creates a function that runs each argument through a corresponding
  7412. * transform function.
  7413. *
  7414. * @static
  7415. * @memberOf _
  7416. * @category Function
  7417. * @param {Function} func The function to wrap.
  7418. * @param {...(Function|Function[])} [transforms] The functions to transform
  7419. * arguments, specified as individual functions or arrays of functions.
  7420. * @returns {Function} Returns the new function.
  7421. * @example
  7422. *
  7423. * function doubled(n) {
  7424. * return n * 2;
  7425. * }
  7426. *
  7427. * function square(n) {
  7428. * return n * n;
  7429. * }
  7430. *
  7431. * var modded = _.modArgs(function(x, y) {
  7432. * return [x, y];
  7433. * }, square, doubled);
  7434. *
  7435. * modded(1, 2);
  7436. * // => [1, 4]
  7437. *
  7438. * modded(5, 10);
  7439. * // => [25, 20]
  7440. */
  7441. var modArgs = restParam(function(func, transforms) {
  7442. transforms = baseFlatten(transforms);
  7443. if (typeof func != 'function' || !arrayEvery(transforms, baseIsFunction)) {
  7444. throw new TypeError(FUNC_ERROR_TEXT);
  7445. }
  7446. var length = transforms.length;
  7447. return restParam(function(args) {
  7448. var index = nativeMin(args.length, length);
  7449. while (index--) {
  7450. args[index] = transforms[index](args[index]);
  7451. }
  7452. return func.apply(this, args);
  7453. });
  7454. });
  7455. /**
  7456. * Creates a function that negates the result of the predicate `func`. The
  7457. * `func` predicate is invoked with the `this` binding and arguments of the
  7458. * created function.
  7459. *
  7460. * @static
  7461. * @memberOf _
  7462. * @category Function
  7463. * @param {Function} predicate The predicate to negate.
  7464. * @returns {Function} Returns the new function.
  7465. * @example
  7466. *
  7467. * function isEven(n) {
  7468. * return n % 2 == 0;
  7469. * }
  7470. *
  7471. * _.filter([1, 2, 3, 4, 5, 6], _.negate(isEven));
  7472. * // => [1, 3, 5]
  7473. */
  7474. function negate(predicate) {
  7475. if (typeof predicate != 'function') {
  7476. throw new TypeError(FUNC_ERROR_TEXT);
  7477. }
  7478. return function() {
  7479. return !predicate.apply(this, arguments);
  7480. };
  7481. }
  7482. /**
  7483. * Creates a function that is restricted to invoking `func` once. Repeat calls
  7484. * to the function return the value of the first call. The `func` is invoked
  7485. * with the `this` binding and arguments of the created function.
  7486. *
  7487. * @static
  7488. * @memberOf _
  7489. * @category Function
  7490. * @param {Function} func The function to restrict.
  7491. * @returns {Function} Returns the new restricted function.
  7492. * @example
  7493. *
  7494. * var initialize = _.once(createApplication);
  7495. * initialize();
  7496. * initialize();
  7497. * // `initialize` invokes `createApplication` once
  7498. */
  7499. function once(func) {
  7500. return before(2, func);
  7501. }
  7502. /**
  7503. * Creates a function that invokes `func` with `partial` arguments prepended
  7504. * to those provided to the new function. This method is like `_.bind` except
  7505. * it does **not** alter the `this` binding.
  7506. *
  7507. * The `_.partial.placeholder` value, which defaults to `_` in monolithic
  7508. * builds, may be used as a placeholder for partially applied arguments.
  7509. *
  7510. * **Note:** This method does not set the "length" property of partially
  7511. * applied functions.
  7512. *
  7513. * @static
  7514. * @memberOf _
  7515. * @category Function
  7516. * @param {Function} func The function to partially apply arguments to.
  7517. * @param {...*} [partials] The arguments to be partially applied.
  7518. * @returns {Function} Returns the new partially applied function.
  7519. * @example
  7520. *
  7521. * var greet = function(greeting, name) {
  7522. * return greeting + ' ' + name;
  7523. * };
  7524. *
  7525. * var sayHelloTo = _.partial(greet, 'hello');
  7526. * sayHelloTo('fred');
  7527. * // => 'hello fred'
  7528. *
  7529. * // using placeholders
  7530. * var greetFred = _.partial(greet, _, 'fred');
  7531. * greetFred('hi');
  7532. * // => 'hi fred'
  7533. */
  7534. var partial = createPartial(PARTIAL_FLAG);
  7535. /**
  7536. * This method is like `_.partial` except that partially applied arguments
  7537. * are appended to those provided to the new function.
  7538. *
  7539. * The `_.partialRight.placeholder` value, which defaults to `_` in monolithic
  7540. * builds, may be used as a placeholder for partially applied arguments.
  7541. *
  7542. * **Note:** This method does not set the "length" property of partially
  7543. * applied functions.
  7544. *
  7545. * @static
  7546. * @memberOf _
  7547. * @category Function
  7548. * @param {Function} func The function to partially apply arguments to.
  7549. * @param {...*} [partials] The arguments to be partially applied.
  7550. * @returns {Function} Returns the new partially applied function.
  7551. * @example
  7552. *
  7553. * var greet = function(greeting, name) {
  7554. * return greeting + ' ' + name;
  7555. * };
  7556. *
  7557. * var greetFred = _.partialRight(greet, 'fred');
  7558. * greetFred('hi');
  7559. * // => 'hi fred'
  7560. *
  7561. * // using placeholders
  7562. * var sayHelloTo = _.partialRight(greet, 'hello', _);
  7563. * sayHelloTo('fred');
  7564. * // => 'hello fred'
  7565. */
  7566. var partialRight = createPartial(PARTIAL_RIGHT_FLAG);
  7567. /**
  7568. * Creates a function that invokes `func` with arguments arranged according
  7569. * to the specified indexes where the argument value at the first index is
  7570. * provided as the first argument, the argument value at the second index is
  7571. * provided as the second argument, and so on.
  7572. *
  7573. * @static
  7574. * @memberOf _
  7575. * @category Function
  7576. * @param {Function} func The function to rearrange arguments for.
  7577. * @param {...(number|number[])} indexes The arranged argument indexes,
  7578. * specified as individual indexes or arrays of indexes.
  7579. * @returns {Function} Returns the new function.
  7580. * @example
  7581. *
  7582. * var rearged = _.rearg(function(a, b, c) {
  7583. * return [a, b, c];
  7584. * }, 2, 0, 1);
  7585. *
  7586. * rearged('b', 'c', 'a')
  7587. * // => ['a', 'b', 'c']
  7588. *
  7589. * var map = _.rearg(_.map, [1, 0]);
  7590. * map(function(n) {
  7591. * return n * 3;
  7592. * }, [1, 2, 3]);
  7593. * // => [3, 6, 9]
  7594. */
  7595. var rearg = restParam(function(func, indexes) {
  7596. return createWrapper(func, REARG_FLAG, undefined, undefined, undefined, baseFlatten(indexes));
  7597. });
  7598. /**
  7599. * Creates a function that invokes `func` with the `this` binding of the
  7600. * created function and arguments from `start` and beyond provided as an array.
  7601. *
  7602. * **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).
  7603. *
  7604. * @static
  7605. * @memberOf _
  7606. * @category Function
  7607. * @param {Function} func The function to apply a rest parameter to.
  7608. * @param {number} [start=func.length-1] The start position of the rest parameter.
  7609. * @returns {Function} Returns the new function.
  7610. * @example
  7611. *
  7612. * var say = _.restParam(function(what, names) {
  7613. * return what + ' ' + _.initial(names).join(', ') +
  7614. * (_.size(names) > 1 ? ', & ' : '') + _.last(names);
  7615. * });
  7616. *
  7617. * say('hello', 'fred', 'barney', 'pebbles');
  7618. * // => 'hello fred, barney, & pebbles'
  7619. */
  7620. function restParam(func, start) {
  7621. if (typeof func != 'function') {
  7622. throw new TypeError(FUNC_ERROR_TEXT);
  7623. }
  7624. start = nativeMax(start === undefined ? (func.length - 1) : (+start || 0), 0);
  7625. return function() {
  7626. var args = arguments,
  7627. index = -1,
  7628. length = nativeMax(args.length - start, 0),
  7629. rest = Array(length);
  7630. while (++index < length) {
  7631. rest[index] = args[start + index];
  7632. }
  7633. switch (start) {
  7634. case 0: return func.call(this, rest);
  7635. case 1: return func.call(this, args[0], rest);
  7636. case 2: return func.call(this, args[0], args[1], rest);
  7637. }
  7638. var otherArgs = Array(start + 1);
  7639. index = -1;
  7640. while (++index < start) {
  7641. otherArgs[index] = args[index];
  7642. }
  7643. otherArgs[start] = rest;
  7644. return func.apply(this, otherArgs);
  7645. };
  7646. }
  7647. /**
  7648. * Creates a function that invokes `func` with the `this` binding of the created
  7649. * function and an array of arguments much like [`Function#apply`](https://es5.github.io/#x15.3.4.3).
  7650. *
  7651. * **Note:** This method is based on the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator).
  7652. *
  7653. * @static
  7654. * @memberOf _
  7655. * @category Function
  7656. * @param {Function} func The function to spread arguments over.
  7657. * @returns {Function} Returns the new function.
  7658. * @example
  7659. *
  7660. * var say = _.spread(function(who, what) {
  7661. * return who + ' says ' + what;
  7662. * });
  7663. *
  7664. * say(['fred', 'hello']);
  7665. * // => 'fred says hello'
  7666. *
  7667. * // with a Promise
  7668. * var numbers = Promise.all([
  7669. * Promise.resolve(40),
  7670. * Promise.resolve(36)
  7671. * ]);
  7672. *
  7673. * numbers.then(_.spread(function(x, y) {
  7674. * return x + y;
  7675. * }));
  7676. * // => a Promise of 76
  7677. */
  7678. function spread(func) {
  7679. if (typeof func != 'function') {
  7680. throw new TypeError(FUNC_ERROR_TEXT);
  7681. }
  7682. return function(array) {
  7683. return func.apply(this, array);
  7684. };
  7685. }
  7686. /**
  7687. * Creates a throttled function that only invokes `func` at most once per
  7688. * every `wait` milliseconds. The throttled function comes with a `cancel`
  7689. * method to cancel delayed invocations. Provide an options object to indicate
  7690. * that `func` should be invoked on the leading and/or trailing edge of the
  7691. * `wait` timeout. Subsequent calls to the throttled function return the
  7692. * result of the last `func` call.
  7693. *
  7694. * **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
  7695. * on the trailing edge of the timeout only if the the throttled function is
  7696. * invoked more than once during the `wait` timeout.
  7697. *
  7698. * See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation)
  7699. * for details over the differences between `_.throttle` and `_.debounce`.
  7700. *
  7701. * @static
  7702. * @memberOf _
  7703. * @category Function
  7704. * @param {Function} func The function to throttle.
  7705. * @param {number} [wait=0] The number of milliseconds to throttle invocations to.
  7706. * @param {Object} [options] The options object.
  7707. * @param {boolean} [options.leading=true] Specify invoking on the leading
  7708. * edge of the timeout.
  7709. * @param {boolean} [options.trailing=true] Specify invoking on the trailing
  7710. * edge of the timeout.
  7711. * @returns {Function} Returns the new throttled function.
  7712. * @example
  7713. *
  7714. * // avoid excessively updating the position while scrolling
  7715. * jQuery(window).on('scroll', _.throttle(updatePosition, 100));
  7716. *
  7717. * // invoke `renewToken` when the click event is fired, but not more than once every 5 minutes
  7718. * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {
  7719. * 'trailing': false
  7720. * }));
  7721. *
  7722. * // cancel a trailing throttled call
  7723. * jQuery(window).on('popstate', throttled.cancel);
  7724. */
  7725. function throttle(func, wait, options) {
  7726. var leading = true,
  7727. trailing = true;
  7728. if (typeof func != 'function') {
  7729. throw new TypeError(FUNC_ERROR_TEXT);
  7730. }
  7731. if (options === false) {
  7732. leading = false;
  7733. } else if (isObject(options)) {
  7734. leading = 'leading' in options ? !!options.leading : leading;
  7735. trailing = 'trailing' in options ? !!options.trailing : trailing;
  7736. }
  7737. return debounce(func, wait, { 'leading': leading, 'maxWait': +wait, 'trailing': trailing });
  7738. }
  7739. /**
  7740. * Creates a function that provides `value` to the wrapper function as its
  7741. * first argument. Any additional arguments provided to the function are
  7742. * appended to those provided to the wrapper function. The wrapper is invoked
  7743. * with the `this` binding of the created function.
  7744. *
  7745. * @static
  7746. * @memberOf _
  7747. * @category Function
  7748. * @param {*} value The value to wrap.
  7749. * @param {Function} wrapper The wrapper function.
  7750. * @returns {Function} Returns the new function.
  7751. * @example
  7752. *
  7753. * var p = _.wrap(_.escape, function(func, text) {
  7754. * return '<p>' + func(text) + '</p>';
  7755. * });
  7756. *
  7757. * p('fred, barney, & pebbles');
  7758. * // => '<p>fred, barney, &amp; pebbles</p>'
  7759. */
  7760. function wrap(value, wrapper) {
  7761. wrapper = wrapper == null ? identity : wrapper;
  7762. return createWrapper(wrapper, PARTIAL_FLAG, undefined, [value], []);
  7763. }
  7764. /*------------------------------------------------------------------------*/
  7765. /**
  7766. * Creates a clone of `value`. If `isDeep` is `true` nested objects are cloned,
  7767. * otherwise they are assigned by reference. If `customizer` is provided it is
  7768. * invoked to produce the cloned values. If `customizer` returns `undefined`
  7769. * cloning is handled by the method instead. The `customizer` is bound to
  7770. * `thisArg` and invoked with two argument; (value [, index|key, object]).
  7771. *
  7772. * **Note:** This method is loosely based on the
  7773. * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).
  7774. * The enumerable properties of `arguments` objects and objects created by
  7775. * constructors other than `Object` are cloned to plain `Object` objects. An
  7776. * empty object is returned for uncloneable values such as functions, DOM nodes,
  7777. * Maps, Sets, and WeakMaps.
  7778. *
  7779. * @static
  7780. * @memberOf _
  7781. * @category Lang
  7782. * @param {*} value The value to clone.
  7783. * @param {boolean} [isDeep] Specify a deep clone.
  7784. * @param {Function} [customizer] The function to customize cloning values.
  7785. * @param {*} [thisArg] The `this` binding of `customizer`.
  7786. * @returns {*} Returns the cloned value.
  7787. * @example
  7788. *
  7789. * var users = [
  7790. * { 'user': 'barney' },
  7791. * { 'user': 'fred' }
  7792. * ];
  7793. *
  7794. * var shallow = _.clone(users);
  7795. * shallow[0] === users[0];
  7796. * // => true
  7797. *
  7798. * var deep = _.clone(users, true);
  7799. * deep[0] === users[0];
  7800. * // => false
  7801. *
  7802. * // using a customizer callback
  7803. * var el = _.clone(document.body, function(value) {
  7804. * if (_.isElement(value)) {
  7805. * return value.cloneNode(false);
  7806. * }
  7807. * });
  7808. *
  7809. * el === document.body
  7810. * // => false
  7811. * el.nodeName
  7812. * // => BODY
  7813. * el.childNodes.length;
  7814. * // => 0
  7815. */
  7816. function clone(value, isDeep, customizer, thisArg) {
  7817. if (isDeep && typeof isDeep != 'boolean' && isIterateeCall(value, isDeep, customizer)) {
  7818. isDeep = false;
  7819. }
  7820. else if (typeof isDeep == 'function') {
  7821. thisArg = customizer;
  7822. customizer = isDeep;
  7823. isDeep = false;
  7824. }
  7825. return typeof customizer == 'function'
  7826. ? baseClone(value, isDeep, bindCallback(customizer, thisArg, 1))
  7827. : baseClone(value, isDeep);
  7828. }
  7829. /**
  7830. * Creates a deep clone of `value`. If `customizer` is provided it is invoked
  7831. * to produce the cloned values. If `customizer` returns `undefined` cloning
  7832. * is handled by the method instead. The `customizer` is bound to `thisArg`
  7833. * and invoked with two argument; (value [, index|key, object]).
  7834. *
  7835. * **Note:** This method is loosely based on the
  7836. * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).
  7837. * The enumerable properties of `arguments` objects and objects created by
  7838. * constructors other than `Object` are cloned to plain `Object` objects. An
  7839. * empty object is returned for uncloneable values such as functions, DOM nodes,
  7840. * Maps, Sets, and WeakMaps.
  7841. *
  7842. * @static
  7843. * @memberOf _
  7844. * @category Lang
  7845. * @param {*} value The value to deep clone.
  7846. * @param {Function} [customizer] The function to customize cloning values.
  7847. * @param {*} [thisArg] The `this` binding of `customizer`.
  7848. * @returns {*} Returns the deep cloned value.
  7849. * @example
  7850. *
  7851. * var users = [
  7852. * { 'user': 'barney' },
  7853. * { 'user': 'fred' }
  7854. * ];
  7855. *
  7856. * var deep = _.cloneDeep(users);
  7857. * deep[0] === users[0];
  7858. * // => false
  7859. *
  7860. * // using a customizer callback
  7861. * var el = _.cloneDeep(document.body, function(value) {
  7862. * if (_.isElement(value)) {
  7863. * return value.cloneNode(true);
  7864. * }
  7865. * });
  7866. *
  7867. * el === document.body
  7868. * // => false
  7869. * el.nodeName
  7870. * // => BODY
  7871. * el.childNodes.length;
  7872. * // => 20
  7873. */
  7874. function cloneDeep(value, customizer, thisArg) {
  7875. return typeof customizer == 'function'
  7876. ? baseClone(value, true, bindCallback(customizer, thisArg, 1))
  7877. : baseClone(value, true);
  7878. }
  7879. /**
  7880. * Checks if `value` is greater than `other`.
  7881. *
  7882. * @static
  7883. * @memberOf _
  7884. * @category Lang
  7885. * @param {*} value The value to compare.
  7886. * @param {*} other The other value to compare.
  7887. * @returns {boolean} Returns `true` if `value` is greater than `other`, else `false`.
  7888. * @example
  7889. *
  7890. * _.gt(3, 1);
  7891. * // => true
  7892. *
  7893. * _.gt(3, 3);
  7894. * // => false
  7895. *
  7896. * _.gt(1, 3);
  7897. * // => false
  7898. */
  7899. function gt(value, other) {
  7900. return value > other;
  7901. }
  7902. /**
  7903. * Checks if `value` is greater than or equal to `other`.
  7904. *
  7905. * @static
  7906. * @memberOf _
  7907. * @category Lang
  7908. * @param {*} value The value to compare.
  7909. * @param {*} other The other value to compare.
  7910. * @returns {boolean} Returns `true` if `value` is greater than or equal to `other`, else `false`.
  7911. * @example
  7912. *
  7913. * _.gte(3, 1);
  7914. * // => true
  7915. *
  7916. * _.gte(3, 3);
  7917. * // => true
  7918. *
  7919. * _.gte(1, 3);
  7920. * // => false
  7921. */
  7922. function gte(value, other) {
  7923. return value >= other;
  7924. }
  7925. /**
  7926. * Checks if `value` is classified as an `arguments` object.
  7927. *
  7928. * @static
  7929. * @memberOf _
  7930. * @category Lang
  7931. * @param {*} value The value to check.
  7932. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  7933. * @example
  7934. *
  7935. * _.isArguments(function() { return arguments; }());
  7936. * // => true
  7937. *
  7938. * _.isArguments([1, 2, 3]);
  7939. * // => false
  7940. */
  7941. function isArguments(value) {
  7942. return isObjectLike(value) && isArrayLike(value) &&
  7943. hasOwnProperty.call(value, 'callee') && !propertyIsEnumerable.call(value, 'callee');
  7944. }
  7945. /**
  7946. * Checks if `value` is classified as an `Array` object.
  7947. *
  7948. * @static
  7949. * @memberOf _
  7950. * @category Lang
  7951. * @param {*} value The value to check.
  7952. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  7953. * @example
  7954. *
  7955. * _.isArray([1, 2, 3]);
  7956. * // => true
  7957. *
  7958. * _.isArray(function() { return arguments; }());
  7959. * // => false
  7960. */
  7961. var isArray = nativeIsArray || function(value) {
  7962. return isObjectLike(value) && isLength(value.length) && objToString.call(value) == arrayTag;
  7963. };
  7964. /**
  7965. * Checks if `value` is classified as a boolean primitive or object.
  7966. *
  7967. * @static
  7968. * @memberOf _
  7969. * @category Lang
  7970. * @param {*} value The value to check.
  7971. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  7972. * @example
  7973. *
  7974. * _.isBoolean(false);
  7975. * // => true
  7976. *
  7977. * _.isBoolean(null);
  7978. * // => false
  7979. */
  7980. function isBoolean(value) {
  7981. return value === true || value === false || (isObjectLike(value) && objToString.call(value) == boolTag);
  7982. }
  7983. /**
  7984. * Checks if `value` is classified as a `Date` object.
  7985. *
  7986. * @static
  7987. * @memberOf _
  7988. * @category Lang
  7989. * @param {*} value The value to check.
  7990. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  7991. * @example
  7992. *
  7993. * _.isDate(new Date);
  7994. * // => true
  7995. *
  7996. * _.isDate('Mon April 23 2012');
  7997. * // => false
  7998. */
  7999. function isDate(value) {
  8000. return isObjectLike(value) && objToString.call(value) == dateTag;
  8001. }
  8002. /**
  8003. * Checks if `value` is a DOM element.
  8004. *
  8005. * @static
  8006. * @memberOf _
  8007. * @category Lang
  8008. * @param {*} value The value to check.
  8009. * @returns {boolean} Returns `true` if `value` is a DOM element, else `false`.
  8010. * @example
  8011. *
  8012. * _.isElement(document.body);
  8013. * // => true
  8014. *
  8015. * _.isElement('<body>');
  8016. * // => false
  8017. */
  8018. function isElement(value) {
  8019. return !!value && value.nodeType === 1 && isObjectLike(value) && !isPlainObject(value);
  8020. }
  8021. /**
  8022. * Checks if `value` is empty. A value is considered empty unless it is an
  8023. * `arguments` object, array, string, or jQuery-like collection with a length
  8024. * greater than `0` or an object with own enumerable properties.
  8025. *
  8026. * @static
  8027. * @memberOf _
  8028. * @category Lang
  8029. * @param {Array|Object|string} value The value to inspect.
  8030. * @returns {boolean} Returns `true` if `value` is empty, else `false`.
  8031. * @example
  8032. *
  8033. * _.isEmpty(null);
  8034. * // => true
  8035. *
  8036. * _.isEmpty(true);
  8037. * // => true
  8038. *
  8039. * _.isEmpty(1);
  8040. * // => true
  8041. *
  8042. * _.isEmpty([1, 2, 3]);
  8043. * // => false
  8044. *
  8045. * _.isEmpty({ 'a': 1 });
  8046. * // => false
  8047. */
  8048. function isEmpty(value) {
  8049. if (value == null) {
  8050. return true;
  8051. }
  8052. if (isArrayLike(value) && (isArray(value) || isString(value) || isArguments(value) ||
  8053. (isObjectLike(value) && isFunction(value.splice)))) {
  8054. return !value.length;
  8055. }
  8056. return !keys(value).length;
  8057. }
  8058. /**
  8059. * Performs a deep comparison between two values to determine if they are
  8060. * equivalent. If `customizer` is provided it is invoked to compare values.
  8061. * If `customizer` returns `undefined` comparisons are handled by the method
  8062. * instead. The `customizer` is bound to `thisArg` and invoked with three
  8063. * arguments: (value, other [, index|key]).
  8064. *
  8065. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  8066. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  8067. * their own, not inherited, enumerable properties. Functions and DOM nodes
  8068. * are **not** supported. Provide a customizer function to extend support
  8069. * for comparing other values.
  8070. *
  8071. * @static
  8072. * @memberOf _
  8073. * @alias eq
  8074. * @category Lang
  8075. * @param {*} value The value to compare.
  8076. * @param {*} other The other value to compare.
  8077. * @param {Function} [customizer] The function to customize value comparisons.
  8078. * @param {*} [thisArg] The `this` binding of `customizer`.
  8079. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  8080. * @example
  8081. *
  8082. * var object = { 'user': 'fred' };
  8083. * var other = { 'user': 'fred' };
  8084. *
  8085. * object == other;
  8086. * // => false
  8087. *
  8088. * _.isEqual(object, other);
  8089. * // => true
  8090. *
  8091. * // using a customizer callback
  8092. * var array = ['hello', 'goodbye'];
  8093. * var other = ['hi', 'goodbye'];
  8094. *
  8095. * _.isEqual(array, other, function(value, other) {
  8096. * if (_.every([value, other], RegExp.prototype.test, /^h(?:i|ello)$/)) {
  8097. * return true;
  8098. * }
  8099. * });
  8100. * // => true
  8101. */
  8102. function isEqual(value, other, customizer, thisArg) {
  8103. customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined;
  8104. var result = customizer ? customizer(value, other) : undefined;
  8105. return result === undefined ? baseIsEqual(value, other, customizer) : !!result;
  8106. }
  8107. /**
  8108. * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`,
  8109. * `SyntaxError`, `TypeError`, or `URIError` object.
  8110. *
  8111. * @static
  8112. * @memberOf _
  8113. * @category Lang
  8114. * @param {*} value The value to check.
  8115. * @returns {boolean} Returns `true` if `value` is an error object, else `false`.
  8116. * @example
  8117. *
  8118. * _.isError(new Error);
  8119. * // => true
  8120. *
  8121. * _.isError(Error);
  8122. * // => false
  8123. */
  8124. function isError(value) {
  8125. return isObjectLike(value) && typeof value.message == 'string' && objToString.call(value) == errorTag;
  8126. }
  8127. /**
  8128. * Checks if `value` is a finite primitive number.
  8129. *
  8130. * **Note:** This method is based on [`Number.isFinite`](http://ecma-international.org/ecma-262/6.0/#sec-number.isfinite).
  8131. *
  8132. * @static
  8133. * @memberOf _
  8134. * @category Lang
  8135. * @param {*} value The value to check.
  8136. * @returns {boolean} Returns `true` if `value` is a finite number, else `false`.
  8137. * @example
  8138. *
  8139. * _.isFinite(10);
  8140. * // => true
  8141. *
  8142. * _.isFinite('10');
  8143. * // => false
  8144. *
  8145. * _.isFinite(true);
  8146. * // => false
  8147. *
  8148. * _.isFinite(Object(10));
  8149. * // => false
  8150. *
  8151. * _.isFinite(Infinity);
  8152. * // => false
  8153. */
  8154. function isFinite(value) {
  8155. return typeof value == 'number' && nativeIsFinite(value);
  8156. }
  8157. /**
  8158. * Checks if `value` is classified as a `Function` object.
  8159. *
  8160. * @static
  8161. * @memberOf _
  8162. * @category Lang
  8163. * @param {*} value The value to check.
  8164. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  8165. * @example
  8166. *
  8167. * _.isFunction(_);
  8168. * // => true
  8169. *
  8170. * _.isFunction(/abc/);
  8171. * // => false
  8172. */
  8173. function isFunction(value) {
  8174. // The use of `Object#toString` avoids issues with the `typeof` operator
  8175. // in older versions of Chrome and Safari which return 'function' for regexes
  8176. // and Safari 8 equivalents which return 'object' for typed array constructors.
  8177. return isObject(value) && objToString.call(value) == funcTag;
  8178. }
  8179. /**
  8180. * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
  8181. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  8182. *
  8183. * @static
  8184. * @memberOf _
  8185. * @category Lang
  8186. * @param {*} value The value to check.
  8187. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  8188. * @example
  8189. *
  8190. * _.isObject({});
  8191. * // => true
  8192. *
  8193. * _.isObject([1, 2, 3]);
  8194. * // => true
  8195. *
  8196. * _.isObject(1);
  8197. * // => false
  8198. */
  8199. function isObject(value) {
  8200. // Avoid a V8 JIT bug in Chrome 19-20.
  8201. // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
  8202. var type = typeof value;
  8203. return !!value && (type == 'object' || type == 'function');
  8204. }
  8205. /**
  8206. * Performs a deep comparison between `object` and `source` to determine if
  8207. * `object` contains equivalent property values. If `customizer` is provided
  8208. * it is invoked to compare values. If `customizer` returns `undefined`
  8209. * comparisons are handled by the method instead. The `customizer` is bound
  8210. * to `thisArg` and invoked with three arguments: (value, other, index|key).
  8211. *
  8212. * **Note:** This method supports comparing properties of arrays, booleans,
  8213. * `Date` objects, numbers, `Object` objects, regexes, and strings. Functions
  8214. * and DOM nodes are **not** supported. Provide a customizer function to extend
  8215. * support for comparing other values.
  8216. *
  8217. * @static
  8218. * @memberOf _
  8219. * @category Lang
  8220. * @param {Object} object The object to inspect.
  8221. * @param {Object} source The object of property values to match.
  8222. * @param {Function} [customizer] The function to customize value comparisons.
  8223. * @param {*} [thisArg] The `this` binding of `customizer`.
  8224. * @returns {boolean} Returns `true` if `object` is a match, else `false`.
  8225. * @example
  8226. *
  8227. * var object = { 'user': 'fred', 'age': 40 };
  8228. *
  8229. * _.isMatch(object, { 'age': 40 });
  8230. * // => true
  8231. *
  8232. * _.isMatch(object, { 'age': 36 });
  8233. * // => false
  8234. *
  8235. * // using a customizer callback
  8236. * var object = { 'greeting': 'hello' };
  8237. * var source = { 'greeting': 'hi' };
  8238. *
  8239. * _.isMatch(object, source, function(value, other) {
  8240. * return _.every([value, other], RegExp.prototype.test, /^h(?:i|ello)$/) || undefined;
  8241. * });
  8242. * // => true
  8243. */
  8244. function isMatch(object, source, customizer, thisArg) {
  8245. customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined;
  8246. return baseIsMatch(object, getMatchData(source), customizer);
  8247. }
  8248. /**
  8249. * Checks if `value` is `NaN`.
  8250. *
  8251. * **Note:** This method is not the same as [`isNaN`](https://es5.github.io/#x15.1.2.4)
  8252. * which returns `true` for `undefined` and other non-numeric values.
  8253. *
  8254. * @static
  8255. * @memberOf _
  8256. * @category Lang
  8257. * @param {*} value The value to check.
  8258. * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`.
  8259. * @example
  8260. *
  8261. * _.isNaN(NaN);
  8262. * // => true
  8263. *
  8264. * _.isNaN(new Number(NaN));
  8265. * // => true
  8266. *
  8267. * isNaN(undefined);
  8268. * // => true
  8269. *
  8270. * _.isNaN(undefined);
  8271. * // => false
  8272. */
  8273. function isNaN(value) {
  8274. // An `NaN` primitive is the only value that is not equal to itself.
  8275. // Perform the `toStringTag` check first to avoid errors with some host objects in IE.
  8276. return isNumber(value) && value != +value;
  8277. }
  8278. /**
  8279. * Checks if `value` is a native function.
  8280. *
  8281. * @static
  8282. * @memberOf _
  8283. * @category Lang
  8284. * @param {*} value The value to check.
  8285. * @returns {boolean} Returns `true` if `value` is a native function, else `false`.
  8286. * @example
  8287. *
  8288. * _.isNative(Array.prototype.push);
  8289. * // => true
  8290. *
  8291. * _.isNative(_);
  8292. * // => false
  8293. */
  8294. function isNative(value) {
  8295. if (value == null) {
  8296. return false;
  8297. }
  8298. if (isFunction(value)) {
  8299. return reIsNative.test(fnToString.call(value));
  8300. }
  8301. return isObjectLike(value) && reIsHostCtor.test(value);
  8302. }
  8303. /**
  8304. * Checks if `value` is `null`.
  8305. *
  8306. * @static
  8307. * @memberOf _
  8308. * @category Lang
  8309. * @param {*} value The value to check.
  8310. * @returns {boolean} Returns `true` if `value` is `null`, else `false`.
  8311. * @example
  8312. *
  8313. * _.isNull(null);
  8314. * // => true
  8315. *
  8316. * _.isNull(void 0);
  8317. * // => false
  8318. */
  8319. function isNull(value) {
  8320. return value === null;
  8321. }
  8322. /**
  8323. * Checks if `value` is classified as a `Number` primitive or object.
  8324. *
  8325. * **Note:** To exclude `Infinity`, `-Infinity`, and `NaN`, which are classified
  8326. * as numbers, use the `_.isFinite` method.
  8327. *
  8328. * @static
  8329. * @memberOf _
  8330. * @category Lang
  8331. * @param {*} value The value to check.
  8332. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  8333. * @example
  8334. *
  8335. * _.isNumber(8.4);
  8336. * // => true
  8337. *
  8338. * _.isNumber(NaN);
  8339. * // => true
  8340. *
  8341. * _.isNumber('8.4');
  8342. * // => false
  8343. */
  8344. function isNumber(value) {
  8345. return typeof value == 'number' || (isObjectLike(value) && objToString.call(value) == numberTag);
  8346. }
  8347. /**
  8348. * Checks if `value` is a plain object, that is, an object created by the
  8349. * `Object` constructor or one with a `[[Prototype]]` of `null`.
  8350. *
  8351. * **Note:** This method assumes objects created by the `Object` constructor
  8352. * have no inherited enumerable properties.
  8353. *
  8354. * @static
  8355. * @memberOf _
  8356. * @category Lang
  8357. * @param {*} value The value to check.
  8358. * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
  8359. * @example
  8360. *
  8361. * function Foo() {
  8362. * this.a = 1;
  8363. * }
  8364. *
  8365. * _.isPlainObject(new Foo);
  8366. * // => false
  8367. *
  8368. * _.isPlainObject([1, 2, 3]);
  8369. * // => false
  8370. *
  8371. * _.isPlainObject({ 'x': 0, 'y': 0 });
  8372. * // => true
  8373. *
  8374. * _.isPlainObject(Object.create(null));
  8375. * // => true
  8376. */
  8377. function isPlainObject(value) {
  8378. var Ctor;
  8379. // Exit early for non `Object` objects.
  8380. if (!(isObjectLike(value) && objToString.call(value) == objectTag && !isArguments(value)) ||
  8381. (!hasOwnProperty.call(value, 'constructor') && (Ctor = value.constructor, typeof Ctor == 'function' && !(Ctor instanceof Ctor)))) {
  8382. return false;
  8383. }
  8384. // IE < 9 iterates inherited properties before own properties. If the first
  8385. // iterated property is an object's own property then there are no inherited
  8386. // enumerable properties.
  8387. var result;
  8388. // In most environments an object's own properties are iterated before
  8389. // its inherited properties. If the last iterated property is an object's
  8390. // own property then there are no inherited enumerable properties.
  8391. baseForIn(value, function(subValue, key) {
  8392. result = key;
  8393. });
  8394. return result === undefined || hasOwnProperty.call(value, result);
  8395. }
  8396. /**
  8397. * Checks if `value` is classified as a `RegExp` object.
  8398. *
  8399. * @static
  8400. * @memberOf _
  8401. * @category Lang
  8402. * @param {*} value The value to check.
  8403. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  8404. * @example
  8405. *
  8406. * _.isRegExp(/abc/);
  8407. * // => true
  8408. *
  8409. * _.isRegExp('/abc/');
  8410. * // => false
  8411. */
  8412. function isRegExp(value) {
  8413. return isObject(value) && objToString.call(value) == regexpTag;
  8414. }
  8415. /**
  8416. * Checks if `value` is classified as a `String` primitive or object.
  8417. *
  8418. * @static
  8419. * @memberOf _
  8420. * @category Lang
  8421. * @param {*} value The value to check.
  8422. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  8423. * @example
  8424. *
  8425. * _.isString('abc');
  8426. * // => true
  8427. *
  8428. * _.isString(1);
  8429. * // => false
  8430. */
  8431. function isString(value) {
  8432. return typeof value == 'string' || (isObjectLike(value) && objToString.call(value) == stringTag);
  8433. }
  8434. /**
  8435. * Checks if `value` is classified as a typed array.
  8436. *
  8437. * @static
  8438. * @memberOf _
  8439. * @category Lang
  8440. * @param {*} value The value to check.
  8441. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  8442. * @example
  8443. *
  8444. * _.isTypedArray(new Uint8Array);
  8445. * // => true
  8446. *
  8447. * _.isTypedArray([]);
  8448. * // => false
  8449. */
  8450. function isTypedArray(value) {
  8451. return isObjectLike(value) && isLength(value.length) && !!typedArrayTags[objToString.call(value)];
  8452. }
  8453. /**
  8454. * Checks if `value` is `undefined`.
  8455. *
  8456. * @static
  8457. * @memberOf _
  8458. * @category Lang
  8459. * @param {*} value The value to check.
  8460. * @returns {boolean} Returns `true` if `value` is `undefined`, else `false`.
  8461. * @example
  8462. *
  8463. * _.isUndefined(void 0);
  8464. * // => true
  8465. *
  8466. * _.isUndefined(null);
  8467. * // => false
  8468. */
  8469. function isUndefined(value) {
  8470. return value === undefined;
  8471. }
  8472. /**
  8473. * Checks if `value` is less than `other`.
  8474. *
  8475. * @static
  8476. * @memberOf _
  8477. * @category Lang
  8478. * @param {*} value The value to compare.
  8479. * @param {*} other The other value to compare.
  8480. * @returns {boolean} Returns `true` if `value` is less than `other`, else `false`.
  8481. * @example
  8482. *
  8483. * _.lt(1, 3);
  8484. * // => true
  8485. *
  8486. * _.lt(3, 3);
  8487. * // => false
  8488. *
  8489. * _.lt(3, 1);
  8490. * // => false
  8491. */
  8492. function lt(value, other) {
  8493. return value < other;
  8494. }
  8495. /**
  8496. * Checks if `value` is less than or equal to `other`.
  8497. *
  8498. * @static
  8499. * @memberOf _
  8500. * @category Lang
  8501. * @param {*} value The value to compare.
  8502. * @param {*} other The other value to compare.
  8503. * @returns {boolean} Returns `true` if `value` is less than or equal to `other`, else `false`.
  8504. * @example
  8505. *
  8506. * _.lte(1, 3);
  8507. * // => true
  8508. *
  8509. * _.lte(3, 3);
  8510. * // => true
  8511. *
  8512. * _.lte(3, 1);
  8513. * // => false
  8514. */
  8515. function lte(value, other) {
  8516. return value <= other;
  8517. }
  8518. /**
  8519. * Converts `value` to an array.
  8520. *
  8521. * @static
  8522. * @memberOf _
  8523. * @category Lang
  8524. * @param {*} value The value to convert.
  8525. * @returns {Array} Returns the converted array.
  8526. * @example
  8527. *
  8528. * (function() {
  8529. * return _.toArray(arguments).slice(1);
  8530. * }(1, 2, 3));
  8531. * // => [2, 3]
  8532. */
  8533. function toArray(value) {
  8534. var length = value ? getLength(value) : 0;
  8535. if (!isLength(length)) {
  8536. return values(value);
  8537. }
  8538. if (!length) {
  8539. return [];
  8540. }
  8541. return arrayCopy(value);
  8542. }
  8543. /**
  8544. * Converts `value` to a plain object flattening inherited enumerable
  8545. * properties of `value` to own properties of the plain object.
  8546. *
  8547. * @static
  8548. * @memberOf _
  8549. * @category Lang
  8550. * @param {*} value The value to convert.
  8551. * @returns {Object} Returns the converted plain object.
  8552. * @example
  8553. *
  8554. * function Foo() {
  8555. * this.b = 2;
  8556. * }
  8557. *
  8558. * Foo.prototype.c = 3;
  8559. *
  8560. * _.assign({ 'a': 1 }, new Foo);
  8561. * // => { 'a': 1, 'b': 2 }
  8562. *
  8563. * _.assign({ 'a': 1 }, _.toPlainObject(new Foo));
  8564. * // => { 'a': 1, 'b': 2, 'c': 3 }
  8565. */
  8566. function toPlainObject(value) {
  8567. return baseCopy(value, keysIn(value));
  8568. }
  8569. /*------------------------------------------------------------------------*/
  8570. /**
  8571. * Recursively merges own enumerable properties of the source object(s), that
  8572. * don't resolve to `undefined` into the destination object. Subsequent sources
  8573. * overwrite property assignments of previous sources. If `customizer` is
  8574. * provided it is invoked to produce the merged values of the destination and
  8575. * source properties. If `customizer` returns `undefined` merging is handled
  8576. * by the method instead. The `customizer` is bound to `thisArg` and invoked
  8577. * with five arguments: (objectValue, sourceValue, key, object, source).
  8578. *
  8579. * @static
  8580. * @memberOf _
  8581. * @category Object
  8582. * @param {Object} object The destination object.
  8583. * @param {...Object} [sources] The source objects.
  8584. * @param {Function} [customizer] The function to customize assigned values.
  8585. * @param {*} [thisArg] The `this` binding of `customizer`.
  8586. * @returns {Object} Returns `object`.
  8587. * @example
  8588. *
  8589. * var users = {
  8590. * 'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
  8591. * };
  8592. *
  8593. * var ages = {
  8594. * 'data': [{ 'age': 36 }, { 'age': 40 }]
  8595. * };
  8596. *
  8597. * _.merge(users, ages);
  8598. * // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }
  8599. *
  8600. * // using a customizer callback
  8601. * var object = {
  8602. * 'fruits': ['apple'],
  8603. * 'vegetables': ['beet']
  8604. * };
  8605. *
  8606. * var other = {
  8607. * 'fruits': ['banana'],
  8608. * 'vegetables': ['carrot']
  8609. * };
  8610. *
  8611. * _.merge(object, other, function(a, b) {
  8612. * if (_.isArray(a)) {
  8613. * return a.concat(b);
  8614. * }
  8615. * });
  8616. * // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] }
  8617. */
  8618. var merge = createAssigner(baseMerge);
  8619. /**
  8620. * Assigns own enumerable properties of source object(s) to the destination
  8621. * object. Subsequent sources overwrite property assignments of previous sources.
  8622. * If `customizer` is provided it is invoked to produce the assigned values.
  8623. * The `customizer` is bound to `thisArg` and invoked with five arguments:
  8624. * (objectValue, sourceValue, key, object, source).
  8625. *
  8626. * **Note:** This method mutates `object` and is based on
  8627. * [`Object.assign`](http://ecma-international.org/ecma-262/6.0/#sec-object.assign).
  8628. *
  8629. * @static
  8630. * @memberOf _
  8631. * @alias extend
  8632. * @category Object
  8633. * @param {Object} object The destination object.
  8634. * @param {...Object} [sources] The source objects.
  8635. * @param {Function} [customizer] The function to customize assigned values.
  8636. * @param {*} [thisArg] The `this` binding of `customizer`.
  8637. * @returns {Object} Returns `object`.
  8638. * @example
  8639. *
  8640. * _.assign({ 'user': 'barney' }, { 'age': 40 }, { 'user': 'fred' });
  8641. * // => { 'user': 'fred', 'age': 40 }
  8642. *
  8643. * // using a customizer callback
  8644. * var defaults = _.partialRight(_.assign, function(value, other) {
  8645. * return _.isUndefined(value) ? other : value;
  8646. * });
  8647. *
  8648. * defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
  8649. * // => { 'user': 'barney', 'age': 36 }
  8650. */
  8651. var assign = createAssigner(function(object, source, customizer) {
  8652. return customizer
  8653. ? assignWith(object, source, customizer)
  8654. : baseAssign(object, source);
  8655. });
  8656. /**
  8657. * Creates an object that inherits from the given `prototype` object. If a
  8658. * `properties` object is provided its own enumerable properties are assigned
  8659. * to the created object.
  8660. *
  8661. * @static
  8662. * @memberOf _
  8663. * @category Object
  8664. * @param {Object} prototype The object to inherit from.
  8665. * @param {Object} [properties] The properties to assign to the object.
  8666. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  8667. * @returns {Object} Returns the new object.
  8668. * @example
  8669. *
  8670. * function Shape() {
  8671. * this.x = 0;
  8672. * this.y = 0;
  8673. * }
  8674. *
  8675. * function Circle() {
  8676. * Shape.call(this);
  8677. * }
  8678. *
  8679. * Circle.prototype = _.create(Shape.prototype, {
  8680. * 'constructor': Circle
  8681. * });
  8682. *
  8683. * var circle = new Circle;
  8684. * circle instanceof Circle;
  8685. * // => true
  8686. *
  8687. * circle instanceof Shape;
  8688. * // => true
  8689. */
  8690. function create(prototype, properties, guard) {
  8691. var result = baseCreate(prototype);
  8692. if (guard && isIterateeCall(prototype, properties, guard)) {
  8693. properties = undefined;
  8694. }
  8695. return properties ? baseAssign(result, properties) : result;
  8696. }
  8697. /**
  8698. * Assigns own enumerable properties of source object(s) to the destination
  8699. * object for all destination properties that resolve to `undefined`. Once a
  8700. * property is set, additional values of the same property are ignored.
  8701. *
  8702. * **Note:** This method mutates `object`.
  8703. *
  8704. * @static
  8705. * @memberOf _
  8706. * @category Object
  8707. * @param {Object} object The destination object.
  8708. * @param {...Object} [sources] The source objects.
  8709. * @returns {Object} Returns `object`.
  8710. * @example
  8711. *
  8712. * _.defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
  8713. * // => { 'user': 'barney', 'age': 36 }
  8714. */
  8715. var defaults = createDefaults(assign, assignDefaults);
  8716. /**
  8717. * This method is like `_.defaults` except that it recursively assigns
  8718. * default properties.
  8719. *
  8720. * **Note:** This method mutates `object`.
  8721. *
  8722. * @static
  8723. * @memberOf _
  8724. * @category Object
  8725. * @param {Object} object The destination object.
  8726. * @param {...Object} [sources] The source objects.
  8727. * @returns {Object} Returns `object`.
  8728. * @example
  8729. *
  8730. * _.defaultsDeep({ 'user': { 'name': 'barney' } }, { 'user': { 'name': 'fred', 'age': 36 } });
  8731. * // => { 'user': { 'name': 'barney', 'age': 36 } }
  8732. *
  8733. */
  8734. var defaultsDeep = createDefaults(merge, mergeDefaults);
  8735. /**
  8736. * This method is like `_.find` except that it returns the key of the first
  8737. * element `predicate` returns truthy for instead of the element itself.
  8738. *
  8739. * If a property name is provided for `predicate` the created `_.property`
  8740. * style callback returns the property value of the given element.
  8741. *
  8742. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  8743. * style callback returns `true` for elements that have a matching property
  8744. * value, else `false`.
  8745. *
  8746. * If an object is provided for `predicate` the created `_.matches` style
  8747. * callback returns `true` for elements that have the properties of the given
  8748. * object, else `false`.
  8749. *
  8750. * @static
  8751. * @memberOf _
  8752. * @category Object
  8753. * @param {Object} object The object to search.
  8754. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  8755. * per iteration.
  8756. * @param {*} [thisArg] The `this` binding of `predicate`.
  8757. * @returns {string|undefined} Returns the key of the matched element, else `undefined`.
  8758. * @example
  8759. *
  8760. * var users = {
  8761. * 'barney': { 'age': 36, 'active': true },
  8762. * 'fred': { 'age': 40, 'active': false },
  8763. * 'pebbles': { 'age': 1, 'active': true }
  8764. * };
  8765. *
  8766. * _.findKey(users, function(chr) {
  8767. * return chr.age < 40;
  8768. * });
  8769. * // => 'barney' (iteration order is not guaranteed)
  8770. *
  8771. * // using the `_.matches` callback shorthand
  8772. * _.findKey(users, { 'age': 1, 'active': true });
  8773. * // => 'pebbles'
  8774. *
  8775. * // using the `_.matchesProperty` callback shorthand
  8776. * _.findKey(users, 'active', false);
  8777. * // => 'fred'
  8778. *
  8779. * // using the `_.property` callback shorthand
  8780. * _.findKey(users, 'active');
  8781. * // => 'barney'
  8782. */
  8783. var findKey = createFindKey(baseForOwn);
  8784. /**
  8785. * This method is like `_.findKey` except that it iterates over elements of
  8786. * a collection in the opposite order.
  8787. *
  8788. * If a property name is provided for `predicate` the created `_.property`
  8789. * style callback returns the property value of the given element.
  8790. *
  8791. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  8792. * style callback returns `true` for elements that have a matching property
  8793. * value, else `false`.
  8794. *
  8795. * If an object is provided for `predicate` the created `_.matches` style
  8796. * callback returns `true` for elements that have the properties of the given
  8797. * object, else `false`.
  8798. *
  8799. * @static
  8800. * @memberOf _
  8801. * @category Object
  8802. * @param {Object} object The object to search.
  8803. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  8804. * per iteration.
  8805. * @param {*} [thisArg] The `this` binding of `predicate`.
  8806. * @returns {string|undefined} Returns the key of the matched element, else `undefined`.
  8807. * @example
  8808. *
  8809. * var users = {
  8810. * 'barney': { 'age': 36, 'active': true },
  8811. * 'fred': { 'age': 40, 'active': false },
  8812. * 'pebbles': { 'age': 1, 'active': true }
  8813. * };
  8814. *
  8815. * _.findLastKey(users, function(chr) {
  8816. * return chr.age < 40;
  8817. * });
  8818. * // => returns `pebbles` assuming `_.findKey` returns `barney`
  8819. *
  8820. * // using the `_.matches` callback shorthand
  8821. * _.findLastKey(users, { 'age': 36, 'active': true });
  8822. * // => 'barney'
  8823. *
  8824. * // using the `_.matchesProperty` callback shorthand
  8825. * _.findLastKey(users, 'active', false);
  8826. * // => 'fred'
  8827. *
  8828. * // using the `_.property` callback shorthand
  8829. * _.findLastKey(users, 'active');
  8830. * // => 'pebbles'
  8831. */
  8832. var findLastKey = createFindKey(baseForOwnRight);
  8833. /**
  8834. * Iterates over own and inherited enumerable properties of an object invoking
  8835. * `iteratee` for each property. The `iteratee` is bound to `thisArg` and invoked
  8836. * with three arguments: (value, key, object). Iteratee functions may exit
  8837. * iteration early by explicitly returning `false`.
  8838. *
  8839. * @static
  8840. * @memberOf _
  8841. * @category Object
  8842. * @param {Object} object The object to iterate over.
  8843. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8844. * @param {*} [thisArg] The `this` binding of `iteratee`.
  8845. * @returns {Object} Returns `object`.
  8846. * @example
  8847. *
  8848. * function Foo() {
  8849. * this.a = 1;
  8850. * this.b = 2;
  8851. * }
  8852. *
  8853. * Foo.prototype.c = 3;
  8854. *
  8855. * _.forIn(new Foo, function(value, key) {
  8856. * console.log(key);
  8857. * });
  8858. * // => logs 'a', 'b', and 'c' (iteration order is not guaranteed)
  8859. */
  8860. var forIn = createForIn(baseFor);
  8861. /**
  8862. * This method is like `_.forIn` except that it iterates over properties of
  8863. * `object` in the opposite order.
  8864. *
  8865. * @static
  8866. * @memberOf _
  8867. * @category Object
  8868. * @param {Object} object The object to iterate over.
  8869. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8870. * @param {*} [thisArg] The `this` binding of `iteratee`.
  8871. * @returns {Object} Returns `object`.
  8872. * @example
  8873. *
  8874. * function Foo() {
  8875. * this.a = 1;
  8876. * this.b = 2;
  8877. * }
  8878. *
  8879. * Foo.prototype.c = 3;
  8880. *
  8881. * _.forInRight(new Foo, function(value, key) {
  8882. * console.log(key);
  8883. * });
  8884. * // => logs 'c', 'b', and 'a' assuming `_.forIn ` logs 'a', 'b', and 'c'
  8885. */
  8886. var forInRight = createForIn(baseForRight);
  8887. /**
  8888. * Iterates over own enumerable properties of an object invoking `iteratee`
  8889. * for each property. The `iteratee` is bound to `thisArg` and invoked with
  8890. * three arguments: (value, key, object). Iteratee functions may exit iteration
  8891. * early by explicitly returning `false`.
  8892. *
  8893. * @static
  8894. * @memberOf _
  8895. * @category Object
  8896. * @param {Object} object The object to iterate over.
  8897. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8898. * @param {*} [thisArg] The `this` binding of `iteratee`.
  8899. * @returns {Object} Returns `object`.
  8900. * @example
  8901. *
  8902. * function Foo() {
  8903. * this.a = 1;
  8904. * this.b = 2;
  8905. * }
  8906. *
  8907. * Foo.prototype.c = 3;
  8908. *
  8909. * _.forOwn(new Foo, function(value, key) {
  8910. * console.log(key);
  8911. * });
  8912. * // => logs 'a' and 'b' (iteration order is not guaranteed)
  8913. */
  8914. var forOwn = createForOwn(baseForOwn);
  8915. /**
  8916. * This method is like `_.forOwn` except that it iterates over properties of
  8917. * `object` in the opposite order.
  8918. *
  8919. * @static
  8920. * @memberOf _
  8921. * @category Object
  8922. * @param {Object} object The object to iterate over.
  8923. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8924. * @param {*} [thisArg] The `this` binding of `iteratee`.
  8925. * @returns {Object} Returns `object`.
  8926. * @example
  8927. *
  8928. * function Foo() {
  8929. * this.a = 1;
  8930. * this.b = 2;
  8931. * }
  8932. *
  8933. * Foo.prototype.c = 3;
  8934. *
  8935. * _.forOwnRight(new Foo, function(value, key) {
  8936. * console.log(key);
  8937. * });
  8938. * // => logs 'b' and 'a' assuming `_.forOwn` logs 'a' and 'b'
  8939. */
  8940. var forOwnRight = createForOwn(baseForOwnRight);
  8941. /**
  8942. * Creates an array of function property names from all enumerable properties,
  8943. * own and inherited, of `object`.
  8944. *
  8945. * @static
  8946. * @memberOf _
  8947. * @alias methods
  8948. * @category Object
  8949. * @param {Object} object The object to inspect.
  8950. * @returns {Array} Returns the new array of property names.
  8951. * @example
  8952. *
  8953. * _.functions(_);
  8954. * // => ['after', 'ary', 'assign', ...]
  8955. */
  8956. function functions(object) {
  8957. return baseFunctions(object, keysIn(object));
  8958. }
  8959. /**
  8960. * Gets the property value at `path` of `object`. If the resolved value is
  8961. * `undefined` the `defaultValue` is used in its place.
  8962. *
  8963. * @static
  8964. * @memberOf _
  8965. * @category Object
  8966. * @param {Object} object The object to query.
  8967. * @param {Array|string} path The path of the property to get.
  8968. * @param {*} [defaultValue] The value returned if the resolved value is `undefined`.
  8969. * @returns {*} Returns the resolved value.
  8970. * @example
  8971. *
  8972. * var object = { 'a': [{ 'b': { 'c': 3 } }] };
  8973. *
  8974. * _.get(object, 'a[0].b.c');
  8975. * // => 3
  8976. *
  8977. * _.get(object, ['a', '0', 'b', 'c']);
  8978. * // => 3
  8979. *
  8980. * _.get(object, 'a.b.c', 'default');
  8981. * // => 'default'
  8982. */
  8983. function get(object, path, defaultValue) {
  8984. var result = object == null ? undefined : baseGet(object, toPath(path), path + '');
  8985. return result === undefined ? defaultValue : result;
  8986. }
  8987. /**
  8988. * Checks if `path` is a direct property.
  8989. *
  8990. * @static
  8991. * @memberOf _
  8992. * @category Object
  8993. * @param {Object} object The object to query.
  8994. * @param {Array|string} path The path to check.
  8995. * @returns {boolean} Returns `true` if `path` is a direct property, else `false`.
  8996. * @example
  8997. *
  8998. * var object = { 'a': { 'b': { 'c': 3 } } };
  8999. *
  9000. * _.has(object, 'a');
  9001. * // => true
  9002. *
  9003. * _.has(object, 'a.b.c');
  9004. * // => true
  9005. *
  9006. * _.has(object, ['a', 'b', 'c']);
  9007. * // => true
  9008. */
  9009. function has(object, path) {
  9010. if (object == null) {
  9011. return false;
  9012. }
  9013. var result = hasOwnProperty.call(object, path);
  9014. if (!result && !isKey(path)) {
  9015. path = toPath(path);
  9016. object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  9017. if (object == null) {
  9018. return false;
  9019. }
  9020. path = last(path);
  9021. result = hasOwnProperty.call(object, path);
  9022. }
  9023. return result || (isLength(object.length) && isIndex(path, object.length) &&
  9024. (isArray(object) || isArguments(object)));
  9025. }
  9026. /**
  9027. * Creates an object composed of the inverted keys and values of `object`.
  9028. * If `object` contains duplicate values, subsequent values overwrite property
  9029. * assignments of previous values unless `multiValue` is `true`.
  9030. *
  9031. * @static
  9032. * @memberOf _
  9033. * @category Object
  9034. * @param {Object} object The object to invert.
  9035. * @param {boolean} [multiValue] Allow multiple values per key.
  9036. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  9037. * @returns {Object} Returns the new inverted object.
  9038. * @example
  9039. *
  9040. * var object = { 'a': 1, 'b': 2, 'c': 1 };
  9041. *
  9042. * _.invert(object);
  9043. * // => { '1': 'c', '2': 'b' }
  9044. *
  9045. * // with `multiValue`
  9046. * _.invert(object, true);
  9047. * // => { '1': ['a', 'c'], '2': ['b'] }
  9048. */
  9049. function invert(object, multiValue, guard) {
  9050. if (guard && isIterateeCall(object, multiValue, guard)) {
  9051. multiValue = undefined;
  9052. }
  9053. var index = -1,
  9054. props = keys(object),
  9055. length = props.length,
  9056. result = {};
  9057. while (++index < length) {
  9058. var key = props[index],
  9059. value = object[key];
  9060. if (multiValue) {
  9061. if (hasOwnProperty.call(result, value)) {
  9062. result[value].push(key);
  9063. } else {
  9064. result[value] = [key];
  9065. }
  9066. }
  9067. else {
  9068. result[value] = key;
  9069. }
  9070. }
  9071. return result;
  9072. }
  9073. /**
  9074. * Creates an array of the own enumerable property names of `object`.
  9075. *
  9076. * **Note:** Non-object values are coerced to objects. See the
  9077. * [ES spec](http://ecma-international.org/ecma-262/6.0/#sec-object.keys)
  9078. * for more details.
  9079. *
  9080. * @static
  9081. * @memberOf _
  9082. * @category Object
  9083. * @param {Object} object The object to query.
  9084. * @returns {Array} Returns the array of property names.
  9085. * @example
  9086. *
  9087. * function Foo() {
  9088. * this.a = 1;
  9089. * this.b = 2;
  9090. * }
  9091. *
  9092. * Foo.prototype.c = 3;
  9093. *
  9094. * _.keys(new Foo);
  9095. * // => ['a', 'b'] (iteration order is not guaranteed)
  9096. *
  9097. * _.keys('hi');
  9098. * // => ['0', '1']
  9099. */
  9100. var keys = !nativeKeys ? shimKeys : function(object) {
  9101. var Ctor = object == null ? undefined : object.constructor;
  9102. if ((typeof Ctor == 'function' && Ctor.prototype === object) ||
  9103. (typeof object != 'function' && isArrayLike(object))) {
  9104. return shimKeys(object);
  9105. }
  9106. return isObject(object) ? nativeKeys(object) : [];
  9107. };
  9108. /**
  9109. * Creates an array of the own and inherited enumerable property names of `object`.
  9110. *
  9111. * **Note:** Non-object values are coerced to objects.
  9112. *
  9113. * @static
  9114. * @memberOf _
  9115. * @category Object
  9116. * @param {Object} object The object to query.
  9117. * @returns {Array} Returns the array of property names.
  9118. * @example
  9119. *
  9120. * function Foo() {
  9121. * this.a = 1;
  9122. * this.b = 2;
  9123. * }
  9124. *
  9125. * Foo.prototype.c = 3;
  9126. *
  9127. * _.keysIn(new Foo);
  9128. * // => ['a', 'b', 'c'] (iteration order is not guaranteed)
  9129. */
  9130. function keysIn(object) {
  9131. if (object == null) {
  9132. return [];
  9133. }
  9134. if (!isObject(object)) {
  9135. object = Object(object);
  9136. }
  9137. var length = object.length;
  9138. length = (length && isLength(length) &&
  9139. (isArray(object) || isArguments(object)) && length) || 0;
  9140. var Ctor = object.constructor,
  9141. index = -1,
  9142. isProto = typeof Ctor == 'function' && Ctor.prototype === object,
  9143. result = Array(length),
  9144. skipIndexes = length > 0;
  9145. while (++index < length) {
  9146. result[index] = (index + '');
  9147. }
  9148. for (var key in object) {
  9149. if (!(skipIndexes && isIndex(key, length)) &&
  9150. !(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) {
  9151. result.push(key);
  9152. }
  9153. }
  9154. return result;
  9155. }
  9156. /**
  9157. * The opposite of `_.mapValues`; this method creates an object with the
  9158. * same values as `object` and keys generated by running each own enumerable
  9159. * property of `object` through `iteratee`.
  9160. *
  9161. * @static
  9162. * @memberOf _
  9163. * @category Object
  9164. * @param {Object} object The object to iterate over.
  9165. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  9166. * per iteration.
  9167. * @param {*} [thisArg] The `this` binding of `iteratee`.
  9168. * @returns {Object} Returns the new mapped object.
  9169. * @example
  9170. *
  9171. * _.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) {
  9172. * return key + value;
  9173. * });
  9174. * // => { 'a1': 1, 'b2': 2 }
  9175. */
  9176. var mapKeys = createObjectMapper(true);
  9177. /**
  9178. * Creates an object with the same keys as `object` and values generated by
  9179. * running each own enumerable property of `object` through `iteratee`. The
  9180. * iteratee function is bound to `thisArg` and invoked with three arguments:
  9181. * (value, key, object).
  9182. *
  9183. * If a property name is provided for `iteratee` the created `_.property`
  9184. * style callback returns the property value of the given element.
  9185. *
  9186. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  9187. * style callback returns `true` for elements that have a matching property
  9188. * value, else `false`.
  9189. *
  9190. * If an object is provided for `iteratee` the created `_.matches` style
  9191. * callback returns `true` for elements that have the properties of the given
  9192. * object, else `false`.
  9193. *
  9194. * @static
  9195. * @memberOf _
  9196. * @category Object
  9197. * @param {Object} object The object to iterate over.
  9198. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  9199. * per iteration.
  9200. * @param {*} [thisArg] The `this` binding of `iteratee`.
  9201. * @returns {Object} Returns the new mapped object.
  9202. * @example
  9203. *
  9204. * _.mapValues({ 'a': 1, 'b': 2 }, function(n) {
  9205. * return n * 3;
  9206. * });
  9207. * // => { 'a': 3, 'b': 6 }
  9208. *
  9209. * var users = {
  9210. * 'fred': { 'user': 'fred', 'age': 40 },
  9211. * 'pebbles': { 'user': 'pebbles', 'age': 1 }
  9212. * };
  9213. *
  9214. * // using the `_.property` callback shorthand
  9215. * _.mapValues(users, 'age');
  9216. * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
  9217. */
  9218. var mapValues = createObjectMapper();
  9219. /**
  9220. * The opposite of `_.pick`; this method creates an object composed of the
  9221. * own and inherited enumerable properties of `object` that are not omitted.
  9222. *
  9223. * @static
  9224. * @memberOf _
  9225. * @category Object
  9226. * @param {Object} object The source object.
  9227. * @param {Function|...(string|string[])} [predicate] The function invoked per
  9228. * iteration or property names to omit, specified as individual property
  9229. * names or arrays of property names.
  9230. * @param {*} [thisArg] The `this` binding of `predicate`.
  9231. * @returns {Object} Returns the new object.
  9232. * @example
  9233. *
  9234. * var object = { 'user': 'fred', 'age': 40 };
  9235. *
  9236. * _.omit(object, 'age');
  9237. * // => { 'user': 'fred' }
  9238. *
  9239. * _.omit(object, _.isNumber);
  9240. * // => { 'user': 'fred' }
  9241. */
  9242. var omit = restParam(function(object, props) {
  9243. if (object == null) {
  9244. return {};
  9245. }
  9246. if (typeof props[0] != 'function') {
  9247. var props = arrayMap(baseFlatten(props), String);
  9248. return pickByArray(object, baseDifference(keysIn(object), props));
  9249. }
  9250. var predicate = bindCallback(props[0], props[1], 3);
  9251. return pickByCallback(object, function(value, key, object) {
  9252. return !predicate(value, key, object);
  9253. });
  9254. });
  9255. /**
  9256. * Creates a two dimensional array of the key-value pairs for `object`,
  9257. * e.g. `[[key1, value1], [key2, value2]]`.
  9258. *
  9259. * @static
  9260. * @memberOf _
  9261. * @category Object
  9262. * @param {Object} object The object to query.
  9263. * @returns {Array} Returns the new array of key-value pairs.
  9264. * @example
  9265. *
  9266. * _.pairs({ 'barney': 36, 'fred': 40 });
  9267. * // => [['barney', 36], ['fred', 40]] (iteration order is not guaranteed)
  9268. */
  9269. function pairs(object) {
  9270. object = toObject(object);
  9271. var index = -1,
  9272. props = keys(object),
  9273. length = props.length,
  9274. result = Array(length);
  9275. while (++index < length) {
  9276. var key = props[index];
  9277. result[index] = [key, object[key]];
  9278. }
  9279. return result;
  9280. }
  9281. /**
  9282. * Creates an object composed of the picked `object` properties. Property
  9283. * names may be specified as individual arguments or as arrays of property
  9284. * names. If `predicate` is provided it is invoked for each property of `object`
  9285. * picking the properties `predicate` returns truthy for. The predicate is
  9286. * bound to `thisArg` and invoked with three arguments: (value, key, object).
  9287. *
  9288. * @static
  9289. * @memberOf _
  9290. * @category Object
  9291. * @param {Object} object The source object.
  9292. * @param {Function|...(string|string[])} [predicate] The function invoked per
  9293. * iteration or property names to pick, specified as individual property
  9294. * names or arrays of property names.
  9295. * @param {*} [thisArg] The `this` binding of `predicate`.
  9296. * @returns {Object} Returns the new object.
  9297. * @example
  9298. *
  9299. * var object = { 'user': 'fred', 'age': 40 };
  9300. *
  9301. * _.pick(object, 'user');
  9302. * // => { 'user': 'fred' }
  9303. *
  9304. * _.pick(object, _.isString);
  9305. * // => { 'user': 'fred' }
  9306. */
  9307. var pick = restParam(function(object, props) {
  9308. if (object == null) {
  9309. return {};
  9310. }
  9311. return typeof props[0] == 'function'
  9312. ? pickByCallback(object, bindCallback(props[0], props[1], 3))
  9313. : pickByArray(object, baseFlatten(props));
  9314. });
  9315. /**
  9316. * This method is like `_.get` except that if the resolved value is a function
  9317. * it is invoked with the `this` binding of its parent object and its result
  9318. * is returned.
  9319. *
  9320. * @static
  9321. * @memberOf _
  9322. * @category Object
  9323. * @param {Object} object The object to query.
  9324. * @param {Array|string} path The path of the property to resolve.
  9325. * @param {*} [defaultValue] The value returned if the resolved value is `undefined`.
  9326. * @returns {*} Returns the resolved value.
  9327. * @example
  9328. *
  9329. * var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
  9330. *
  9331. * _.result(object, 'a[0].b.c1');
  9332. * // => 3
  9333. *
  9334. * _.result(object, 'a[0].b.c2');
  9335. * // => 4
  9336. *
  9337. * _.result(object, 'a.b.c', 'default');
  9338. * // => 'default'
  9339. *
  9340. * _.result(object, 'a.b.c', _.constant('default'));
  9341. * // => 'default'
  9342. */
  9343. function result(object, path, defaultValue) {
  9344. var result = object == null ? undefined : object[path];
  9345. if (result === undefined) {
  9346. if (object != null && !isKey(path, object)) {
  9347. path = toPath(path);
  9348. object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  9349. result = object == null ? undefined : object[last(path)];
  9350. }
  9351. result = result === undefined ? defaultValue : result;
  9352. }
  9353. return isFunction(result) ? result.call(object) : result;
  9354. }
  9355. /**
  9356. * Sets the property value of `path` on `object`. If a portion of `path`
  9357. * does not exist it is created.
  9358. *
  9359. * @static
  9360. * @memberOf _
  9361. * @category Object
  9362. * @param {Object} object The object to augment.
  9363. * @param {Array|string} path The path of the property to set.
  9364. * @param {*} value The value to set.
  9365. * @returns {Object} Returns `object`.
  9366. * @example
  9367. *
  9368. * var object = { 'a': [{ 'b': { 'c': 3 } }] };
  9369. *
  9370. * _.set(object, 'a[0].b.c', 4);
  9371. * console.log(object.a[0].b.c);
  9372. * // => 4
  9373. *
  9374. * _.set(object, 'x[0].y.z', 5);
  9375. * console.log(object.x[0].y.z);
  9376. * // => 5
  9377. */
  9378. function set(object, path, value) {
  9379. if (object == null) {
  9380. return object;
  9381. }
  9382. var pathKey = (path + '');
  9383. path = (object[pathKey] != null || isKey(path, object)) ? [pathKey] : toPath(path);
  9384. var index = -1,
  9385. length = path.length,
  9386. lastIndex = length - 1,
  9387. nested = object;
  9388. while (nested != null && ++index < length) {
  9389. var key = path[index];
  9390. if (isObject(nested)) {
  9391. if (index == lastIndex) {
  9392. nested[key] = value;
  9393. } else if (nested[key] == null) {
  9394. nested[key] = isIndex(path[index + 1]) ? [] : {};
  9395. }
  9396. }
  9397. nested = nested[key];
  9398. }
  9399. return object;
  9400. }
  9401. /**
  9402. * An alternative to `_.reduce`; this method transforms `object` to a new
  9403. * `accumulator` object which is the result of running each of its own enumerable
  9404. * properties through `iteratee`, with each invocation potentially mutating
  9405. * the `accumulator` object. The `iteratee` is bound to `thisArg` and invoked
  9406. * with four arguments: (accumulator, value, key, object). Iteratee functions
  9407. * may exit iteration early by explicitly returning `false`.
  9408. *
  9409. * @static
  9410. * @memberOf _
  9411. * @category Object
  9412. * @param {Array|Object} object The object to iterate over.
  9413. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  9414. * @param {*} [accumulator] The custom accumulator value.
  9415. * @param {*} [thisArg] The `this` binding of `iteratee`.
  9416. * @returns {*} Returns the accumulated value.
  9417. * @example
  9418. *
  9419. * _.transform([2, 3, 4], function(result, n) {
  9420. * result.push(n *= n);
  9421. * return n % 2 == 0;
  9422. * });
  9423. * // => [4, 9]
  9424. *
  9425. * _.transform({ 'a': 1, 'b': 2 }, function(result, n, key) {
  9426. * result[key] = n * 3;
  9427. * });
  9428. * // => { 'a': 3, 'b': 6 }
  9429. */
  9430. function transform(object, iteratee, accumulator, thisArg) {
  9431. var isArr = isArray(object) || isTypedArray(object);
  9432. iteratee = getCallback(iteratee, thisArg, 4);
  9433. if (accumulator == null) {
  9434. if (isArr || isObject(object)) {
  9435. var Ctor = object.constructor;
  9436. if (isArr) {
  9437. accumulator = isArray(object) ? new Ctor : [];
  9438. } else {
  9439. accumulator = baseCreate(isFunction(Ctor) ? Ctor.prototype : undefined);
  9440. }
  9441. } else {
  9442. accumulator = {};
  9443. }
  9444. }
  9445. (isArr ? arrayEach : baseForOwn)(object, function(value, index, object) {
  9446. return iteratee(accumulator, value, index, object);
  9447. });
  9448. return accumulator;
  9449. }
  9450. /**
  9451. * Creates an array of the own enumerable property values of `object`.
  9452. *
  9453. * **Note:** Non-object values are coerced to objects.
  9454. *
  9455. * @static
  9456. * @memberOf _
  9457. * @category Object
  9458. * @param {Object} object The object to query.
  9459. * @returns {Array} Returns the array of property values.
  9460. * @example
  9461. *
  9462. * function Foo() {
  9463. * this.a = 1;
  9464. * this.b = 2;
  9465. * }
  9466. *
  9467. * Foo.prototype.c = 3;
  9468. *
  9469. * _.values(new Foo);
  9470. * // => [1, 2] (iteration order is not guaranteed)
  9471. *
  9472. * _.values('hi');
  9473. * // => ['h', 'i']
  9474. */
  9475. function values(object) {
  9476. return baseValues(object, keys(object));
  9477. }
  9478. /**
  9479. * Creates an array of the own and inherited enumerable property values
  9480. * of `object`.
  9481. *
  9482. * **Note:** Non-object values are coerced to objects.
  9483. *
  9484. * @static
  9485. * @memberOf _
  9486. * @category Object
  9487. * @param {Object} object The object to query.
  9488. * @returns {Array} Returns the array of property values.
  9489. * @example
  9490. *
  9491. * function Foo() {
  9492. * this.a = 1;
  9493. * this.b = 2;
  9494. * }
  9495. *
  9496. * Foo.prototype.c = 3;
  9497. *
  9498. * _.valuesIn(new Foo);
  9499. * // => [1, 2, 3] (iteration order is not guaranteed)
  9500. */
  9501. function valuesIn(object) {
  9502. return baseValues(object, keysIn(object));
  9503. }
  9504. /*------------------------------------------------------------------------*/
  9505. /**
  9506. * Checks if `n` is between `start` and up to but not including, `end`. If
  9507. * `end` is not specified it is set to `start` with `start` then set to `0`.
  9508. *
  9509. * @static
  9510. * @memberOf _
  9511. * @category Number
  9512. * @param {number} n The number to check.
  9513. * @param {number} [start=0] The start of the range.
  9514. * @param {number} end The end of the range.
  9515. * @returns {boolean} Returns `true` if `n` is in the range, else `false`.
  9516. * @example
  9517. *
  9518. * _.inRange(3, 2, 4);
  9519. * // => true
  9520. *
  9521. * _.inRange(4, 8);
  9522. * // => true
  9523. *
  9524. * _.inRange(4, 2);
  9525. * // => false
  9526. *
  9527. * _.inRange(2, 2);
  9528. * // => false
  9529. *
  9530. * _.inRange(1.2, 2);
  9531. * // => true
  9532. *
  9533. * _.inRange(5.2, 4);
  9534. * // => false
  9535. */
  9536. function inRange(value, start, end) {
  9537. start = +start || 0;
  9538. if (end === undefined) {
  9539. end = start;
  9540. start = 0;
  9541. } else {
  9542. end = +end || 0;
  9543. }
  9544. return value >= nativeMin(start, end) && value < nativeMax(start, end);
  9545. }
  9546. /**
  9547. * Produces a random number between `min` and `max` (inclusive). If only one
  9548. * argument is provided a number between `0` and the given number is returned.
  9549. * If `floating` is `true`, or either `min` or `max` are floats, a floating-point
  9550. * number is returned instead of an integer.
  9551. *
  9552. * @static
  9553. * @memberOf _
  9554. * @category Number
  9555. * @param {number} [min=0] The minimum possible value.
  9556. * @param {number} [max=1] The maximum possible value.
  9557. * @param {boolean} [floating] Specify returning a floating-point number.
  9558. * @returns {number} Returns the random number.
  9559. * @example
  9560. *
  9561. * _.random(0, 5);
  9562. * // => an integer between 0 and 5
  9563. *
  9564. * _.random(5);
  9565. * // => also an integer between 0 and 5
  9566. *
  9567. * _.random(5, true);
  9568. * // => a floating-point number between 0 and 5
  9569. *
  9570. * _.random(1.2, 5.2);
  9571. * // => a floating-point number between 1.2 and 5.2
  9572. */
  9573. function random(min, max, floating) {
  9574. if (floating && isIterateeCall(min, max, floating)) {
  9575. max = floating = undefined;
  9576. }
  9577. var noMin = min == null,
  9578. noMax = max == null;
  9579. if (floating == null) {
  9580. if (noMax && typeof min == 'boolean') {
  9581. floating = min;
  9582. min = 1;
  9583. }
  9584. else if (typeof max == 'boolean') {
  9585. floating = max;
  9586. noMax = true;
  9587. }
  9588. }
  9589. if (noMin && noMax) {
  9590. max = 1;
  9591. noMax = false;
  9592. }
  9593. min = +min || 0;
  9594. if (noMax) {
  9595. max = min;
  9596. min = 0;
  9597. } else {
  9598. max = +max || 0;
  9599. }
  9600. if (floating || min % 1 || max % 1) {
  9601. var rand = nativeRandom();
  9602. return nativeMin(min + (rand * (max - min + parseFloat('1e-' + ((rand + '').length - 1)))), max);
  9603. }
  9604. return baseRandom(min, max);
  9605. }
  9606. /*------------------------------------------------------------------------*/
  9607. /**
  9608. * Converts `string` to [camel case](https://en.wikipedia.org/wiki/CamelCase).
  9609. *
  9610. * @static
  9611. * @memberOf _
  9612. * @category String
  9613. * @param {string} [string=''] The string to convert.
  9614. * @returns {string} Returns the camel cased string.
  9615. * @example
  9616. *
  9617. * _.camelCase('Foo Bar');
  9618. * // => 'fooBar'
  9619. *
  9620. * _.camelCase('--foo-bar');
  9621. * // => 'fooBar'
  9622. *
  9623. * _.camelCase('__foo_bar__');
  9624. * // => 'fooBar'
  9625. */
  9626. var camelCase = createCompounder(function(result, word, index) {
  9627. word = word.toLowerCase();
  9628. return result + (index ? (word.charAt(0).toUpperCase() + word.slice(1)) : word);
  9629. });
  9630. /**
  9631. * Capitalizes the first character of `string`.
  9632. *
  9633. * @static
  9634. * @memberOf _
  9635. * @category String
  9636. * @param {string} [string=''] The string to capitalize.
  9637. * @returns {string} Returns the capitalized string.
  9638. * @example
  9639. *
  9640. * _.capitalize('fred');
  9641. * // => 'Fred'
  9642. */
  9643. function capitalize(string) {
  9644. string = baseToString(string);
  9645. return string && (string.charAt(0).toUpperCase() + string.slice(1));
  9646. }
  9647. /**
  9648. * Deburrs `string` by converting [latin-1 supplementary letters](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)#Character_table)
  9649. * to basic latin letters and removing [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks).
  9650. *
  9651. * @static
  9652. * @memberOf _
  9653. * @category String
  9654. * @param {string} [string=''] The string to deburr.
  9655. * @returns {string} Returns the deburred string.
  9656. * @example
  9657. *
  9658. * _.deburr('déjà vu');
  9659. * // => 'deja vu'
  9660. */
  9661. function deburr(string) {
  9662. string = baseToString(string);
  9663. return string && string.replace(reLatin1, deburrLetter).replace(reComboMark, '');
  9664. }
  9665. /**
  9666. * Checks if `string` ends with the given target string.
  9667. *
  9668. * @static
  9669. * @memberOf _
  9670. * @category String
  9671. * @param {string} [string=''] The string to search.
  9672. * @param {string} [target] The string to search for.
  9673. * @param {number} [position=string.length] The position to search from.
  9674. * @returns {boolean} Returns `true` if `string` ends with `target`, else `false`.
  9675. * @example
  9676. *
  9677. * _.endsWith('abc', 'c');
  9678. * // => true
  9679. *
  9680. * _.endsWith('abc', 'b');
  9681. * // => false
  9682. *
  9683. * _.endsWith('abc', 'b', 2);
  9684. * // => true
  9685. */
  9686. function endsWith(string, target, position) {
  9687. string = baseToString(string);
  9688. target = (target + '');
  9689. var length = string.length;
  9690. position = position === undefined
  9691. ? length
  9692. : nativeMin(position < 0 ? 0 : (+position || 0), length);
  9693. position -= target.length;
  9694. return position >= 0 && string.indexOf(target, position) == position;
  9695. }
  9696. /**
  9697. * Converts the characters "&", "<", ">", '"', "'", and "\`", in `string` to
  9698. * their corresponding HTML entities.
  9699. *
  9700. * **Note:** No other characters are escaped. To escape additional characters
  9701. * use a third-party library like [_he_](https://mths.be/he).
  9702. *
  9703. * Though the ">" character is escaped for symmetry, characters like
  9704. * ">" and "/" don't need escaping in HTML and have no special meaning
  9705. * unless they're part of a tag or unquoted attribute value.
  9706. * See [Mathias Bynens's article](https://mathiasbynens.be/notes/ambiguous-ampersands)
  9707. * (under "semi-related fun fact") for more details.
  9708. *
  9709. * Backticks are escaped because in Internet Explorer < 9, they can break out
  9710. * of attribute values or HTML comments. See [#59](https://html5sec.org/#59),
  9711. * [#102](https://html5sec.org/#102), [#108](https://html5sec.org/#108), and
  9712. * [#133](https://html5sec.org/#133) of the [HTML5 Security Cheatsheet](https://html5sec.org/)
  9713. * for more details.
  9714. *
  9715. * When working with HTML you should always [quote attribute values](http://wonko.com/post/html-escaping)
  9716. * to reduce XSS vectors.
  9717. *
  9718. * @static
  9719. * @memberOf _
  9720. * @category String
  9721. * @param {string} [string=''] The string to escape.
  9722. * @returns {string} Returns the escaped string.
  9723. * @example
  9724. *
  9725. * _.escape('fred, barney, & pebbles');
  9726. * // => 'fred, barney, &amp; pebbles'
  9727. */
  9728. function escape(string) {
  9729. // Reset `lastIndex` because in IE < 9 `String#replace` does not.
  9730. string = baseToString(string);
  9731. return (string && reHasUnescapedHtml.test(string))
  9732. ? string.replace(reUnescapedHtml, escapeHtmlChar)
  9733. : string;
  9734. }
  9735. /**
  9736. * Escapes the `RegExp` special characters "\", "/", "^", "$", ".", "|", "?",
  9737. * "*", "+", "(", ")", "[", "]", "{" and "}" in `string`.
  9738. *
  9739. * @static
  9740. * @memberOf _
  9741. * @category String
  9742. * @param {string} [string=''] The string to escape.
  9743. * @returns {string} Returns the escaped string.
  9744. * @example
  9745. *
  9746. * _.escapeRegExp('[lodash](https://lodash.com/)');
  9747. * // => '\[lodash\]\(https:\/\/lodash\.com\/\)'
  9748. */
  9749. function escapeRegExp(string) {
  9750. string = baseToString(string);
  9751. return (string && reHasRegExpChars.test(string))
  9752. ? string.replace(reRegExpChars, escapeRegExpChar)
  9753. : (string || '(?:)');
  9754. }
  9755. /**
  9756. * Converts `string` to [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles).
  9757. *
  9758. * @static
  9759. * @memberOf _
  9760. * @category String
  9761. * @param {string} [string=''] The string to convert.
  9762. * @returns {string} Returns the kebab cased string.
  9763. * @example
  9764. *
  9765. * _.kebabCase('Foo Bar');
  9766. * // => 'foo-bar'
  9767. *
  9768. * _.kebabCase('fooBar');
  9769. * // => 'foo-bar'
  9770. *
  9771. * _.kebabCase('__foo_bar__');
  9772. * // => 'foo-bar'
  9773. */
  9774. var kebabCase = createCompounder(function(result, word, index) {
  9775. return result + (index ? '-' : '') + word.toLowerCase();
  9776. });
  9777. /**
  9778. * Pads `string` on the left and right sides if it's shorter than `length`.
  9779. * Padding characters are truncated if they can't be evenly divided by `length`.
  9780. *
  9781. * @static
  9782. * @memberOf _
  9783. * @category String
  9784. * @param {string} [string=''] The string to pad.
  9785. * @param {number} [length=0] The padding length.
  9786. * @param {string} [chars=' '] The string used as padding.
  9787. * @returns {string} Returns the padded string.
  9788. * @example
  9789. *
  9790. * _.pad('abc', 8);
  9791. * // => ' abc '
  9792. *
  9793. * _.pad('abc', 8, '_-');
  9794. * // => '_-abc_-_'
  9795. *
  9796. * _.pad('abc', 3);
  9797. * // => 'abc'
  9798. */
  9799. function pad(string, length, chars) {
  9800. string = baseToString(string);
  9801. length = +length;
  9802. var strLength = string.length;
  9803. if (strLength >= length || !nativeIsFinite(length)) {
  9804. return string;
  9805. }
  9806. var mid = (length - strLength) / 2,
  9807. leftLength = nativeFloor(mid),
  9808. rightLength = nativeCeil(mid);
  9809. chars = createPadding('', rightLength, chars);
  9810. return chars.slice(0, leftLength) + string + chars;
  9811. }
  9812. /**
  9813. * Pads `string` on the left side if it's shorter than `length`. Padding
  9814. * characters are truncated if they exceed `length`.
  9815. *
  9816. * @static
  9817. * @memberOf _
  9818. * @category String
  9819. * @param {string} [string=''] The string to pad.
  9820. * @param {number} [length=0] The padding length.
  9821. * @param {string} [chars=' '] The string used as padding.
  9822. * @returns {string} Returns the padded string.
  9823. * @example
  9824. *
  9825. * _.padLeft('abc', 6);
  9826. * // => ' abc'
  9827. *
  9828. * _.padLeft('abc', 6, '_-');
  9829. * // => '_-_abc'
  9830. *
  9831. * _.padLeft('abc', 3);
  9832. * // => 'abc'
  9833. */
  9834. var padLeft = createPadDir();
  9835. /**
  9836. * Pads `string` on the right side if it's shorter than `length`. Padding
  9837. * characters are truncated if they exceed `length`.
  9838. *
  9839. * @static
  9840. * @memberOf _
  9841. * @category String
  9842. * @param {string} [string=''] The string to pad.
  9843. * @param {number} [length=0] The padding length.
  9844. * @param {string} [chars=' '] The string used as padding.
  9845. * @returns {string} Returns the padded string.
  9846. * @example
  9847. *
  9848. * _.padRight('abc', 6);
  9849. * // => 'abc '
  9850. *
  9851. * _.padRight('abc', 6, '_-');
  9852. * // => 'abc_-_'
  9853. *
  9854. * _.padRight('abc', 3);
  9855. * // => 'abc'
  9856. */
  9857. var padRight = createPadDir(true);
  9858. /**
  9859. * Converts `string` to an integer of the specified radix. If `radix` is
  9860. * `undefined` or `0`, a `radix` of `10` is used unless `value` is a hexadecimal,
  9861. * in which case a `radix` of `16` is used.
  9862. *
  9863. * **Note:** This method aligns with the [ES5 implementation](https://es5.github.io/#E)
  9864. * of `parseInt`.
  9865. *
  9866. * @static
  9867. * @memberOf _
  9868. * @category String
  9869. * @param {string} string The string to convert.
  9870. * @param {number} [radix] The radix to interpret `value` by.
  9871. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  9872. * @returns {number} Returns the converted integer.
  9873. * @example
  9874. *
  9875. * _.parseInt('08');
  9876. * // => 8
  9877. *
  9878. * _.map(['6', '08', '10'], _.parseInt);
  9879. * // => [6, 8, 10]
  9880. */
  9881. function parseInt(string, radix, guard) {
  9882. // Firefox < 21 and Opera < 15 follow ES3 for `parseInt`.
  9883. // Chrome fails to trim leading <BOM> whitespace characters.
  9884. // See https://code.google.com/p/v8/issues/detail?id=3109 for more details.
  9885. if (guard ? isIterateeCall(string, radix, guard) : radix == null) {
  9886. radix = 0;
  9887. } else if (radix) {
  9888. radix = +radix;
  9889. }
  9890. string = trim(string);
  9891. return nativeParseInt(string, radix || (reHasHexPrefix.test(string) ? 16 : 10));
  9892. }
  9893. /**
  9894. * Repeats the given string `n` times.
  9895. *
  9896. * @static
  9897. * @memberOf _
  9898. * @category String
  9899. * @param {string} [string=''] The string to repeat.
  9900. * @param {number} [n=0] The number of times to repeat the string.
  9901. * @returns {string} Returns the repeated string.
  9902. * @example
  9903. *
  9904. * _.repeat('*', 3);
  9905. * // => '***'
  9906. *
  9907. * _.repeat('abc', 2);
  9908. * // => 'abcabc'
  9909. *
  9910. * _.repeat('abc', 0);
  9911. * // => ''
  9912. */
  9913. function repeat(string, n) {
  9914. var result = '';
  9915. string = baseToString(string);
  9916. n = +n;
  9917. if (n < 1 || !string || !nativeIsFinite(n)) {
  9918. return result;
  9919. }
  9920. // Leverage the exponentiation by squaring algorithm for a faster repeat.
  9921. // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details.
  9922. do {
  9923. if (n % 2) {
  9924. result += string;
  9925. }
  9926. n = nativeFloor(n / 2);
  9927. string += string;
  9928. } while (n);
  9929. return result;
  9930. }
  9931. /**
  9932. * Converts `string` to [snake case](https://en.wikipedia.org/wiki/Snake_case).
  9933. *
  9934. * @static
  9935. * @memberOf _
  9936. * @category String
  9937. * @param {string} [string=''] The string to convert.
  9938. * @returns {string} Returns the snake cased string.
  9939. * @example
  9940. *
  9941. * _.snakeCase('Foo Bar');
  9942. * // => 'foo_bar'
  9943. *
  9944. * _.snakeCase('fooBar');
  9945. * // => 'foo_bar'
  9946. *
  9947. * _.snakeCase('--foo-bar');
  9948. * // => 'foo_bar'
  9949. */
  9950. var snakeCase = createCompounder(function(result, word, index) {
  9951. return result + (index ? '_' : '') + word.toLowerCase();
  9952. });
  9953. /**
  9954. * Converts `string` to [start case](https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage).
  9955. *
  9956. * @static
  9957. * @memberOf _
  9958. * @category String
  9959. * @param {string} [string=''] The string to convert.
  9960. * @returns {string} Returns the start cased string.
  9961. * @example
  9962. *
  9963. * _.startCase('--foo-bar');
  9964. * // => 'Foo Bar'
  9965. *
  9966. * _.startCase('fooBar');
  9967. * // => 'Foo Bar'
  9968. *
  9969. * _.startCase('__foo_bar__');
  9970. * // => 'Foo Bar'
  9971. */
  9972. var startCase = createCompounder(function(result, word, index) {
  9973. return result + (index ? ' ' : '') + (word.charAt(0).toUpperCase() + word.slice(1));
  9974. });
  9975. /**
  9976. * Checks if `string` starts with the given target string.
  9977. *
  9978. * @static
  9979. * @memberOf _
  9980. * @category String
  9981. * @param {string} [string=''] The string to search.
  9982. * @param {string} [target] The string to search for.
  9983. * @param {number} [position=0] The position to search from.
  9984. * @returns {boolean} Returns `true` if `string` starts with `target`, else `false`.
  9985. * @example
  9986. *
  9987. * _.startsWith('abc', 'a');
  9988. * // => true
  9989. *
  9990. * _.startsWith('abc', 'b');
  9991. * // => false
  9992. *
  9993. * _.startsWith('abc', 'b', 1);
  9994. * // => true
  9995. */
  9996. function startsWith(string, target, position) {
  9997. string = baseToString(string);
  9998. position = position == null
  9999. ? 0
  10000. : nativeMin(position < 0 ? 0 : (+position || 0), string.length);
  10001. return string.lastIndexOf(target, position) == position;
  10002. }
  10003. /**
  10004. * Creates a compiled template function that can interpolate data properties
  10005. * in "interpolate" delimiters, HTML-escape interpolated data properties in
  10006. * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data
  10007. * properties may be accessed as free variables in the template. If a setting
  10008. * object is provided it takes precedence over `_.templateSettings` values.
  10009. *
  10010. * **Note:** In the development build `_.template` utilizes
  10011. * [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl)
  10012. * for easier debugging.
  10013. *
  10014. * For more information on precompiling templates see
  10015. * [lodash's custom builds documentation](https://lodash.com/custom-builds).
  10016. *
  10017. * For more information on Chrome extension sandboxes see
  10018. * [Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval).
  10019. *
  10020. * @static
  10021. * @memberOf _
  10022. * @category String
  10023. * @param {string} [string=''] The template string.
  10024. * @param {Object} [options] The options object.
  10025. * @param {RegExp} [options.escape] The HTML "escape" delimiter.
  10026. * @param {RegExp} [options.evaluate] The "evaluate" delimiter.
  10027. * @param {Object} [options.imports] An object to import into the template as free variables.
  10028. * @param {RegExp} [options.interpolate] The "interpolate" delimiter.
  10029. * @param {string} [options.sourceURL] The sourceURL of the template's compiled source.
  10030. * @param {string} [options.variable] The data object variable name.
  10031. * @param- {Object} [otherOptions] Enables the legacy `options` param signature.
  10032. * @returns {Function} Returns the compiled template function.
  10033. * @example
  10034. *
  10035. * // using the "interpolate" delimiter to create a compiled template
  10036. * var compiled = _.template('hello <%= user %>!');
  10037. * compiled({ 'user': 'fred' });
  10038. * // => 'hello fred!'
  10039. *
  10040. * // using the HTML "escape" delimiter to escape data property values
  10041. * var compiled = _.template('<b><%- value %></b>');
  10042. * compiled({ 'value': '<script>' });
  10043. * // => '<b>&lt;script&gt;</b>'
  10044. *
  10045. * // using the "evaluate" delimiter to execute JavaScript and generate HTML
  10046. * var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
  10047. * compiled({ 'users': ['fred', 'barney'] });
  10048. * // => '<li>fred</li><li>barney</li>'
  10049. *
  10050. * // using the internal `print` function in "evaluate" delimiters
  10051. * var compiled = _.template('<% print("hello " + user); %>!');
  10052. * compiled({ 'user': 'barney' });
  10053. * // => 'hello barney!'
  10054. *
  10055. * // using the ES delimiter as an alternative to the default "interpolate" delimiter
  10056. * var compiled = _.template('hello ${ user }!');
  10057. * compiled({ 'user': 'pebbles' });
  10058. * // => 'hello pebbles!'
  10059. *
  10060. * // using custom template delimiters
  10061. * _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
  10062. * var compiled = _.template('hello {{ user }}!');
  10063. * compiled({ 'user': 'mustache' });
  10064. * // => 'hello mustache!'
  10065. *
  10066. * // using backslashes to treat delimiters as plain text
  10067. * var compiled = _.template('<%= "\\<%- value %\\>" %>');
  10068. * compiled({ 'value': 'ignored' });
  10069. * // => '<%- value %>'
  10070. *
  10071. * // using the `imports` option to import `jQuery` as `jq`
  10072. * var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
  10073. * var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
  10074. * compiled({ 'users': ['fred', 'barney'] });
  10075. * // => '<li>fred</li><li>barney</li>'
  10076. *
  10077. * // using the `sourceURL` option to specify a custom sourceURL for the template
  10078. * var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
  10079. * compiled(data);
  10080. * // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector
  10081. *
  10082. * // using the `variable` option to ensure a with-statement isn't used in the compiled template
  10083. * var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
  10084. * compiled.source;
  10085. * // => function(data) {
  10086. * // var __t, __p = '';
  10087. * // __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
  10088. * // return __p;
  10089. * // }
  10090. *
  10091. * // using the `source` property to inline compiled templates for meaningful
  10092. * // line numbers in error messages and a stack trace
  10093. * fs.writeFileSync(path.join(cwd, 'jst.js'), '\
  10094. * var JST = {\
  10095. * "main": ' + _.template(mainText).source + '\
  10096. * };\
  10097. * ');
  10098. */
  10099. function template(string, options, otherOptions) {
  10100. // Based on John Resig's `tmpl` implementation (http://ejohn.org/blog/javascript-micro-templating/)
  10101. // and Laura Doktorova's doT.js (https://github.com/olado/doT).
  10102. var settings = lodash.templateSettings;
  10103. if (otherOptions && isIterateeCall(string, options, otherOptions)) {
  10104. options = otherOptions = undefined;
  10105. }
  10106. string = baseToString(string);
  10107. options = assignWith(baseAssign({}, otherOptions || options), settings, assignOwnDefaults);
  10108. var imports = assignWith(baseAssign({}, options.imports), settings.imports, assignOwnDefaults),
  10109. importsKeys = keys(imports),
  10110. importsValues = baseValues(imports, importsKeys);
  10111. var isEscaping,
  10112. isEvaluating,
  10113. index = 0,
  10114. interpolate = options.interpolate || reNoMatch,
  10115. source = "__p += '";
  10116. // Compile the regexp to match each delimiter.
  10117. var reDelimiters = RegExp(
  10118. (options.escape || reNoMatch).source + '|' +
  10119. interpolate.source + '|' +
  10120. (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
  10121. (options.evaluate || reNoMatch).source + '|$'
  10122. , 'g');
  10123. // Use a sourceURL for easier debugging.
  10124. var sourceURL = '//# sourceURL=' +
  10125. ('sourceURL' in options
  10126. ? options.sourceURL
  10127. : ('lodash.templateSources[' + (++templateCounter) + ']')
  10128. ) + '\n';
  10129. string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
  10130. interpolateValue || (interpolateValue = esTemplateValue);
  10131. // Escape characters that can't be included in string literals.
  10132. source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar);
  10133. // Replace delimiters with snippets.
  10134. if (escapeValue) {
  10135. isEscaping = true;
  10136. source += "' +\n__e(" + escapeValue + ") +\n'";
  10137. }
  10138. if (evaluateValue) {
  10139. isEvaluating = true;
  10140. source += "';\n" + evaluateValue + ";\n__p += '";
  10141. }
  10142. if (interpolateValue) {
  10143. source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
  10144. }
  10145. index = offset + match.length;
  10146. // The JS engine embedded in Adobe products requires returning the `match`
  10147. // string in order to produce the correct `offset` value.
  10148. return match;
  10149. });
  10150. source += "';\n";
  10151. // If `variable` is not specified wrap a with-statement around the generated
  10152. // code to add the data object to the top of the scope chain.
  10153. var variable = options.variable;
  10154. if (!variable) {
  10155. source = 'with (obj) {\n' + source + '\n}\n';
  10156. }
  10157. // Cleanup code by stripping empty strings.
  10158. source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
  10159. .replace(reEmptyStringMiddle, '$1')
  10160. .replace(reEmptyStringTrailing, '$1;');
  10161. // Frame code as the function body.
  10162. source = 'function(' + (variable || 'obj') + ') {\n' +
  10163. (variable
  10164. ? ''
  10165. : 'obj || (obj = {});\n'
  10166. ) +
  10167. "var __t, __p = ''" +
  10168. (isEscaping
  10169. ? ', __e = _.escape'
  10170. : ''
  10171. ) +
  10172. (isEvaluating
  10173. ? ', __j = Array.prototype.join;\n' +
  10174. "function print() { __p += __j.call(arguments, '') }\n"
  10175. : ';\n'
  10176. ) +
  10177. source +
  10178. 'return __p\n}';
  10179. var result = attempt(function() {
  10180. return Function(importsKeys, sourceURL + 'return ' + source).apply(undefined, importsValues);
  10181. });
  10182. // Provide the compiled function's source by its `toString` method or
  10183. // the `source` property as a convenience for inlining compiled templates.
  10184. result.source = source;
  10185. if (isError(result)) {
  10186. throw result;
  10187. }
  10188. return result;
  10189. }
  10190. /**
  10191. * Removes leading and trailing whitespace or specified characters from `string`.
  10192. *
  10193. * @static
  10194. * @memberOf _
  10195. * @category String
  10196. * @param {string} [string=''] The string to trim.
  10197. * @param {string} [chars=whitespace] The characters to trim.
  10198. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10199. * @returns {string} Returns the trimmed string.
  10200. * @example
  10201. *
  10202. * _.trim(' abc ');
  10203. * // => 'abc'
  10204. *
  10205. * _.trim('-_-abc-_-', '_-');
  10206. * // => 'abc'
  10207. *
  10208. * _.map([' foo ', ' bar '], _.trim);
  10209. * // => ['foo', 'bar']
  10210. */
  10211. function trim(string, chars, guard) {
  10212. var value = string;
  10213. string = baseToString(string);
  10214. if (!string) {
  10215. return string;
  10216. }
  10217. if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
  10218. return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1);
  10219. }
  10220. chars = (chars + '');
  10221. return string.slice(charsLeftIndex(string, chars), charsRightIndex(string, chars) + 1);
  10222. }
  10223. /**
  10224. * Removes leading whitespace or specified characters from `string`.
  10225. *
  10226. * @static
  10227. * @memberOf _
  10228. * @category String
  10229. * @param {string} [string=''] The string to trim.
  10230. * @param {string} [chars=whitespace] The characters to trim.
  10231. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10232. * @returns {string} Returns the trimmed string.
  10233. * @example
  10234. *
  10235. * _.trimLeft(' abc ');
  10236. * // => 'abc '
  10237. *
  10238. * _.trimLeft('-_-abc-_-', '_-');
  10239. * // => 'abc-_-'
  10240. */
  10241. function trimLeft(string, chars, guard) {
  10242. var value = string;
  10243. string = baseToString(string);
  10244. if (!string) {
  10245. return string;
  10246. }
  10247. if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
  10248. return string.slice(trimmedLeftIndex(string));
  10249. }
  10250. return string.slice(charsLeftIndex(string, (chars + '')));
  10251. }
  10252. /**
  10253. * Removes trailing whitespace or specified characters from `string`.
  10254. *
  10255. * @static
  10256. * @memberOf _
  10257. * @category String
  10258. * @param {string} [string=''] The string to trim.
  10259. * @param {string} [chars=whitespace] The characters to trim.
  10260. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10261. * @returns {string} Returns the trimmed string.
  10262. * @example
  10263. *
  10264. * _.trimRight(' abc ');
  10265. * // => ' abc'
  10266. *
  10267. * _.trimRight('-_-abc-_-', '_-');
  10268. * // => '-_-abc'
  10269. */
  10270. function trimRight(string, chars, guard) {
  10271. var value = string;
  10272. string = baseToString(string);
  10273. if (!string) {
  10274. return string;
  10275. }
  10276. if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
  10277. return string.slice(0, trimmedRightIndex(string) + 1);
  10278. }
  10279. return string.slice(0, charsRightIndex(string, (chars + '')) + 1);
  10280. }
  10281. /**
  10282. * Truncates `string` if it's longer than the given maximum string length.
  10283. * The last characters of the truncated string are replaced with the omission
  10284. * string which defaults to "...".
  10285. *
  10286. * @static
  10287. * @memberOf _
  10288. * @category String
  10289. * @param {string} [string=''] The string to truncate.
  10290. * @param {Object|number} [options] The options object or maximum string length.
  10291. * @param {number} [options.length=30] The maximum string length.
  10292. * @param {string} [options.omission='...'] The string to indicate text is omitted.
  10293. * @param {RegExp|string} [options.separator] The separator pattern to truncate to.
  10294. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10295. * @returns {string} Returns the truncated string.
  10296. * @example
  10297. *
  10298. * _.trunc('hi-diddly-ho there, neighborino');
  10299. * // => 'hi-diddly-ho there, neighbo...'
  10300. *
  10301. * _.trunc('hi-diddly-ho there, neighborino', 24);
  10302. * // => 'hi-diddly-ho there, n...'
  10303. *
  10304. * _.trunc('hi-diddly-ho there, neighborino', {
  10305. * 'length': 24,
  10306. * 'separator': ' '
  10307. * });
  10308. * // => 'hi-diddly-ho there,...'
  10309. *
  10310. * _.trunc('hi-diddly-ho there, neighborino', {
  10311. * 'length': 24,
  10312. * 'separator': /,? +/
  10313. * });
  10314. * // => 'hi-diddly-ho there...'
  10315. *
  10316. * _.trunc('hi-diddly-ho there, neighborino', {
  10317. * 'omission': ' [...]'
  10318. * });
  10319. * // => 'hi-diddly-ho there, neig [...]'
  10320. */
  10321. function trunc(string, options, guard) {
  10322. if (guard && isIterateeCall(string, options, guard)) {
  10323. options = undefined;
  10324. }
  10325. var length = DEFAULT_TRUNC_LENGTH,
  10326. omission = DEFAULT_TRUNC_OMISSION;
  10327. if (options != null) {
  10328. if (isObject(options)) {
  10329. var separator = 'separator' in options ? options.separator : separator;
  10330. length = 'length' in options ? (+options.length || 0) : length;
  10331. omission = 'omission' in options ? baseToString(options.omission) : omission;
  10332. } else {
  10333. length = +options || 0;
  10334. }
  10335. }
  10336. string = baseToString(string);
  10337. if (length >= string.length) {
  10338. return string;
  10339. }
  10340. var end = length - omission.length;
  10341. if (end < 1) {
  10342. return omission;
  10343. }
  10344. var result = string.slice(0, end);
  10345. if (separator == null) {
  10346. return result + omission;
  10347. }
  10348. if (isRegExp(separator)) {
  10349. if (string.slice(end).search(separator)) {
  10350. var match,
  10351. newEnd,
  10352. substring = string.slice(0, end);
  10353. if (!separator.global) {
  10354. separator = RegExp(separator.source, (reFlags.exec(separator) || '') + 'g');
  10355. }
  10356. separator.lastIndex = 0;
  10357. while ((match = separator.exec(substring))) {
  10358. newEnd = match.index;
  10359. }
  10360. result = result.slice(0, newEnd == null ? end : newEnd);
  10361. }
  10362. } else if (string.indexOf(separator, end) != end) {
  10363. var index = result.lastIndexOf(separator);
  10364. if (index > -1) {
  10365. result = result.slice(0, index);
  10366. }
  10367. }
  10368. return result + omission;
  10369. }
  10370. /**
  10371. * The inverse of `_.escape`; this method converts the HTML entities
  10372. * `&amp;`, `&lt;`, `&gt;`, `&quot;`, `&#39;`, and `&#96;` in `string` to their
  10373. * corresponding characters.
  10374. *
  10375. * **Note:** No other HTML entities are unescaped. To unescape additional HTML
  10376. * entities use a third-party library like [_he_](https://mths.be/he).
  10377. *
  10378. * @static
  10379. * @memberOf _
  10380. * @category String
  10381. * @param {string} [string=''] The string to unescape.
  10382. * @returns {string} Returns the unescaped string.
  10383. * @example
  10384. *
  10385. * _.unescape('fred, barney, &amp; pebbles');
  10386. * // => 'fred, barney, & pebbles'
  10387. */
  10388. function unescape(string) {
  10389. string = baseToString(string);
  10390. return (string && reHasEscapedHtml.test(string))
  10391. ? string.replace(reEscapedHtml, unescapeHtmlChar)
  10392. : string;
  10393. }
  10394. /**
  10395. * Splits `string` into an array of its words.
  10396. *
  10397. * @static
  10398. * @memberOf _
  10399. * @category String
  10400. * @param {string} [string=''] The string to inspect.
  10401. * @param {RegExp|string} [pattern] The pattern to match words.
  10402. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10403. * @returns {Array} Returns the words of `string`.
  10404. * @example
  10405. *
  10406. * _.words('fred, barney, & pebbles');
  10407. * // => ['fred', 'barney', 'pebbles']
  10408. *
  10409. * _.words('fred, barney, & pebbles', /[^, ]+/g);
  10410. * // => ['fred', 'barney', '&', 'pebbles']
  10411. */
  10412. function words(string, pattern, guard) {
  10413. if (guard && isIterateeCall(string, pattern, guard)) {
  10414. pattern = undefined;
  10415. }
  10416. string = baseToString(string);
  10417. return string.match(pattern || reWords) || [];
  10418. }
  10419. /*------------------------------------------------------------------------*/
  10420. /**
  10421. * Attempts to invoke `func`, returning either the result or the caught error
  10422. * object. Any additional arguments are provided to `func` when it is invoked.
  10423. *
  10424. * @static
  10425. * @memberOf _
  10426. * @category Utility
  10427. * @param {Function} func The function to attempt.
  10428. * @returns {*} Returns the `func` result or error object.
  10429. * @example
  10430. *
  10431. * // avoid throwing errors for invalid selectors
  10432. * var elements = _.attempt(function(selector) {
  10433. * return document.querySelectorAll(selector);
  10434. * }, '>_>');
  10435. *
  10436. * if (_.isError(elements)) {
  10437. * elements = [];
  10438. * }
  10439. */
  10440. var attempt = restParam(function(func, args) {
  10441. try {
  10442. return func.apply(undefined, args);
  10443. } catch(e) {
  10444. return isError(e) ? e : new Error(e);
  10445. }
  10446. });
  10447. /**
  10448. * Creates a function that invokes `func` with the `this` binding of `thisArg`
  10449. * and arguments of the created function. If `func` is a property name the
  10450. * created callback returns the property value for a given element. If `func`
  10451. * is an object the created callback returns `true` for elements that contain
  10452. * the equivalent object properties, otherwise it returns `false`.
  10453. *
  10454. * @static
  10455. * @memberOf _
  10456. * @alias iteratee
  10457. * @category Utility
  10458. * @param {*} [func=_.identity] The value to convert to a callback.
  10459. * @param {*} [thisArg] The `this` binding of `func`.
  10460. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10461. * @returns {Function} Returns the callback.
  10462. * @example
  10463. *
  10464. * var users = [
  10465. * { 'user': 'barney', 'age': 36 },
  10466. * { 'user': 'fred', 'age': 40 }
  10467. * ];
  10468. *
  10469. * // wrap to create custom callback shorthands
  10470. * _.callback = _.wrap(_.callback, function(callback, func, thisArg) {
  10471. * var match = /^(.+?)__([gl]t)(.+)$/.exec(func);
  10472. * if (!match) {
  10473. * return callback(func, thisArg);
  10474. * }
  10475. * return function(object) {
  10476. * return match[2] == 'gt'
  10477. * ? object[match[1]] > match[3]
  10478. * : object[match[1]] < match[3];
  10479. * };
  10480. * });
  10481. *
  10482. * _.filter(users, 'age__gt36');
  10483. * // => [{ 'user': 'fred', 'age': 40 }]
  10484. */
  10485. function callback(func, thisArg, guard) {
  10486. if (guard && isIterateeCall(func, thisArg, guard)) {
  10487. thisArg = undefined;
  10488. }
  10489. return isObjectLike(func)
  10490. ? matches(func)
  10491. : baseCallback(func, thisArg);
  10492. }
  10493. /**
  10494. * Creates a function that returns `value`.
  10495. *
  10496. * @static
  10497. * @memberOf _
  10498. * @category Utility
  10499. * @param {*} value The value to return from the new function.
  10500. * @returns {Function} Returns the new function.
  10501. * @example
  10502. *
  10503. * var object = { 'user': 'fred' };
  10504. * var getter = _.constant(object);
  10505. *
  10506. * getter() === object;
  10507. * // => true
  10508. */
  10509. function constant(value) {
  10510. return function() {
  10511. return value;
  10512. };
  10513. }
  10514. /**
  10515. * This method returns the first argument provided to it.
  10516. *
  10517. * @static
  10518. * @memberOf _
  10519. * @category Utility
  10520. * @param {*} value Any value.
  10521. * @returns {*} Returns `value`.
  10522. * @example
  10523. *
  10524. * var object = { 'user': 'fred' };
  10525. *
  10526. * _.identity(object) === object;
  10527. * // => true
  10528. */
  10529. function identity(value) {
  10530. return value;
  10531. }
  10532. /**
  10533. * Creates a function that performs a deep comparison between a given object
  10534. * and `source`, returning `true` if the given object has equivalent property
  10535. * values, else `false`.
  10536. *
  10537. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  10538. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  10539. * their own, not inherited, enumerable properties. For comparing a single
  10540. * own or inherited property value see `_.matchesProperty`.
  10541. *
  10542. * @static
  10543. * @memberOf _
  10544. * @category Utility
  10545. * @param {Object} source The object of property values to match.
  10546. * @returns {Function} Returns the new function.
  10547. * @example
  10548. *
  10549. * var users = [
  10550. * { 'user': 'barney', 'age': 36, 'active': true },
  10551. * { 'user': 'fred', 'age': 40, 'active': false }
  10552. * ];
  10553. *
  10554. * _.filter(users, _.matches({ 'age': 40, 'active': false }));
  10555. * // => [{ 'user': 'fred', 'age': 40, 'active': false }]
  10556. */
  10557. function matches(source) {
  10558. return baseMatches(baseClone(source, true));
  10559. }
  10560. /**
  10561. * Creates a function that compares the property value of `path` on a given
  10562. * object to `value`.
  10563. *
  10564. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  10565. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  10566. * their own, not inherited, enumerable properties.
  10567. *
  10568. * @static
  10569. * @memberOf _
  10570. * @category Utility
  10571. * @param {Array|string} path The path of the property to get.
  10572. * @param {*} srcValue The value to match.
  10573. * @returns {Function} Returns the new function.
  10574. * @example
  10575. *
  10576. * var users = [
  10577. * { 'user': 'barney' },
  10578. * { 'user': 'fred' }
  10579. * ];
  10580. *
  10581. * _.find(users, _.matchesProperty('user', 'fred'));
  10582. * // => { 'user': 'fred' }
  10583. */
  10584. function matchesProperty(path, srcValue) {
  10585. return baseMatchesProperty(path, baseClone(srcValue, true));
  10586. }
  10587. /**
  10588. * Creates a function that invokes the method at `path` on a given object.
  10589. * Any additional arguments are provided to the invoked method.
  10590. *
  10591. * @static
  10592. * @memberOf _
  10593. * @category Utility
  10594. * @param {Array|string} path The path of the method to invoke.
  10595. * @param {...*} [args] The arguments to invoke the method with.
  10596. * @returns {Function} Returns the new function.
  10597. * @example
  10598. *
  10599. * var objects = [
  10600. * { 'a': { 'b': { 'c': _.constant(2) } } },
  10601. * { 'a': { 'b': { 'c': _.constant(1) } } }
  10602. * ];
  10603. *
  10604. * _.map(objects, _.method('a.b.c'));
  10605. * // => [2, 1]
  10606. *
  10607. * _.invoke(_.sortBy(objects, _.method(['a', 'b', 'c'])), 'a.b.c');
  10608. * // => [1, 2]
  10609. */
  10610. var method = restParam(function(path, args) {
  10611. return function(object) {
  10612. return invokePath(object, path, args);
  10613. };
  10614. });
  10615. /**
  10616. * The opposite of `_.method`; this method creates a function that invokes
  10617. * the method at a given path on `object`. Any additional arguments are
  10618. * provided to the invoked method.
  10619. *
  10620. * @static
  10621. * @memberOf _
  10622. * @category Utility
  10623. * @param {Object} object The object to query.
  10624. * @param {...*} [args] The arguments to invoke the method with.
  10625. * @returns {Function} Returns the new function.
  10626. * @example
  10627. *
  10628. * var array = _.times(3, _.constant),
  10629. * object = { 'a': array, 'b': array, 'c': array };
  10630. *
  10631. * _.map(['a[2]', 'c[0]'], _.methodOf(object));
  10632. * // => [2, 0]
  10633. *
  10634. * _.map([['a', '2'], ['c', '0']], _.methodOf(object));
  10635. * // => [2, 0]
  10636. */
  10637. var methodOf = restParam(function(object, args) {
  10638. return function(path) {
  10639. return invokePath(object, path, args);
  10640. };
  10641. });
  10642. /**
  10643. * Adds all own enumerable function properties of a source object to the
  10644. * destination object. If `object` is a function then methods are added to
  10645. * its prototype as well.
  10646. *
  10647. * **Note:** Use `_.runInContext` to create a pristine `lodash` function to
  10648. * avoid conflicts caused by modifying the original.
  10649. *
  10650. * @static
  10651. * @memberOf _
  10652. * @category Utility
  10653. * @param {Function|Object} [object=lodash] The destination object.
  10654. * @param {Object} source The object of functions to add.
  10655. * @param {Object} [options] The options object.
  10656. * @param {boolean} [options.chain=true] Specify whether the functions added
  10657. * are chainable.
  10658. * @returns {Function|Object} Returns `object`.
  10659. * @example
  10660. *
  10661. * function vowels(string) {
  10662. * return _.filter(string, function(v) {
  10663. * return /[aeiou]/i.test(v);
  10664. * });
  10665. * }
  10666. *
  10667. * _.mixin({ 'vowels': vowels });
  10668. * _.vowels('fred');
  10669. * // => ['e']
  10670. *
  10671. * _('fred').vowels().value();
  10672. * // => ['e']
  10673. *
  10674. * _.mixin({ 'vowels': vowels }, { 'chain': false });
  10675. * _('fred').vowels();
  10676. * // => ['e']
  10677. */
  10678. function mixin(object, source, options) {
  10679. if (options == null) {
  10680. var isObj = isObject(source),
  10681. props = isObj ? keys(source) : undefined,
  10682. methodNames = (props && props.length) ? baseFunctions(source, props) : undefined;
  10683. if (!(methodNames ? methodNames.length : isObj)) {
  10684. methodNames = false;
  10685. options = source;
  10686. source = object;
  10687. object = this;
  10688. }
  10689. }
  10690. if (!methodNames) {
  10691. methodNames = baseFunctions(source, keys(source));
  10692. }
  10693. var chain = true,
  10694. index = -1,
  10695. isFunc = isFunction(object),
  10696. length = methodNames.length;
  10697. if (options === false) {
  10698. chain = false;
  10699. } else if (isObject(options) && 'chain' in options) {
  10700. chain = options.chain;
  10701. }
  10702. while (++index < length) {
  10703. var methodName = methodNames[index],
  10704. func = source[methodName];
  10705. object[methodName] = func;
  10706. if (isFunc) {
  10707. object.prototype[methodName] = (function(func) {
  10708. return function() {
  10709. var chainAll = this.__chain__;
  10710. if (chain || chainAll) {
  10711. var result = object(this.__wrapped__),
  10712. actions = result.__actions__ = arrayCopy(this.__actions__);
  10713. actions.push({ 'func': func, 'args': arguments, 'thisArg': object });
  10714. result.__chain__ = chainAll;
  10715. return result;
  10716. }
  10717. return func.apply(object, arrayPush([this.value()], arguments));
  10718. };
  10719. }(func));
  10720. }
  10721. }
  10722. return object;
  10723. }
  10724. /**
  10725. * Reverts the `_` variable to its previous value and returns a reference to
  10726. * the `lodash` function.
  10727. *
  10728. * @static
  10729. * @memberOf _
  10730. * @category Utility
  10731. * @returns {Function} Returns the `lodash` function.
  10732. * @example
  10733. *
  10734. * var lodash = _.noConflict();
  10735. */
  10736. function noConflict() {
  10737. root._ = oldDash;
  10738. return this;
  10739. }
  10740. /**
  10741. * A no-operation function that returns `undefined` regardless of the
  10742. * arguments it receives.
  10743. *
  10744. * @static
  10745. * @memberOf _
  10746. * @category Utility
  10747. * @example
  10748. *
  10749. * var object = { 'user': 'fred' };
  10750. *
  10751. * _.noop(object) === undefined;
  10752. * // => true
  10753. */
  10754. function noop() {
  10755. // No operation performed.
  10756. }
  10757. /**
  10758. * Creates a function that returns the property value at `path` on a
  10759. * given object.
  10760. *
  10761. * @static
  10762. * @memberOf _
  10763. * @category Utility
  10764. * @param {Array|string} path The path of the property to get.
  10765. * @returns {Function} Returns the new function.
  10766. * @example
  10767. *
  10768. * var objects = [
  10769. * { 'a': { 'b': { 'c': 2 } } },
  10770. * { 'a': { 'b': { 'c': 1 } } }
  10771. * ];
  10772. *
  10773. * _.map(objects, _.property('a.b.c'));
  10774. * // => [2, 1]
  10775. *
  10776. * _.pluck(_.sortBy(objects, _.property(['a', 'b', 'c'])), 'a.b.c');
  10777. * // => [1, 2]
  10778. */
  10779. function property(path) {
  10780. return isKey(path) ? baseProperty(path) : basePropertyDeep(path);
  10781. }
  10782. /**
  10783. * The opposite of `_.property`; this method creates a function that returns
  10784. * the property value at a given path on `object`.
  10785. *
  10786. * @static
  10787. * @memberOf _
  10788. * @category Utility
  10789. * @param {Object} object The object to query.
  10790. * @returns {Function} Returns the new function.
  10791. * @example
  10792. *
  10793. * var array = [0, 1, 2],
  10794. * object = { 'a': array, 'b': array, 'c': array };
  10795. *
  10796. * _.map(['a[2]', 'c[0]'], _.propertyOf(object));
  10797. * // => [2, 0]
  10798. *
  10799. * _.map([['a', '2'], ['c', '0']], _.propertyOf(object));
  10800. * // => [2, 0]
  10801. */
  10802. function propertyOf(object) {
  10803. return function(path) {
  10804. return baseGet(object, toPath(path), path + '');
  10805. };
  10806. }
  10807. /**
  10808. * Creates an array of numbers (positive and/or negative) progressing from
  10809. * `start` up to, but not including, `end`. If `end` is not specified it is
  10810. * set to `start` with `start` then set to `0`. If `end` is less than `start`
  10811. * a zero-length range is created unless a negative `step` is specified.
  10812. *
  10813. * @static
  10814. * @memberOf _
  10815. * @category Utility
  10816. * @param {number} [start=0] The start of the range.
  10817. * @param {number} end The end of the range.
  10818. * @param {number} [step=1] The value to increment or decrement by.
  10819. * @returns {Array} Returns the new array of numbers.
  10820. * @example
  10821. *
  10822. * _.range(4);
  10823. * // => [0, 1, 2, 3]
  10824. *
  10825. * _.range(1, 5);
  10826. * // => [1, 2, 3, 4]
  10827. *
  10828. * _.range(0, 20, 5);
  10829. * // => [0, 5, 10, 15]
  10830. *
  10831. * _.range(0, -4, -1);
  10832. * // => [0, -1, -2, -3]
  10833. *
  10834. * _.range(1, 4, 0);
  10835. * // => [1, 1, 1]
  10836. *
  10837. * _.range(0);
  10838. * // => []
  10839. */
  10840. function range(start, end, step) {
  10841. if (step && isIterateeCall(start, end, step)) {
  10842. end = step = undefined;
  10843. }
  10844. start = +start || 0;
  10845. step = step == null ? 1 : (+step || 0);
  10846. if (end == null) {
  10847. end = start;
  10848. start = 0;
  10849. } else {
  10850. end = +end || 0;
  10851. }
  10852. // Use `Array(length)` so engines like Chakra and V8 avoid slower modes.
  10853. // See https://youtu.be/XAqIpGU8ZZk#t=17m25s for more details.
  10854. var index = -1,
  10855. length = nativeMax(nativeCeil((end - start) / (step || 1)), 0),
  10856. result = Array(length);
  10857. while (++index < length) {
  10858. result[index] = start;
  10859. start += step;
  10860. }
  10861. return result;
  10862. }
  10863. /**
  10864. * Invokes the iteratee function `n` times, returning an array of the results
  10865. * of each invocation. The `iteratee` is bound to `thisArg` and invoked with
  10866. * one argument; (index).
  10867. *
  10868. * @static
  10869. * @memberOf _
  10870. * @category Utility
  10871. * @param {number} n The number of times to invoke `iteratee`.
  10872. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  10873. * @param {*} [thisArg] The `this` binding of `iteratee`.
  10874. * @returns {Array} Returns the array of results.
  10875. * @example
  10876. *
  10877. * var diceRolls = _.times(3, _.partial(_.random, 1, 6, false));
  10878. * // => [3, 6, 4]
  10879. *
  10880. * _.times(3, function(n) {
  10881. * mage.castSpell(n);
  10882. * });
  10883. * // => invokes `mage.castSpell(n)` three times with `n` of `0`, `1`, and `2`
  10884. *
  10885. * _.times(3, function(n) {
  10886. * this.cast(n);
  10887. * }, mage);
  10888. * // => also invokes `mage.castSpell(n)` three times
  10889. */
  10890. function times(n, iteratee, thisArg) {
  10891. n = nativeFloor(n);
  10892. // Exit early to avoid a JSC JIT bug in Safari 8
  10893. // where `Array(0)` is treated as `Array(1)`.
  10894. if (n < 1 || !nativeIsFinite(n)) {
  10895. return [];
  10896. }
  10897. var index = -1,
  10898. result = Array(nativeMin(n, MAX_ARRAY_LENGTH));
  10899. iteratee = bindCallback(iteratee, thisArg, 1);
  10900. while (++index < n) {
  10901. if (index < MAX_ARRAY_LENGTH) {
  10902. result[index] = iteratee(index);
  10903. } else {
  10904. iteratee(index);
  10905. }
  10906. }
  10907. return result;
  10908. }
  10909. /**
  10910. * Generates a unique ID. If `prefix` is provided the ID is appended to it.
  10911. *
  10912. * @static
  10913. * @memberOf _
  10914. * @category Utility
  10915. * @param {string} [prefix] The value to prefix the ID with.
  10916. * @returns {string} Returns the unique ID.
  10917. * @example
  10918. *
  10919. * _.uniqueId('contact_');
  10920. * // => 'contact_104'
  10921. *
  10922. * _.uniqueId();
  10923. * // => '105'
  10924. */
  10925. function uniqueId(prefix) {
  10926. var id = ++idCounter;
  10927. return baseToString(prefix) + id;
  10928. }
  10929. /*------------------------------------------------------------------------*/
  10930. /**
  10931. * Adds two numbers.
  10932. *
  10933. * @static
  10934. * @memberOf _
  10935. * @category Math
  10936. * @param {number} augend The first number to add.
  10937. * @param {number} addend The second number to add.
  10938. * @returns {number} Returns the sum.
  10939. * @example
  10940. *
  10941. * _.add(6, 4);
  10942. * // => 10
  10943. */
  10944. function add(augend, addend) {
  10945. return (+augend || 0) + (+addend || 0);
  10946. }
  10947. /**
  10948. * Calculates `n` rounded up to `precision`.
  10949. *
  10950. * @static
  10951. * @memberOf _
  10952. * @category Math
  10953. * @param {number} n The number to round up.
  10954. * @param {number} [precision=0] The precision to round up to.
  10955. * @returns {number} Returns the rounded up number.
  10956. * @example
  10957. *
  10958. * _.ceil(4.006);
  10959. * // => 5
  10960. *
  10961. * _.ceil(6.004, 2);
  10962. * // => 6.01
  10963. *
  10964. * _.ceil(6040, -2);
  10965. * // => 6100
  10966. */
  10967. var ceil = createRound('ceil');
  10968. /**
  10969. * Calculates `n` rounded down to `precision`.
  10970. *
  10971. * @static
  10972. * @memberOf _
  10973. * @category Math
  10974. * @param {number} n The number to round down.
  10975. * @param {number} [precision=0] The precision to round down to.
  10976. * @returns {number} Returns the rounded down number.
  10977. * @example
  10978. *
  10979. * _.floor(4.006);
  10980. * // => 4
  10981. *
  10982. * _.floor(0.046, 2);
  10983. * // => 0.04
  10984. *
  10985. * _.floor(4060, -2);
  10986. * // => 4000
  10987. */
  10988. var floor = createRound('floor');
  10989. /**
  10990. * Gets the maximum value of `collection`. If `collection` is empty or falsey
  10991. * `-Infinity` is returned. If an iteratee function is provided it is invoked
  10992. * for each value in `collection` to generate the criterion by which the value
  10993. * is ranked. The `iteratee` is bound to `thisArg` and invoked with three
  10994. * arguments: (value, index, collection).
  10995. *
  10996. * If a property name is provided for `iteratee` the created `_.property`
  10997. * style callback returns the property value of the given element.
  10998. *
  10999. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  11000. * style callback returns `true` for elements that have a matching property
  11001. * value, else `false`.
  11002. *
  11003. * If an object is provided for `iteratee` the created `_.matches` style
  11004. * callback returns `true` for elements that have the properties of the given
  11005. * object, else `false`.
  11006. *
  11007. * @static
  11008. * @memberOf _
  11009. * @category Math
  11010. * @param {Array|Object|string} collection The collection to iterate over.
  11011. * @param {Function|Object|string} [iteratee] The function invoked per iteration.
  11012. * @param {*} [thisArg] The `this` binding of `iteratee`.
  11013. * @returns {*} Returns the maximum value.
  11014. * @example
  11015. *
  11016. * _.max([4, 2, 8, 6]);
  11017. * // => 8
  11018. *
  11019. * _.max([]);
  11020. * // => -Infinity
  11021. *
  11022. * var users = [
  11023. * { 'user': 'barney', 'age': 36 },
  11024. * { 'user': 'fred', 'age': 40 }
  11025. * ];
  11026. *
  11027. * _.max(users, function(chr) {
  11028. * return chr.age;
  11029. * });
  11030. * // => { 'user': 'fred', 'age': 40 }
  11031. *
  11032. * // using the `_.property` callback shorthand
  11033. * _.max(users, 'age');
  11034. * // => { 'user': 'fred', 'age': 40 }
  11035. */
  11036. var max = createExtremum(gt, NEGATIVE_INFINITY);
  11037. /**
  11038. * Gets the minimum value of `collection`. If `collection` is empty or falsey
  11039. * `Infinity` is returned. If an iteratee function is provided it is invoked
  11040. * for each value in `collection` to generate the criterion by which the value
  11041. * is ranked. The `iteratee` is bound to `thisArg` and invoked with three
  11042. * arguments: (value, index, collection).
  11043. *
  11044. * If a property name is provided for `iteratee` the created `_.property`
  11045. * style callback returns the property value of the given element.
  11046. *
  11047. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  11048. * style callback returns `true` for elements that have a matching property
  11049. * value, else `false`.
  11050. *
  11051. * If an object is provided for `iteratee` the created `_.matches` style
  11052. * callback returns `true` for elements that have the properties of the given
  11053. * object, else `false`.
  11054. *
  11055. * @static
  11056. * @memberOf _
  11057. * @category Math
  11058. * @param {Array|Object|string} collection The collection to iterate over.
  11059. * @param {Function|Object|string} [iteratee] The function invoked per iteration.
  11060. * @param {*} [thisArg] The `this` binding of `iteratee`.
  11061. * @returns {*} Returns the minimum value.
  11062. * @example
  11063. *
  11064. * _.min([4, 2, 8, 6]);
  11065. * // => 2
  11066. *
  11067. * _.min([]);
  11068. * // => Infinity
  11069. *
  11070. * var users = [
  11071. * { 'user': 'barney', 'age': 36 },
  11072. * { 'user': 'fred', 'age': 40 }
  11073. * ];
  11074. *
  11075. * _.min(users, function(chr) {
  11076. * return chr.age;
  11077. * });
  11078. * // => { 'user': 'barney', 'age': 36 }
  11079. *
  11080. * // using the `_.property` callback shorthand
  11081. * _.min(users, 'age');
  11082. * // => { 'user': 'barney', 'age': 36 }
  11083. */
  11084. var min = createExtremum(lt, POSITIVE_INFINITY);
  11085. /**
  11086. * Calculates `n` rounded to `precision`.
  11087. *
  11088. * @static
  11089. * @memberOf _
  11090. * @category Math
  11091. * @param {number} n The number to round.
  11092. * @param {number} [precision=0] The precision to round to.
  11093. * @returns {number} Returns the rounded number.
  11094. * @example
  11095. *
  11096. * _.round(4.006);
  11097. * // => 4
  11098. *
  11099. * _.round(4.006, 2);
  11100. * // => 4.01
  11101. *
  11102. * _.round(4060, -2);
  11103. * // => 4100
  11104. */
  11105. var round = createRound('round');
  11106. /**
  11107. * Gets the sum of the values in `collection`.
  11108. *
  11109. * @static
  11110. * @memberOf _
  11111. * @category Math
  11112. * @param {Array|Object|string} collection The collection to iterate over.
  11113. * @param {Function|Object|string} [iteratee] The function invoked per iteration.
  11114. * @param {*} [thisArg] The `this` binding of `iteratee`.
  11115. * @returns {number} Returns the sum.
  11116. * @example
  11117. *
  11118. * _.sum([4, 6]);
  11119. * // => 10
  11120. *
  11121. * _.sum({ 'a': 4, 'b': 6 });
  11122. * // => 10
  11123. *
  11124. * var objects = [
  11125. * { 'n': 4 },
  11126. * { 'n': 6 }
  11127. * ];
  11128. *
  11129. * _.sum(objects, function(object) {
  11130. * return object.n;
  11131. * });
  11132. * // => 10
  11133. *
  11134. * // using the `_.property` callback shorthand
  11135. * _.sum(objects, 'n');
  11136. * // => 10
  11137. */
  11138. function sum(collection, iteratee, thisArg) {
  11139. if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
  11140. iteratee = undefined;
  11141. }
  11142. iteratee = getCallback(iteratee, thisArg, 3);
  11143. return iteratee.length == 1
  11144. ? arraySum(isArray(collection) ? collection : toIterable(collection), iteratee)
  11145. : baseSum(collection, iteratee);
  11146. }
  11147. /*------------------------------------------------------------------------*/
  11148. // Ensure wrappers are instances of `baseLodash`.
  11149. lodash.prototype = baseLodash.prototype;
  11150. LodashWrapper.prototype = baseCreate(baseLodash.prototype);
  11151. LodashWrapper.prototype.constructor = LodashWrapper;
  11152. LazyWrapper.prototype = baseCreate(baseLodash.prototype);
  11153. LazyWrapper.prototype.constructor = LazyWrapper;
  11154. // Add functions to the `Map` cache.
  11155. MapCache.prototype['delete'] = mapDelete;
  11156. MapCache.prototype.get = mapGet;
  11157. MapCache.prototype.has = mapHas;
  11158. MapCache.prototype.set = mapSet;
  11159. // Add functions to the `Set` cache.
  11160. SetCache.prototype.push = cachePush;
  11161. // Assign cache to `_.memoize`.
  11162. memoize.Cache = MapCache;
  11163. // Add functions that return wrapped values when chaining.
  11164. lodash.after = after;
  11165. lodash.ary = ary;
  11166. lodash.assign = assign;
  11167. lodash.at = at;
  11168. lodash.before = before;
  11169. lodash.bind = bind;
  11170. lodash.bindAll = bindAll;
  11171. lodash.bindKey = bindKey;
  11172. lodash.callback = callback;
  11173. lodash.chain = chain;
  11174. lodash.chunk = chunk;
  11175. lodash.compact = compact;
  11176. lodash.constant = constant;
  11177. lodash.countBy = countBy;
  11178. lodash.create = create;
  11179. lodash.curry = curry;
  11180. lodash.curryRight = curryRight;
  11181. lodash.debounce = debounce;
  11182. lodash.defaults = defaults;
  11183. lodash.defaultsDeep = defaultsDeep;
  11184. lodash.defer = defer;
  11185. lodash.delay = delay;
  11186. lodash.difference = difference;
  11187. lodash.drop = drop;
  11188. lodash.dropRight = dropRight;
  11189. lodash.dropRightWhile = dropRightWhile;
  11190. lodash.dropWhile = dropWhile;
  11191. lodash.fill = fill;
  11192. lodash.filter = filter;
  11193. lodash.flatten = flatten;
  11194. lodash.flattenDeep = flattenDeep;
  11195. lodash.flow = flow;
  11196. lodash.flowRight = flowRight;
  11197. lodash.forEach = forEach;
  11198. lodash.forEachRight = forEachRight;
  11199. lodash.forIn = forIn;
  11200. lodash.forInRight = forInRight;
  11201. lodash.forOwn = forOwn;
  11202. lodash.forOwnRight = forOwnRight;
  11203. lodash.functions = functions;
  11204. lodash.groupBy = groupBy;
  11205. lodash.indexBy = indexBy;
  11206. lodash.initial = initial;
  11207. lodash.intersection = intersection;
  11208. lodash.invert = invert;
  11209. lodash.invoke = invoke;
  11210. lodash.keys = keys;
  11211. lodash.keysIn = keysIn;
  11212. lodash.map = map;
  11213. lodash.mapKeys = mapKeys;
  11214. lodash.mapValues = mapValues;
  11215. lodash.matches = matches;
  11216. lodash.matchesProperty = matchesProperty;
  11217. lodash.memoize = memoize;
  11218. lodash.merge = merge;
  11219. lodash.method = method;
  11220. lodash.methodOf = methodOf;
  11221. lodash.mixin = mixin;
  11222. lodash.modArgs = modArgs;
  11223. lodash.negate = negate;
  11224. lodash.omit = omit;
  11225. lodash.once = once;
  11226. lodash.pairs = pairs;
  11227. lodash.partial = partial;
  11228. lodash.partialRight = partialRight;
  11229. lodash.partition = partition;
  11230. lodash.pick = pick;
  11231. lodash.pluck = pluck;
  11232. lodash.property = property;
  11233. lodash.propertyOf = propertyOf;
  11234. lodash.pull = pull;
  11235. lodash.pullAt = pullAt;
  11236. lodash.range = range;
  11237. lodash.rearg = rearg;
  11238. lodash.reject = reject;
  11239. lodash.remove = remove;
  11240. lodash.rest = rest;
  11241. lodash.restParam = restParam;
  11242. lodash.set = set;
  11243. lodash.shuffle = shuffle;
  11244. lodash.slice = slice;
  11245. lodash.sortBy = sortBy;
  11246. lodash.sortByAll = sortByAll;
  11247. lodash.sortByOrder = sortByOrder;
  11248. lodash.spread = spread;
  11249. lodash.take = take;
  11250. lodash.takeRight = takeRight;
  11251. lodash.takeRightWhile = takeRightWhile;
  11252. lodash.takeWhile = takeWhile;
  11253. lodash.tap = tap;
  11254. lodash.throttle = throttle;
  11255. lodash.thru = thru;
  11256. lodash.times = times;
  11257. lodash.toArray = toArray;
  11258. lodash.toPlainObject = toPlainObject;
  11259. lodash.transform = transform;
  11260. lodash.union = union;
  11261. lodash.uniq = uniq;
  11262. lodash.unzip = unzip;
  11263. lodash.unzipWith = unzipWith;
  11264. lodash.values = values;
  11265. lodash.valuesIn = valuesIn;
  11266. lodash.where = where;
  11267. lodash.without = without;
  11268. lodash.wrap = wrap;
  11269. lodash.xor = xor;
  11270. lodash.zip = zip;
  11271. lodash.zipObject = zipObject;
  11272. lodash.zipWith = zipWith;
  11273. // Add aliases.
  11274. lodash.backflow = flowRight;
  11275. lodash.collect = map;
  11276. lodash.compose = flowRight;
  11277. lodash.each = forEach;
  11278. lodash.eachRight = forEachRight;
  11279. lodash.extend = assign;
  11280. lodash.iteratee = callback;
  11281. lodash.methods = functions;
  11282. lodash.object = zipObject;
  11283. lodash.select = filter;
  11284. lodash.tail = rest;
  11285. lodash.unique = uniq;
  11286. // Add functions to `lodash.prototype`.
  11287. mixin(lodash, lodash);
  11288. /*------------------------------------------------------------------------*/
  11289. // Add functions that return unwrapped values when chaining.
  11290. lodash.add = add;
  11291. lodash.attempt = attempt;
  11292. lodash.camelCase = camelCase;
  11293. lodash.capitalize = capitalize;
  11294. lodash.ceil = ceil;
  11295. lodash.clone = clone;
  11296. lodash.cloneDeep = cloneDeep;
  11297. lodash.deburr = deburr;
  11298. lodash.endsWith = endsWith;
  11299. lodash.escape = escape;
  11300. lodash.escapeRegExp = escapeRegExp;
  11301. lodash.every = every;
  11302. lodash.find = find;
  11303. lodash.findIndex = findIndex;
  11304. lodash.findKey = findKey;
  11305. lodash.findLast = findLast;
  11306. lodash.findLastIndex = findLastIndex;
  11307. lodash.findLastKey = findLastKey;
  11308. lodash.findWhere = findWhere;
  11309. lodash.first = first;
  11310. lodash.floor = floor;
  11311. lodash.get = get;
  11312. lodash.gt = gt;
  11313. lodash.gte = gte;
  11314. lodash.has = has;
  11315. lodash.identity = identity;
  11316. lodash.includes = includes;
  11317. lodash.indexOf = indexOf;
  11318. lodash.inRange = inRange;
  11319. lodash.isArguments = isArguments;
  11320. lodash.isArray = isArray;
  11321. lodash.isBoolean = isBoolean;
  11322. lodash.isDate = isDate;
  11323. lodash.isElement = isElement;
  11324. lodash.isEmpty = isEmpty;
  11325. lodash.isEqual = isEqual;
  11326. lodash.isError = isError;
  11327. lodash.isFinite = isFinite;
  11328. lodash.isFunction = isFunction;
  11329. lodash.isMatch = isMatch;
  11330. lodash.isNaN = isNaN;
  11331. lodash.isNative = isNative;
  11332. lodash.isNull = isNull;
  11333. lodash.isNumber = isNumber;
  11334. lodash.isObject = isObject;
  11335. lodash.isPlainObject = isPlainObject;
  11336. lodash.isRegExp = isRegExp;
  11337. lodash.isString = isString;
  11338. lodash.isTypedArray = isTypedArray;
  11339. lodash.isUndefined = isUndefined;
  11340. lodash.kebabCase = kebabCase;
  11341. lodash.last = last;
  11342. lodash.lastIndexOf = lastIndexOf;
  11343. lodash.lt = lt;
  11344. lodash.lte = lte;
  11345. lodash.max = max;
  11346. lodash.min = min;
  11347. lodash.noConflict = noConflict;
  11348. lodash.noop = noop;
  11349. lodash.now = now;
  11350. lodash.pad = pad;
  11351. lodash.padLeft = padLeft;
  11352. lodash.padRight = padRight;
  11353. lodash.parseInt = parseInt;
  11354. lodash.random = random;
  11355. lodash.reduce = reduce;
  11356. lodash.reduceRight = reduceRight;
  11357. lodash.repeat = repeat;
  11358. lodash.result = result;
  11359. lodash.round = round;
  11360. lodash.runInContext = runInContext;
  11361. lodash.size = size;
  11362. lodash.snakeCase = snakeCase;
  11363. lodash.some = some;
  11364. lodash.sortedIndex = sortedIndex;
  11365. lodash.sortedLastIndex = sortedLastIndex;
  11366. lodash.startCase = startCase;
  11367. lodash.startsWith = startsWith;
  11368. lodash.sum = sum;
  11369. lodash.template = template;
  11370. lodash.trim = trim;
  11371. lodash.trimLeft = trimLeft;
  11372. lodash.trimRight = trimRight;
  11373. lodash.trunc = trunc;
  11374. lodash.unescape = unescape;
  11375. lodash.uniqueId = uniqueId;
  11376. lodash.words = words;
  11377. // Add aliases.
  11378. lodash.all = every;
  11379. lodash.any = some;
  11380. lodash.contains = includes;
  11381. lodash.eq = isEqual;
  11382. lodash.detect = find;
  11383. lodash.foldl = reduce;
  11384. lodash.foldr = reduceRight;
  11385. lodash.head = first;
  11386. lodash.include = includes;
  11387. lodash.inject = reduce;
  11388. mixin(lodash, (function() {
  11389. var source = {};
  11390. baseForOwn(lodash, function(func, methodName) {
  11391. if (!lodash.prototype[methodName]) {
  11392. source[methodName] = func;
  11393. }
  11394. });
  11395. return source;
  11396. }()), false);
  11397. /*------------------------------------------------------------------------*/
  11398. // Add functions capable of returning wrapped and unwrapped values when chaining.
  11399. lodash.sample = sample;
  11400. lodash.prototype.sample = function(n) {
  11401. if (!this.__chain__ && n == null) {
  11402. return sample(this.value());
  11403. }
  11404. return this.thru(function(value) {
  11405. return sample(value, n);
  11406. });
  11407. };
  11408. /*------------------------------------------------------------------------*/
  11409. /**
  11410. * The semantic version number.
  11411. *
  11412. * @static
  11413. * @memberOf _
  11414. * @type string
  11415. */
  11416. lodash.VERSION = VERSION;
  11417. // Assign default placeholders.
  11418. arrayEach(['bind', 'bindKey', 'curry', 'curryRight', 'partial', 'partialRight'], function(methodName) {
  11419. lodash[methodName].placeholder = lodash;
  11420. });
  11421. // Add `LazyWrapper` methods for `_.drop` and `_.take` variants.
  11422. arrayEach(['drop', 'take'], function(methodName, index) {
  11423. LazyWrapper.prototype[methodName] = function(n) {
  11424. var filtered = this.__filtered__;
  11425. if (filtered && !index) {
  11426. return new LazyWrapper(this);
  11427. }
  11428. n = n == null ? 1 : nativeMax(nativeFloor(n) || 0, 0);
  11429. var result = this.clone();
  11430. if (filtered) {
  11431. result.__takeCount__ = nativeMin(result.__takeCount__, n);
  11432. } else {
  11433. result.__views__.push({ 'size': n, 'type': methodName + (result.__dir__ < 0 ? 'Right' : '') });
  11434. }
  11435. return result;
  11436. };
  11437. LazyWrapper.prototype[methodName + 'Right'] = function(n) {
  11438. return this.reverse()[methodName](n).reverse();
  11439. };
  11440. });
  11441. // Add `LazyWrapper` methods that accept an `iteratee` value.
  11442. arrayEach(['filter', 'map', 'takeWhile'], function(methodName, index) {
  11443. var type = index + 1,
  11444. isFilter = type != LAZY_MAP_FLAG;
  11445. LazyWrapper.prototype[methodName] = function(iteratee, thisArg) {
  11446. var result = this.clone();
  11447. result.__iteratees__.push({ 'iteratee': getCallback(iteratee, thisArg, 1), 'type': type });
  11448. result.__filtered__ = result.__filtered__ || isFilter;
  11449. return result;
  11450. };
  11451. });
  11452. // Add `LazyWrapper` methods for `_.first` and `_.last`.
  11453. arrayEach(['first', 'last'], function(methodName, index) {
  11454. var takeName = 'take' + (index ? 'Right' : '');
  11455. LazyWrapper.prototype[methodName] = function() {
  11456. return this[takeName](1).value()[0];
  11457. };
  11458. });
  11459. // Add `LazyWrapper` methods for `_.initial` and `_.rest`.
  11460. arrayEach(['initial', 'rest'], function(methodName, index) {
  11461. var dropName = 'drop' + (index ? '' : 'Right');
  11462. LazyWrapper.prototype[methodName] = function() {
  11463. return this.__filtered__ ? new LazyWrapper(this) : this[dropName](1);
  11464. };
  11465. });
  11466. // Add `LazyWrapper` methods for `_.pluck` and `_.where`.
  11467. arrayEach(['pluck', 'where'], function(methodName, index) {
  11468. var operationName = index ? 'filter' : 'map',
  11469. createCallback = index ? baseMatches : property;
  11470. LazyWrapper.prototype[methodName] = function(value) {
  11471. return this[operationName](createCallback(value));
  11472. };
  11473. });
  11474. LazyWrapper.prototype.compact = function() {
  11475. return this.filter(identity);
  11476. };
  11477. LazyWrapper.prototype.reject = function(predicate, thisArg) {
  11478. predicate = getCallback(predicate, thisArg, 1);
  11479. return this.filter(function(value) {
  11480. return !predicate(value);
  11481. });
  11482. };
  11483. LazyWrapper.prototype.slice = function(start, end) {
  11484. start = start == null ? 0 : (+start || 0);
  11485. var result = this;
  11486. if (result.__filtered__ && (start > 0 || end < 0)) {
  11487. return new LazyWrapper(result);
  11488. }
  11489. if (start < 0) {
  11490. result = result.takeRight(-start);
  11491. } else if (start) {
  11492. result = result.drop(start);
  11493. }
  11494. if (end !== undefined) {
  11495. end = (+end || 0);
  11496. result = end < 0 ? result.dropRight(-end) : result.take(end - start);
  11497. }
  11498. return result;
  11499. };
  11500. LazyWrapper.prototype.takeRightWhile = function(predicate, thisArg) {
  11501. return this.reverse().takeWhile(predicate, thisArg).reverse();
  11502. };
  11503. LazyWrapper.prototype.toArray = function() {
  11504. return this.take(POSITIVE_INFINITY);
  11505. };
  11506. // Add `LazyWrapper` methods to `lodash.prototype`.
  11507. baseForOwn(LazyWrapper.prototype, function(func, methodName) {
  11508. var checkIteratee = /^(?:filter|map|reject)|While$/.test(methodName),
  11509. retUnwrapped = /^(?:first|last)$/.test(methodName),
  11510. lodashFunc = lodash[retUnwrapped ? ('take' + (methodName == 'last' ? 'Right' : '')) : methodName];
  11511. if (!lodashFunc) {
  11512. return;
  11513. }
  11514. lodash.prototype[methodName] = function() {
  11515. var args = retUnwrapped ? [1] : arguments,
  11516. chainAll = this.__chain__,
  11517. value = this.__wrapped__,
  11518. isHybrid = !!this.__actions__.length,
  11519. isLazy = value instanceof LazyWrapper,
  11520. iteratee = args[0],
  11521. useLazy = isLazy || isArray(value);
  11522. if (useLazy && checkIteratee && typeof iteratee == 'function' && iteratee.length != 1) {
  11523. // Avoid lazy use if the iteratee has a "length" value other than `1`.
  11524. isLazy = useLazy = false;
  11525. }
  11526. var interceptor = function(value) {
  11527. return (retUnwrapped && chainAll)
  11528. ? lodashFunc(value, 1)[0]
  11529. : lodashFunc.apply(undefined, arrayPush([value], args));
  11530. };
  11531. var action = { 'func': thru, 'args': [interceptor], 'thisArg': undefined },
  11532. onlyLazy = isLazy && !isHybrid;
  11533. if (retUnwrapped && !chainAll) {
  11534. if (onlyLazy) {
  11535. value = value.clone();
  11536. value.__actions__.push(action);
  11537. return func.call(value);
  11538. }
  11539. return lodashFunc.call(undefined, this.value())[0];
  11540. }
  11541. if (!retUnwrapped && useLazy) {
  11542. value = onlyLazy ? value : new LazyWrapper(this);
  11543. var result = func.apply(value, args);
  11544. result.__actions__.push(action);
  11545. return new LodashWrapper(result, chainAll);
  11546. }
  11547. return this.thru(interceptor);
  11548. };
  11549. });
  11550. // Add `Array` and `String` methods to `lodash.prototype`.
  11551. arrayEach(['join', 'pop', 'push', 'replace', 'shift', 'sort', 'splice', 'split', 'unshift'], function(methodName) {
  11552. var func = (/^(?:replace|split)$/.test(methodName) ? stringProto : arrayProto)[methodName],
  11553. chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru',
  11554. retUnwrapped = /^(?:join|pop|replace|shift)$/.test(methodName);
  11555. lodash.prototype[methodName] = function() {
  11556. var args = arguments;
  11557. if (retUnwrapped && !this.__chain__) {
  11558. return func.apply(this.value(), args);
  11559. }
  11560. return this[chainName](function(value) {
  11561. return func.apply(value, args);
  11562. });
  11563. };
  11564. });
  11565. // Map minified function names to their real names.
  11566. baseForOwn(LazyWrapper.prototype, function(func, methodName) {
  11567. var lodashFunc = lodash[methodName];
  11568. if (lodashFunc) {
  11569. var key = lodashFunc.name,
  11570. names = realNames[key] || (realNames[key] = []);
  11571. names.push({ 'name': methodName, 'func': lodashFunc });
  11572. }
  11573. });
  11574. realNames[createHybridWrapper(undefined, BIND_KEY_FLAG).name] = [{ 'name': 'wrapper', 'func': undefined }];
  11575. // Add functions to the lazy wrapper.
  11576. LazyWrapper.prototype.clone = lazyClone;
  11577. LazyWrapper.prototype.reverse = lazyReverse;
  11578. LazyWrapper.prototype.value = lazyValue;
  11579. // Add chaining functions to the `lodash` wrapper.
  11580. lodash.prototype.chain = wrapperChain;
  11581. lodash.prototype.commit = wrapperCommit;
  11582. lodash.prototype.concat = wrapperConcat;
  11583. lodash.prototype.plant = wrapperPlant;
  11584. lodash.prototype.reverse = wrapperReverse;
  11585. lodash.prototype.toString = wrapperToString;
  11586. lodash.prototype.run = lodash.prototype.toJSON = lodash.prototype.valueOf = lodash.prototype.value = wrapperValue;
  11587. // Add function aliases to the `lodash` wrapper.
  11588. lodash.prototype.collect = lodash.prototype.map;
  11589. lodash.prototype.head = lodash.prototype.first;
  11590. lodash.prototype.select = lodash.prototype.filter;
  11591. lodash.prototype.tail = lodash.prototype.rest;
  11592. return lodash;
  11593. }
  11594. /*--------------------------------------------------------------------------*/
  11595. // Export lodash.
  11596. var _ = runInContext();
  11597. // Some AMD build optimizers like r.js check for condition patterns like the following:
  11598. if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  11599. // Expose lodash to the global object when an AMD loader is present to avoid
  11600. // errors in cases where lodash is loaded by a script tag and not intended
  11601. // as an AMD module. See http://requirejs.org/docs/errors.html#mismatch for
  11602. // more details.
  11603. root._ = _;
  11604. // Define as an anonymous module so, through path mapping, it can be
  11605. // referenced as the "underscore" module.
  11606. define(function() {
  11607. return _;
  11608. });
  11609. }
  11610. // Check for `exports` after `define` in case a build optimizer adds an `exports` object.
  11611. else if (freeExports && freeModule) {
  11612. // Export for Node.js or RingoJS.
  11613. if (moduleExports) {
  11614. (freeModule.exports = _)._ = _;
  11615. }
  11616. // Export for Rhino with CommonJS support.
  11617. else {
  11618. freeExports._ = _;
  11619. }
  11620. }
  11621. else {
  11622. // Export for a browser or Rhino.
  11623. root._ = _;
  11624. }
  11625. }.call(this));