Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

lodash.compat.js 238KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158
  1. /**
  2. * @license
  3. * Lo-Dash 2.4.2 (Custom Build) <https://lodash.com/>
  4. * Build: `lodash -o ./dist/lodash.compat.js`
  5. * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
  6. * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
  7. * Copyright 2009-2013 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 to pool arrays and objects used internally */
  14. var arrayPool = [],
  15. objectPool = [];
  16. /** Used to generate unique IDs */
  17. var idCounter = 0;
  18. /** Used internally to indicate various things */
  19. var indicatorObject = {};
  20. /** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */
  21. var keyPrefix = +new Date + '';
  22. /** Used as the size when optimizations are enabled for large arrays */
  23. var largeArraySize = 75;
  24. /** Used as the max size of the `arrayPool` and `objectPool` */
  25. var maxPoolSize = 40;
  26. /** Used to detect and test whitespace */
  27. var whitespace = (
  28. // whitespace
  29. ' \t\x0B\f\xA0\ufeff' +
  30. // line terminators
  31. '\n\r\u2028\u2029' +
  32. // unicode category "Zs" space separators
  33. '\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'
  34. );
  35. /** Used to match empty string literals in compiled template source */
  36. var reEmptyStringLeading = /\b__p \+= '';/g,
  37. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  38. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  39. /**
  40. * Used to match ES6 template delimiters
  41. * http://people.mozilla.org/~jorendorff/es6-draft.html#sec-literals-string-literals
  42. */
  43. var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
  44. /** Used to match regexp flags from their coerced string values */
  45. var reFlags = /\w*$/;
  46. /** Used to detected named functions */
  47. var reFuncName = /^\s*function[ \n\r\t]+\w/;
  48. /** Used to match "interpolate" template delimiters */
  49. var reInterpolate = /<%=([\s\S]+?)%>/g;
  50. /** Used to match leading whitespace and zeros to be removed */
  51. var reLeadingSpacesAndZeros = RegExp('^[' + whitespace + ']*0+(?=.$)');
  52. /** Used to ensure capturing order of template delimiters */
  53. var reNoMatch = /($^)/;
  54. /** Used to detect functions containing a `this` reference */
  55. var reThis = /\bthis\b/;
  56. /** Used to match unescaped characters in compiled string literals */
  57. var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
  58. /** Used to assign default `context` object properties */
  59. var contextProps = [
  60. 'Array', 'Boolean', 'Date', 'Error', 'Function', 'Math', 'Number', 'Object',
  61. 'RegExp', 'String', '_', 'attachEvent', 'clearTimeout', 'isFinite', 'isNaN',
  62. 'parseInt', 'setTimeout'
  63. ];
  64. /** Used to fix the JScript [[DontEnum]] bug */
  65. var shadowedProps = [
  66. 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable',
  67. 'toLocaleString', 'toString', 'valueOf'
  68. ];
  69. /** Used to make template sourceURLs easier to identify */
  70. var templateCounter = 0;
  71. /** `Object#toString` result shortcuts */
  72. var argsClass = '[object Arguments]',
  73. arrayClass = '[object Array]',
  74. boolClass = '[object Boolean]',
  75. dateClass = '[object Date]',
  76. errorClass = '[object Error]',
  77. funcClass = '[object Function]',
  78. numberClass = '[object Number]',
  79. objectClass = '[object Object]',
  80. regexpClass = '[object RegExp]',
  81. stringClass = '[object String]';
  82. /** Used to identify object classifications that `_.clone` supports */
  83. var cloneableClasses = {};
  84. cloneableClasses[funcClass] = false;
  85. cloneableClasses[argsClass] = cloneableClasses[arrayClass] =
  86. cloneableClasses[boolClass] = cloneableClasses[dateClass] =
  87. cloneableClasses[numberClass] = cloneableClasses[objectClass] =
  88. cloneableClasses[regexpClass] = cloneableClasses[stringClass] = true;
  89. /** Used as an internal `_.debounce` options object */
  90. var debounceOptions = {
  91. 'leading': false,
  92. 'maxWait': 0,
  93. 'trailing': false
  94. };
  95. /** Used as the property descriptor for `__bindData__` */
  96. var descriptor = {
  97. 'configurable': false,
  98. 'enumerable': false,
  99. 'value': null,
  100. 'writable': false
  101. };
  102. /** Used as the data object for `iteratorTemplate` */
  103. var iteratorData = {
  104. 'args': '',
  105. 'array': null,
  106. 'bottom': '',
  107. 'firstArg': '',
  108. 'init': '',
  109. 'keys': null,
  110. 'loop': '',
  111. 'shadowedProps': null,
  112. 'support': null,
  113. 'top': '',
  114. 'useHas': false
  115. };
  116. /** Used to determine if values are of the language type Object */
  117. var objectTypes = {
  118. 'boolean': false,
  119. 'function': true,
  120. 'object': true,
  121. 'number': false,
  122. 'string': false,
  123. 'undefined': false
  124. };
  125. /** Used to escape characters for inclusion in compiled string literals */
  126. var stringEscapes = {
  127. '\\': '\\',
  128. "'": "'",
  129. '\n': 'n',
  130. '\r': 'r',
  131. '\t': 't',
  132. '\u2028': 'u2028',
  133. '\u2029': 'u2029'
  134. };
  135. /** Used as a reference to the global object */
  136. var root = (objectTypes[typeof window] && window) || this;
  137. /** Detect free variable `exports` */
  138. var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
  139. /** Detect free variable `module` */
  140. var freeModule = objectTypes[typeof module] && module && !module.nodeType && module;
  141. /** Detect the popular CommonJS extension `module.exports` */
  142. var moduleExports = freeModule && freeModule.exports === freeExports && freeExports;
  143. /** Detect free variable `global` from Node.js or Browserified code and use it as `root` */
  144. var freeGlobal = objectTypes[typeof global] && global;
  145. if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
  146. root = freeGlobal;
  147. }
  148. /*--------------------------------------------------------------------------*/
  149. /**
  150. * The base implementation of `_.indexOf` without support for binary searches
  151. * or `fromIndex` constraints.
  152. *
  153. * @private
  154. * @param {Array} array The array to search.
  155. * @param {*} value The value to search for.
  156. * @param {number} [fromIndex=0] The index to search from.
  157. * @returns {number} Returns the index of the matched value or `-1`.
  158. */
  159. function baseIndexOf(array, value, fromIndex) {
  160. var index = (fromIndex || 0) - 1,
  161. length = array ? array.length : 0;
  162. while (++index < length) {
  163. if (array[index] === value) {
  164. return index;
  165. }
  166. }
  167. return -1;
  168. }
  169. /**
  170. * An implementation of `_.contains` for cache objects that mimics the return
  171. * signature of `_.indexOf` by returning `0` if the value is found, else `-1`.
  172. *
  173. * @private
  174. * @param {Object} cache The cache object to inspect.
  175. * @param {*} value The value to search for.
  176. * @returns {number} Returns `0` if `value` is found, else `-1`.
  177. */
  178. function cacheIndexOf(cache, value) {
  179. var type = typeof value;
  180. cache = cache.cache;
  181. if (type == 'boolean' || value == null) {
  182. return cache[value] ? 0 : -1;
  183. }
  184. if (type != 'number' && type != 'string') {
  185. type = 'object';
  186. }
  187. var key = type == 'number' ? value : keyPrefix + value;
  188. cache = (cache = cache[type]) && cache[key];
  189. return type == 'object'
  190. ? (cache && baseIndexOf(cache, value) > -1 ? 0 : -1)
  191. : (cache ? 0 : -1);
  192. }
  193. /**
  194. * Adds a given value to the corresponding cache object.
  195. *
  196. * @private
  197. * @param {*} value The value to add to the cache.
  198. */
  199. function cachePush(value) {
  200. var cache = this.cache,
  201. type = typeof value;
  202. if (type == 'boolean' || value == null) {
  203. cache[value] = true;
  204. } else {
  205. if (type != 'number' && type != 'string') {
  206. type = 'object';
  207. }
  208. var key = type == 'number' ? value : keyPrefix + value,
  209. typeCache = cache[type] || (cache[type] = {});
  210. if (type == 'object') {
  211. (typeCache[key] || (typeCache[key] = [])).push(value);
  212. } else {
  213. typeCache[key] = true;
  214. }
  215. }
  216. }
  217. /**
  218. * Used by `_.max` and `_.min` as the default callback when a given
  219. * collection is a string value.
  220. *
  221. * @private
  222. * @param {string} value The character to inspect.
  223. * @returns {number} Returns the code unit of given character.
  224. */
  225. function charAtCallback(value) {
  226. return value.charCodeAt(0);
  227. }
  228. /**
  229. * Used by `sortBy` to compare transformed `collection` elements, stable sorting
  230. * them in ascending order.
  231. *
  232. * @private
  233. * @param {Object} a The object to compare to `b`.
  234. * @param {Object} b The object to compare to `a`.
  235. * @returns {number} Returns the sort order indicator of `1` or `-1`.
  236. */
  237. function compareAscending(a, b) {
  238. var ac = a.criteria,
  239. bc = b.criteria,
  240. index = -1,
  241. length = ac.length;
  242. while (++index < length) {
  243. var value = ac[index],
  244. other = bc[index];
  245. if (value !== other) {
  246. if (value > other || typeof value == 'undefined') {
  247. return 1;
  248. }
  249. if (value < other || typeof other == 'undefined') {
  250. return -1;
  251. }
  252. }
  253. }
  254. // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
  255. // that causes it, under certain circumstances, to return the same value for
  256. // `a` and `b`. See https://github.com/jashkenas/underscore/pull/1247
  257. //
  258. // This also ensures a stable sort in V8 and other engines.
  259. // See http://code.google.com/p/v8/issues/detail?id=90
  260. return a.index - b.index;
  261. }
  262. /**
  263. * Creates a cache object to optimize linear searches of large arrays.
  264. *
  265. * @private
  266. * @param {Array} [array=[]] The array to search.
  267. * @returns {null|Object} Returns the cache object or `null` if caching should not be used.
  268. */
  269. function createCache(array) {
  270. var index = -1,
  271. length = array.length,
  272. first = array[0],
  273. mid = array[(length / 2) | 0],
  274. last = array[length - 1];
  275. if (first && typeof first == 'object' &&
  276. mid && typeof mid == 'object' && last && typeof last == 'object') {
  277. return false;
  278. }
  279. var cache = getObject();
  280. cache['false'] = cache['null'] = cache['true'] = cache['undefined'] = false;
  281. var result = getObject();
  282. result.array = array;
  283. result.cache = cache;
  284. result.push = cachePush;
  285. while (++index < length) {
  286. result.push(array[index]);
  287. }
  288. return result;
  289. }
  290. /**
  291. * Used by `template` to escape characters for inclusion in compiled
  292. * string literals.
  293. *
  294. * @private
  295. * @param {string} match The matched character to escape.
  296. * @returns {string} Returns the escaped character.
  297. */
  298. function escapeStringChar(match) {
  299. return '\\' + stringEscapes[match];
  300. }
  301. /**
  302. * Gets an array from the array pool or creates a new one if the pool is empty.
  303. *
  304. * @private
  305. * @returns {Array} The array from the pool.
  306. */
  307. function getArray() {
  308. return arrayPool.pop() || [];
  309. }
  310. /**
  311. * Gets an object from the object pool or creates a new one if the pool is empty.
  312. *
  313. * @private
  314. * @returns {Object} The object from the pool.
  315. */
  316. function getObject() {
  317. return objectPool.pop() || {
  318. 'array': null,
  319. 'cache': null,
  320. 'criteria': null,
  321. 'false': false,
  322. 'index': 0,
  323. 'null': false,
  324. 'number': null,
  325. 'object': null,
  326. 'push': null,
  327. 'string': null,
  328. 'true': false,
  329. 'undefined': false,
  330. 'value': null
  331. };
  332. }
  333. /**
  334. * Checks if `value` is a DOM node in IE < 9.
  335. *
  336. * @private
  337. * @param {*} value The value to check.
  338. * @returns {boolean} Returns `true` if the `value` is a DOM node, else `false`.
  339. */
  340. function isNode(value) {
  341. // IE < 9 presents DOM nodes as `Object` objects except they have `toString`
  342. // methods that are `typeof` "string" and still can coerce nodes to strings
  343. return typeof value.toString != 'function' && typeof (value + '') == 'string';
  344. }
  345. /**
  346. * Releases the given array back to the array pool.
  347. *
  348. * @private
  349. * @param {Array} [array] The array to release.
  350. */
  351. function releaseArray(array) {
  352. array.length = 0;
  353. if (arrayPool.length < maxPoolSize) {
  354. arrayPool.push(array);
  355. }
  356. }
  357. /**
  358. * Releases the given object back to the object pool.
  359. *
  360. * @private
  361. * @param {Object} [object] The object to release.
  362. */
  363. function releaseObject(object) {
  364. var cache = object.cache;
  365. if (cache) {
  366. releaseObject(cache);
  367. }
  368. object.array = object.cache = object.criteria = object.object = object.number = object.string = object.value = null;
  369. if (objectPool.length < maxPoolSize) {
  370. objectPool.push(object);
  371. }
  372. }
  373. /**
  374. * Slices the `collection` from the `start` index up to, but not including,
  375. * the `end` index.
  376. *
  377. * Note: This function is used instead of `Array#slice` to support node lists
  378. * in IE < 9 and to ensure dense arrays are returned.
  379. *
  380. * @private
  381. * @param {Array|Object|string} collection The collection to slice.
  382. * @param {number} start The start index.
  383. * @param {number} end The end index.
  384. * @returns {Array} Returns the new array.
  385. */
  386. function slice(array, start, end) {
  387. start || (start = 0);
  388. if (typeof end == 'undefined') {
  389. end = array ? array.length : 0;
  390. }
  391. var index = -1,
  392. length = end - start || 0,
  393. result = Array(length < 0 ? 0 : length);
  394. while (++index < length) {
  395. result[index] = array[start + index];
  396. }
  397. return result;
  398. }
  399. /*--------------------------------------------------------------------------*/
  400. /**
  401. * Create a new `lodash` function using the given context object.
  402. *
  403. * @static
  404. * @memberOf _
  405. * @category Utilities
  406. * @param {Object} [context=root] The context object.
  407. * @returns {Function} Returns the `lodash` function.
  408. */
  409. function runInContext(context) {
  410. // Avoid issues with some ES3 environments that attempt to use values, named
  411. // after built-in constructors like `Object`, for the creation of literals.
  412. // ES5 clears this up by stating that literals must use built-in constructors.
  413. // See http://es5.github.io/#x11.1.5.
  414. context = context ? _.defaults(root.Object(), context, _.pick(root, contextProps)) : root;
  415. /** Native constructor references */
  416. var Array = context.Array,
  417. Boolean = context.Boolean,
  418. Date = context.Date,
  419. Error = context.Error,
  420. Function = context.Function,
  421. Math = context.Math,
  422. Number = context.Number,
  423. Object = context.Object,
  424. RegExp = context.RegExp,
  425. String = context.String,
  426. TypeError = context.TypeError;
  427. /**
  428. * Used for `Array` method references.
  429. *
  430. * Normally `Array.prototype` would suffice, however, using an array literal
  431. * avoids issues in Narwhal.
  432. */
  433. var arrayRef = [];
  434. /** Used for native method references */
  435. var errorProto = Error.prototype,
  436. objectProto = Object.prototype,
  437. stringProto = String.prototype;
  438. /** Used to restore the original `_` reference in `noConflict` */
  439. var oldDash = context._;
  440. /** Used to resolve the internal [[Class]] of values */
  441. var toString = objectProto.toString;
  442. /** Used to detect if a method is native */
  443. var reNative = RegExp('^' +
  444. String(toString)
  445. .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  446. .replace(/toString| for [^\]]+/g, '.*?') + '$'
  447. );
  448. /** Native method shortcuts */
  449. var ceil = Math.ceil,
  450. clearTimeout = context.clearTimeout,
  451. floor = Math.floor,
  452. fnToString = Function.prototype.toString,
  453. getPrototypeOf = isNative(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf,
  454. hasOwnProperty = objectProto.hasOwnProperty,
  455. push = arrayRef.push,
  456. propertyIsEnumerable = objectProto.propertyIsEnumerable,
  457. setTimeout = context.setTimeout,
  458. splice = arrayRef.splice,
  459. unshift = arrayRef.unshift;
  460. /** Used to set meta data on functions */
  461. var defineProperty = (function() {
  462. // IE 8 only accepts DOM elements
  463. try {
  464. var o = {},
  465. func = isNative(func = Object.defineProperty) && func,
  466. result = func(o, o, o) && func;
  467. } catch(e) { }
  468. return result;
  469. }());
  470. /* Native method shortcuts for methods with the same name as other `lodash` methods */
  471. var nativeCreate = isNative(nativeCreate = Object.create) && nativeCreate,
  472. nativeIsArray = isNative(nativeIsArray = Array.isArray) && nativeIsArray,
  473. nativeIsFinite = context.isFinite,
  474. nativeIsNaN = context.isNaN,
  475. nativeKeys = isNative(nativeKeys = Object.keys) && nativeKeys,
  476. nativeMax = Math.max,
  477. nativeMin = Math.min,
  478. nativeParseInt = context.parseInt,
  479. nativeRandom = Math.random;
  480. /** Used to lookup a built-in constructor by [[Class]] */
  481. var ctorByClass = {};
  482. ctorByClass[arrayClass] = Array;
  483. ctorByClass[boolClass] = Boolean;
  484. ctorByClass[dateClass] = Date;
  485. ctorByClass[funcClass] = Function;
  486. ctorByClass[objectClass] = Object;
  487. ctorByClass[numberClass] = Number;
  488. ctorByClass[regexpClass] = RegExp;
  489. ctorByClass[stringClass] = String;
  490. /** Used to avoid iterating non-enumerable properties in IE < 9 */
  491. var nonEnumProps = {};
  492. nonEnumProps[arrayClass] = nonEnumProps[dateClass] = nonEnumProps[numberClass] = { 'constructor': true, 'toLocaleString': true, 'toString': true, 'valueOf': true };
  493. nonEnumProps[boolClass] = nonEnumProps[stringClass] = { 'constructor': true, 'toString': true, 'valueOf': true };
  494. nonEnumProps[errorClass] = nonEnumProps[funcClass] = nonEnumProps[regexpClass] = { 'constructor': true, 'toString': true };
  495. nonEnumProps[objectClass] = { 'constructor': true };
  496. (function() {
  497. var length = shadowedProps.length;
  498. while (length--) {
  499. var key = shadowedProps[length];
  500. for (var className in nonEnumProps) {
  501. if (hasOwnProperty.call(nonEnumProps, className) && !hasOwnProperty.call(nonEnumProps[className], key)) {
  502. nonEnumProps[className][key] = false;
  503. }
  504. }
  505. }
  506. }());
  507. /*--------------------------------------------------------------------------*/
  508. /**
  509. * Creates a `lodash` object which wraps the given value to enable intuitive
  510. * method chaining.
  511. *
  512. * In addition to Lo-Dash methods, wrappers also have the following `Array` methods:
  513. * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
  514. * and `unshift`
  515. *
  516. * Chaining is supported in custom builds as long as the `value` method is
  517. * implicitly or explicitly included in the build.
  518. *
  519. * The chainable wrapper functions are:
  520. * `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`,
  521. * `compose`, `concat`, `countBy`, `create`, `createCallback`, `curry`,
  522. * `debounce`, `defaults`, `defer`, `delay`, `difference`, `filter`, `flatten`,
  523. * `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`,
  524. * `functions`, `groupBy`, `indexBy`, `initial`, `intersection`, `invert`,
  525. * `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
  526. * `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `pull`, `push`,
  527. * `range`, `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`,
  528. * `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`, `transform`,
  529. * `union`, `uniq`, `unshift`, `unzip`, `values`, `where`, `without`, `wrap`,
  530. * and `zip`
  531. *
  532. * The non-chainable wrapper functions are:
  533. * `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `findIndex`,
  534. * `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `has`, `identity`,
  535. * `indexOf`, `isArguments`, `isArray`, `isBoolean`, `isDate`, `isElement`,
  536. * `isEmpty`, `isEqual`, `isFinite`, `isFunction`, `isNaN`, `isNull`, `isNumber`,
  537. * `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`, `join`,
  538. * `lastIndexOf`, `mixin`, `noConflict`, `parseInt`, `pop`, `random`, `reduce`,
  539. * `reduceRight`, `result`, `shift`, `size`, `some`, `sortedIndex`, `runInContext`,
  540. * `template`, `unescape`, `uniqueId`, and `value`
  541. *
  542. * The wrapper functions `first` and `last` return wrapped values when `n` is
  543. * provided, otherwise they return unwrapped values.
  544. *
  545. * Explicit chaining can be enabled by using the `_.chain` method.
  546. *
  547. * @name _
  548. * @constructor
  549. * @category Chaining
  550. * @param {*} value The value to wrap in a `lodash` instance.
  551. * @returns {Object} Returns a `lodash` instance.
  552. * @example
  553. *
  554. * var wrapped = _([1, 2, 3]);
  555. *
  556. * // returns an unwrapped value
  557. * wrapped.reduce(function(sum, num) {
  558. * return sum + num;
  559. * });
  560. * // => 6
  561. *
  562. * // returns a wrapped value
  563. * var squares = wrapped.map(function(num) {
  564. * return num * num;
  565. * });
  566. *
  567. * _.isArray(squares);
  568. * // => false
  569. *
  570. * _.isArray(squares.value());
  571. * // => true
  572. */
  573. function lodash(value) {
  574. // don't wrap if already wrapped, even if wrapped by a different `lodash` constructor
  575. return (value && typeof value == 'object' && !isArray(value) && hasOwnProperty.call(value, '__wrapped__'))
  576. ? value
  577. : new lodashWrapper(value);
  578. }
  579. /**
  580. * A fast path for creating `lodash` wrapper objects.
  581. *
  582. * @private
  583. * @param {*} value The value to wrap in a `lodash` instance.
  584. * @param {boolean} chainAll A flag to enable chaining for all methods
  585. * @returns {Object} Returns a `lodash` instance.
  586. */
  587. function lodashWrapper(value, chainAll) {
  588. this.__chain__ = !!chainAll;
  589. this.__wrapped__ = value;
  590. }
  591. // ensure `new lodashWrapper` is an instance of `lodash`
  592. lodashWrapper.prototype = lodash.prototype;
  593. /**
  594. * An object used to flag environments features.
  595. *
  596. * @static
  597. * @memberOf _
  598. * @type Object
  599. */
  600. var support = lodash.support = {};
  601. (function() {
  602. var ctor = function() { this.x = 1; },
  603. object = { '0': 1, 'length': 1 },
  604. props = [];
  605. ctor.prototype = { 'valueOf': 1, 'y': 1 };
  606. for (var key in new ctor) { props.push(key); }
  607. for (key in arguments) { }
  608. /**
  609. * Detect if an `arguments` object's [[Class]] is resolvable (all but Firefox < 4, IE < 9).
  610. *
  611. * @memberOf _.support
  612. * @type boolean
  613. */
  614. support.argsClass = toString.call(arguments) == argsClass;
  615. /**
  616. * Detect if `arguments` objects are `Object` objects (all but Narwhal and Opera < 10.5).
  617. *
  618. * @memberOf _.support
  619. * @type boolean
  620. */
  621. support.argsObject = arguments.constructor == Object && !(arguments instanceof Array);
  622. /**
  623. * Detect if `name` or `message` properties of `Error.prototype` are
  624. * enumerable by default. (IE < 9, Safari < 5.1)
  625. *
  626. * @memberOf _.support
  627. * @type boolean
  628. */
  629. support.enumErrorProps = propertyIsEnumerable.call(errorProto, 'message') || propertyIsEnumerable.call(errorProto, 'name');
  630. /**
  631. * Detect if `prototype` properties are enumerable by default.
  632. *
  633. * Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1
  634. * (if the prototype or a property on the prototype has been set)
  635. * incorrectly sets a function's `prototype` property [[Enumerable]]
  636. * value to `true`.
  637. *
  638. * @memberOf _.support
  639. * @type boolean
  640. */
  641. support.enumPrototypes = propertyIsEnumerable.call(ctor, 'prototype');
  642. /**
  643. * Detect if functions can be decompiled by `Function#toString`
  644. * (all but PS3 and older Opera mobile browsers & avoided in Windows 8 apps).
  645. *
  646. * @memberOf _.support
  647. * @type boolean
  648. */
  649. support.funcDecomp = !isNative(context.WinRTError) && reThis.test(runInContext);
  650. /**
  651. * Detect if `Function#name` is supported (all but IE).
  652. *
  653. * @memberOf _.support
  654. * @type boolean
  655. */
  656. support.funcNames = typeof Function.name == 'string';
  657. /**
  658. * Detect if `arguments` object indexes are non-enumerable
  659. * (Firefox < 4, IE < 9, PhantomJS, Safari < 5.1).
  660. *
  661. * @memberOf _.support
  662. * @type boolean
  663. */
  664. support.nonEnumArgs = key != 0;
  665. /**
  666. * Detect if properties shadowing those on `Object.prototype` are non-enumerable.
  667. *
  668. * In IE < 9 an objects own properties, shadowing non-enumerable ones, are
  669. * made non-enumerable as well (a.k.a the JScript [[DontEnum]] bug).
  670. *
  671. * @memberOf _.support
  672. * @type boolean
  673. */
  674. support.nonEnumShadows = !/valueOf/.test(props);
  675. /**
  676. * Detect if own properties are iterated after inherited properties (all but IE < 9).
  677. *
  678. * @memberOf _.support
  679. * @type boolean
  680. */
  681. support.ownLast = props[0] != 'x';
  682. /**
  683. * Detect if `Array#shift` and `Array#splice` augment array-like objects correctly.
  684. *
  685. * Firefox < 10, IE compatibility mode, and IE < 9 have buggy Array `shift()`
  686. * and `splice()` functions that fail to remove the last element, `value[0]`,
  687. * of array-like objects even though the `length` property is set to `0`.
  688. * The `shift()` method is buggy in IE 8 compatibility mode, while `splice()`
  689. * is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
  690. *
  691. * @memberOf _.support
  692. * @type boolean
  693. */
  694. support.spliceObjects = (arrayRef.splice.call(object, 0, 1), !object[0]);
  695. /**
  696. * Detect lack of support for accessing string characters by index.
  697. *
  698. * IE < 8 can't access characters by index and IE 8 can only access
  699. * characters by index on string literals.
  700. *
  701. * @memberOf _.support
  702. * @type boolean
  703. */
  704. support.unindexedChars = ('x'[0] + Object('x')[0]) != 'xx';
  705. /**
  706. * Detect if a DOM node's [[Class]] is resolvable (all but IE < 9)
  707. * and that the JS engine errors when attempting to coerce an object to
  708. * a string without a `toString` function.
  709. *
  710. * @memberOf _.support
  711. * @type boolean
  712. */
  713. try {
  714. support.nodeClass = !(toString.call(document) == objectClass && !({ 'toString': 0 } + ''));
  715. } catch(e) {
  716. support.nodeClass = true;
  717. }
  718. }(1));
  719. /**
  720. * By default, the template delimiters used by Lo-Dash are similar to those in
  721. * embedded Ruby (ERB). Change the following template settings to use alternative
  722. * delimiters.
  723. *
  724. * @static
  725. * @memberOf _
  726. * @type Object
  727. */
  728. lodash.templateSettings = {
  729. /**
  730. * Used to detect `data` property values to be HTML-escaped.
  731. *
  732. * @memberOf _.templateSettings
  733. * @type RegExp
  734. */
  735. 'escape': /<%-([\s\S]+?)%>/g,
  736. /**
  737. * Used to detect code to be evaluated.
  738. *
  739. * @memberOf _.templateSettings
  740. * @type RegExp
  741. */
  742. 'evaluate': /<%([\s\S]+?)%>/g,
  743. /**
  744. * Used to detect `data` property values to inject.
  745. *
  746. * @memberOf _.templateSettings
  747. * @type RegExp
  748. */
  749. 'interpolate': reInterpolate,
  750. /**
  751. * Used to reference the data object in the template text.
  752. *
  753. * @memberOf _.templateSettings
  754. * @type string
  755. */
  756. 'variable': '',
  757. /**
  758. * Used to import variables into the compiled template.
  759. *
  760. * @memberOf _.templateSettings
  761. * @type Object
  762. */
  763. 'imports': {
  764. /**
  765. * A reference to the `lodash` function.
  766. *
  767. * @memberOf _.templateSettings.imports
  768. * @type Function
  769. */
  770. '_': lodash
  771. }
  772. };
  773. /*--------------------------------------------------------------------------*/
  774. /**
  775. * The template used to create iterator functions.
  776. *
  777. * @private
  778. * @param {Object} data The data object used to populate the text.
  779. * @returns {string} Returns the interpolated text.
  780. */
  781. var iteratorTemplate = function(obj) {
  782. var __p = 'var index, iterable = ' +
  783. (obj.firstArg) +
  784. ', result = ' +
  785. (obj.init) +
  786. ';\nif (!iterable) return result;\n' +
  787. (obj.top) +
  788. ';';
  789. if (obj.array) {
  790. __p += '\nvar length = iterable.length; index = -1;\nif (' +
  791. (obj.array) +
  792. ') { ';
  793. if (support.unindexedChars) {
  794. __p += '\n if (isString(iterable)) {\n iterable = iterable.split(\'\')\n } ';
  795. }
  796. __p += '\n while (++index < length) {\n ' +
  797. (obj.loop) +
  798. ';\n }\n}\nelse { ';
  799. } else if (support.nonEnumArgs) {
  800. __p += '\n var length = iterable.length; index = -1;\n if (length && isArguments(iterable)) {\n while (++index < length) {\n index += \'\';\n ' +
  801. (obj.loop) +
  802. ';\n }\n } else { ';
  803. }
  804. if (support.enumPrototypes) {
  805. __p += '\n var skipProto = typeof iterable == \'function\';\n ';
  806. }
  807. if (support.enumErrorProps) {
  808. __p += '\n var skipErrorProps = iterable === errorProto || iterable instanceof Error;\n ';
  809. }
  810. var conditions = []; if (support.enumPrototypes) { conditions.push('!(skipProto && index == "prototype")'); } if (support.enumErrorProps) { conditions.push('!(skipErrorProps && (index == "message" || index == "name"))'); }
  811. if (obj.useHas && obj.keys) {
  812. __p += '\n var ownIndex = -1,\n ownProps = objectTypes[typeof iterable] && keys(iterable),\n length = ownProps ? ownProps.length : 0;\n\n while (++ownIndex < length) {\n index = ownProps[ownIndex];\n';
  813. if (conditions.length) {
  814. __p += ' if (' +
  815. (conditions.join(' && ')) +
  816. ') {\n ';
  817. }
  818. __p +=
  819. (obj.loop) +
  820. '; ';
  821. if (conditions.length) {
  822. __p += '\n }';
  823. }
  824. __p += '\n } ';
  825. } else {
  826. __p += '\n for (index in iterable) {\n';
  827. if (obj.useHas) { conditions.push("hasOwnProperty.call(iterable, index)"); } if (conditions.length) {
  828. __p += ' if (' +
  829. (conditions.join(' && ')) +
  830. ') {\n ';
  831. }
  832. __p +=
  833. (obj.loop) +
  834. '; ';
  835. if (conditions.length) {
  836. __p += '\n }';
  837. }
  838. __p += '\n } ';
  839. if (support.nonEnumShadows) {
  840. __p += '\n\n if (iterable !== objectProto) {\n var ctor = iterable.constructor,\n isProto = iterable === (ctor && ctor.prototype),\n className = iterable === stringProto ? stringClass : iterable === errorProto ? errorClass : toString.call(iterable),\n nonEnum = nonEnumProps[className];\n ';
  841. for (k = 0; k < 7; k++) {
  842. __p += '\n index = \'' +
  843. (obj.shadowedProps[k]) +
  844. '\';\n if ((!(isProto && nonEnum[index]) && hasOwnProperty.call(iterable, index))';
  845. if (!obj.useHas) {
  846. __p += ' || (!nonEnum[index] && iterable[index] !== objectProto[index])';
  847. }
  848. __p += ') {\n ' +
  849. (obj.loop) +
  850. ';\n } ';
  851. }
  852. __p += '\n } ';
  853. }
  854. }
  855. if (obj.array || support.nonEnumArgs) {
  856. __p += '\n}';
  857. }
  858. __p +=
  859. (obj.bottom) +
  860. ';\nreturn result';
  861. return __p
  862. };
  863. /*--------------------------------------------------------------------------*/
  864. /**
  865. * The base implementation of `_.bind` that creates the bound function and
  866. * sets its meta data.
  867. *
  868. * @private
  869. * @param {Array} bindData The bind data array.
  870. * @returns {Function} Returns the new bound function.
  871. */
  872. function baseBind(bindData) {
  873. var func = bindData[0],
  874. partialArgs = bindData[2],
  875. thisArg = bindData[4];
  876. function bound() {
  877. // `Function#bind` spec
  878. // http://es5.github.io/#x15.3.4.5
  879. if (partialArgs) {
  880. // avoid `arguments` object deoptimizations by using `slice` instead
  881. // of `Array.prototype.slice.call` and not assigning `arguments` to a
  882. // variable as a ternary expression
  883. var args = slice(partialArgs);
  884. push.apply(args, arguments);
  885. }
  886. // mimic the constructor's `return` behavior
  887. // http://es5.github.io/#x13.2.2
  888. if (this instanceof bound) {
  889. // ensure `new bound` is an instance of `func`
  890. var thisBinding = baseCreate(func.prototype),
  891. result = func.apply(thisBinding, args || arguments);
  892. return isObject(result) ? result : thisBinding;
  893. }
  894. return func.apply(thisArg, args || arguments);
  895. }
  896. setBindData(bound, bindData);
  897. return bound;
  898. }
  899. /**
  900. * The base implementation of `_.clone` without argument juggling or support
  901. * for `thisArg` binding.
  902. *
  903. * @private
  904. * @param {*} value The value to clone.
  905. * @param {boolean} [isDeep=false] Specify a deep clone.
  906. * @param {Function} [callback] The function to customize cloning values.
  907. * @param {Array} [stackA=[]] Tracks traversed source objects.
  908. * @param {Array} [stackB=[]] Associates clones with source counterparts.
  909. * @returns {*} Returns the cloned value.
  910. */
  911. function baseClone(value, isDeep, callback, stackA, stackB) {
  912. if (callback) {
  913. var result = callback(value);
  914. if (typeof result != 'undefined') {
  915. return result;
  916. }
  917. }
  918. // inspect [[Class]]
  919. var isObj = isObject(value);
  920. if (isObj) {
  921. var className = toString.call(value);
  922. if (!cloneableClasses[className] || (!support.nodeClass && isNode(value))) {
  923. return value;
  924. }
  925. var ctor = ctorByClass[className];
  926. switch (className) {
  927. case boolClass:
  928. case dateClass:
  929. return new ctor(+value);
  930. case numberClass:
  931. case stringClass:
  932. return new ctor(value);
  933. case regexpClass:
  934. result = ctor(value.source, reFlags.exec(value));
  935. result.lastIndex = value.lastIndex;
  936. return result;
  937. }
  938. } else {
  939. return value;
  940. }
  941. var isArr = isArray(value);
  942. if (isDeep) {
  943. // check for circular references and return corresponding clone
  944. var initedStack = !stackA;
  945. stackA || (stackA = getArray());
  946. stackB || (stackB = getArray());
  947. var length = stackA.length;
  948. while (length--) {
  949. if (stackA[length] == value) {
  950. return stackB[length];
  951. }
  952. }
  953. result = isArr ? ctor(value.length) : {};
  954. }
  955. else {
  956. result = isArr ? slice(value) : assign({}, value);
  957. }
  958. // add array properties assigned by `RegExp#exec`
  959. if (isArr) {
  960. if (hasOwnProperty.call(value, 'index')) {
  961. result.index = value.index;
  962. }
  963. if (hasOwnProperty.call(value, 'input')) {
  964. result.input = value.input;
  965. }
  966. }
  967. // exit for shallow clone
  968. if (!isDeep) {
  969. return result;
  970. }
  971. // add the source value to the stack of traversed objects
  972. // and associate it with its clone
  973. stackA.push(value);
  974. stackB.push(result);
  975. // recursively populate clone (susceptible to call stack limits)
  976. (isArr ? baseEach : forOwn)(value, function(objValue, key) {
  977. result[key] = baseClone(objValue, isDeep, callback, stackA, stackB);
  978. });
  979. if (initedStack) {
  980. releaseArray(stackA);
  981. releaseArray(stackB);
  982. }
  983. return result;
  984. }
  985. /**
  986. * The base implementation of `_.create` without support for assigning
  987. * properties to the created object.
  988. *
  989. * @private
  990. * @param {Object} prototype The object to inherit from.
  991. * @returns {Object} Returns the new object.
  992. */
  993. function baseCreate(prototype, properties) {
  994. return isObject(prototype) ? nativeCreate(prototype) : {};
  995. }
  996. // fallback for browsers without `Object.create`
  997. if (!nativeCreate) {
  998. baseCreate = (function() {
  999. function Object() {}
  1000. return function(prototype) {
  1001. if (isObject(prototype)) {
  1002. Object.prototype = prototype;
  1003. var result = new Object;
  1004. Object.prototype = null;
  1005. }
  1006. return result || context.Object();
  1007. };
  1008. }());
  1009. }
  1010. /**
  1011. * The base implementation of `_.createCallback` without support for creating
  1012. * "_.pluck" or "_.where" style callbacks.
  1013. *
  1014. * @private
  1015. * @param {*} [func=identity] The value to convert to a callback.
  1016. * @param {*} [thisArg] The `this` binding of the created callback.
  1017. * @param {number} [argCount] The number of arguments the callback accepts.
  1018. * @returns {Function} Returns a callback function.
  1019. */
  1020. function baseCreateCallback(func, thisArg, argCount) {
  1021. if (typeof func != 'function') {
  1022. return identity;
  1023. }
  1024. // exit early for no `thisArg` or already bound by `Function#bind`
  1025. if (typeof thisArg == 'undefined' || !('prototype' in func)) {
  1026. return func;
  1027. }
  1028. var bindData = func.__bindData__;
  1029. if (typeof bindData == 'undefined') {
  1030. if (support.funcNames) {
  1031. bindData = !func.name;
  1032. }
  1033. bindData = bindData || !support.funcDecomp;
  1034. if (!bindData) {
  1035. var source = fnToString.call(func);
  1036. if (!support.funcNames) {
  1037. bindData = !reFuncName.test(source);
  1038. }
  1039. if (!bindData) {
  1040. // checks if `func` references the `this` keyword and stores the result
  1041. bindData = reThis.test(source);
  1042. setBindData(func, bindData);
  1043. }
  1044. }
  1045. }
  1046. // exit early if there are no `this` references or `func` is bound
  1047. if (bindData === false || (bindData !== true && bindData[1] & 1)) {
  1048. return func;
  1049. }
  1050. switch (argCount) {
  1051. case 1: return function(value) {
  1052. return func.call(thisArg, value);
  1053. };
  1054. case 2: return function(a, b) {
  1055. return func.call(thisArg, a, b);
  1056. };
  1057. case 3: return function(value, index, collection) {
  1058. return func.call(thisArg, value, index, collection);
  1059. };
  1060. case 4: return function(accumulator, value, index, collection) {
  1061. return func.call(thisArg, accumulator, value, index, collection);
  1062. };
  1063. }
  1064. return bind(func, thisArg);
  1065. }
  1066. /**
  1067. * The base implementation of `createWrapper` that creates the wrapper and
  1068. * sets its meta data.
  1069. *
  1070. * @private
  1071. * @param {Array} bindData The bind data array.
  1072. * @returns {Function} Returns the new function.
  1073. */
  1074. function baseCreateWrapper(bindData) {
  1075. var func = bindData[0],
  1076. bitmask = bindData[1],
  1077. partialArgs = bindData[2],
  1078. partialRightArgs = bindData[3],
  1079. thisArg = bindData[4],
  1080. arity = bindData[5];
  1081. var isBind = bitmask & 1,
  1082. isBindKey = bitmask & 2,
  1083. isCurry = bitmask & 4,
  1084. isCurryBound = bitmask & 8,
  1085. key = func;
  1086. function bound() {
  1087. var thisBinding = isBind ? thisArg : this;
  1088. if (partialArgs) {
  1089. var args = slice(partialArgs);
  1090. push.apply(args, arguments);
  1091. }
  1092. if (partialRightArgs || isCurry) {
  1093. args || (args = slice(arguments));
  1094. if (partialRightArgs) {
  1095. push.apply(args, partialRightArgs);
  1096. }
  1097. if (isCurry && args.length < arity) {
  1098. bitmask |= 16 & ~32;
  1099. return baseCreateWrapper([func, (isCurryBound ? bitmask : bitmask & ~3), args, null, thisArg, arity]);
  1100. }
  1101. }
  1102. args || (args = arguments);
  1103. if (isBindKey) {
  1104. func = thisBinding[key];
  1105. }
  1106. if (this instanceof bound) {
  1107. thisBinding = baseCreate(func.prototype);
  1108. var result = func.apply(thisBinding, args);
  1109. return isObject(result) ? result : thisBinding;
  1110. }
  1111. return func.apply(thisBinding, args);
  1112. }
  1113. setBindData(bound, bindData);
  1114. return bound;
  1115. }
  1116. /**
  1117. * The base implementation of `_.difference` that accepts a single array
  1118. * of values to exclude.
  1119. *
  1120. * @private
  1121. * @param {Array} array The array to process.
  1122. * @param {Array} [values] The array of values to exclude.
  1123. * @returns {Array} Returns a new array of filtered values.
  1124. */
  1125. function baseDifference(array, values) {
  1126. var index = -1,
  1127. indexOf = getIndexOf(),
  1128. length = array ? array.length : 0,
  1129. isLarge = length >= largeArraySize && indexOf === baseIndexOf,
  1130. result = [];
  1131. if (isLarge) {
  1132. var cache = createCache(values);
  1133. if (cache) {
  1134. indexOf = cacheIndexOf;
  1135. values = cache;
  1136. } else {
  1137. isLarge = false;
  1138. }
  1139. }
  1140. while (++index < length) {
  1141. var value = array[index];
  1142. if (indexOf(values, value) < 0) {
  1143. result.push(value);
  1144. }
  1145. }
  1146. if (isLarge) {
  1147. releaseObject(values);
  1148. }
  1149. return result;
  1150. }
  1151. /**
  1152. * The base implementation of `_.flatten` without support for callback
  1153. * shorthands or `thisArg` binding.
  1154. *
  1155. * @private
  1156. * @param {Array} array The array to flatten.
  1157. * @param {boolean} [isShallow=false] A flag to restrict flattening to a single level.
  1158. * @param {boolean} [isStrict=false] A flag to restrict flattening to arrays and `arguments` objects.
  1159. * @param {number} [fromIndex=0] The index to start from.
  1160. * @returns {Array} Returns a new flattened array.
  1161. */
  1162. function baseFlatten(array, isShallow, isStrict, fromIndex) {
  1163. var index = (fromIndex || 0) - 1,
  1164. length = array ? array.length : 0,
  1165. result = [];
  1166. while (++index < length) {
  1167. var value = array[index];
  1168. if (value && typeof value == 'object' && typeof value.length == 'number'
  1169. && (isArray(value) || isArguments(value))) {
  1170. // recursively flatten arrays (susceptible to call stack limits)
  1171. if (!isShallow) {
  1172. value = baseFlatten(value, isShallow, isStrict);
  1173. }
  1174. var valIndex = -1,
  1175. valLength = value.length,
  1176. resIndex = result.length;
  1177. result.length += valLength;
  1178. while (++valIndex < valLength) {
  1179. result[resIndex++] = value[valIndex];
  1180. }
  1181. } else if (!isStrict) {
  1182. result.push(value);
  1183. }
  1184. }
  1185. return result;
  1186. }
  1187. /**
  1188. * The base implementation of `_.isEqual`, without support for `thisArg` binding,
  1189. * that allows partial "_.where" style comparisons.
  1190. *
  1191. * @private
  1192. * @param {*} a The value to compare.
  1193. * @param {*} b The other value to compare.
  1194. * @param {Function} [callback] The function to customize comparing values.
  1195. * @param {Function} [isWhere=false] A flag to indicate performing partial comparisons.
  1196. * @param {Array} [stackA=[]] Tracks traversed `a` objects.
  1197. * @param {Array} [stackB=[]] Tracks traversed `b` objects.
  1198. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  1199. */
  1200. function baseIsEqual(a, b, callback, isWhere, stackA, stackB) {
  1201. // used to indicate that when comparing objects, `a` has at least the properties of `b`
  1202. if (callback) {
  1203. var result = callback(a, b);
  1204. if (typeof result != 'undefined') {
  1205. return !!result;
  1206. }
  1207. }
  1208. // exit early for identical values
  1209. if (a === b) {
  1210. // treat `+0` vs. `-0` as not equal
  1211. return a !== 0 || (1 / a == 1 / b);
  1212. }
  1213. var type = typeof a,
  1214. otherType = typeof b;
  1215. // exit early for unlike primitive values
  1216. if (a === a &&
  1217. !(a && objectTypes[type]) &&
  1218. !(b && objectTypes[otherType])) {
  1219. return false;
  1220. }
  1221. // exit early for `null` and `undefined` avoiding ES3's Function#call behavior
  1222. // http://es5.github.io/#x15.3.4.4
  1223. if (a == null || b == null) {
  1224. return a === b;
  1225. }
  1226. // compare [[Class]] names
  1227. var className = toString.call(a),
  1228. otherClass = toString.call(b);
  1229. if (className == argsClass) {
  1230. className = objectClass;
  1231. }
  1232. if (otherClass == argsClass) {
  1233. otherClass = objectClass;
  1234. }
  1235. if (className != otherClass) {
  1236. return false;
  1237. }
  1238. switch (className) {
  1239. case boolClass:
  1240. case dateClass:
  1241. // coerce dates and booleans to numbers, dates to milliseconds and booleans
  1242. // to `1` or `0` treating invalid dates coerced to `NaN` as not equal
  1243. return +a == +b;
  1244. case numberClass:
  1245. // treat `NaN` vs. `NaN` as equal
  1246. return (a != +a)
  1247. ? b != +b
  1248. // but treat `+0` vs. `-0` as not equal
  1249. : (a == 0 ? (1 / a == 1 / b) : a == +b);
  1250. case regexpClass:
  1251. case stringClass:
  1252. // coerce regexes to strings (http://es5.github.io/#x15.10.6.4)
  1253. // treat string primitives and their corresponding object instances as equal
  1254. return a == String(b);
  1255. }
  1256. var isArr = className == arrayClass;
  1257. if (!isArr) {
  1258. // unwrap any `lodash` wrapped values
  1259. var aWrapped = hasOwnProperty.call(a, '__wrapped__'),
  1260. bWrapped = hasOwnProperty.call(b, '__wrapped__');
  1261. if (aWrapped || bWrapped) {
  1262. return baseIsEqual(aWrapped ? a.__wrapped__ : a, bWrapped ? b.__wrapped__ : b, callback, isWhere, stackA, stackB);
  1263. }
  1264. // exit for functions and DOM nodes
  1265. if (className != objectClass || (!support.nodeClass && (isNode(a) || isNode(b)))) {
  1266. return false;
  1267. }
  1268. // in older versions of Opera, `arguments` objects have `Array` constructors
  1269. var ctorA = !support.argsObject && isArguments(a) ? Object : a.constructor,
  1270. ctorB = !support.argsObject && isArguments(b) ? Object : b.constructor;
  1271. // non `Object` object instances with different constructors are not equal
  1272. if (ctorA != ctorB &&
  1273. !(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) &&
  1274. ('constructor' in a && 'constructor' in b)
  1275. ) {
  1276. return false;
  1277. }
  1278. }
  1279. // assume cyclic structures are equal
  1280. // the algorithm for detecting cyclic structures is adapted from ES 5.1
  1281. // section 15.12.3, abstract operation `JO` (http://es5.github.io/#x15.12.3)
  1282. var initedStack = !stackA;
  1283. stackA || (stackA = getArray());
  1284. stackB || (stackB = getArray());
  1285. var length = stackA.length;
  1286. while (length--) {
  1287. if (stackA[length] == a) {
  1288. return stackB[length] == b;
  1289. }
  1290. }
  1291. var size = 0;
  1292. result = true;
  1293. // add `a` and `b` to the stack of traversed objects
  1294. stackA.push(a);
  1295. stackB.push(b);
  1296. // recursively compare objects and arrays (susceptible to call stack limits)
  1297. if (isArr) {
  1298. // compare lengths to determine if a deep comparison is necessary
  1299. length = a.length;
  1300. size = b.length;
  1301. result = size == length;
  1302. if (result || isWhere) {
  1303. // deep compare the contents, ignoring non-numeric properties
  1304. while (size--) {
  1305. var index = length,
  1306. value = b[size];
  1307. if (isWhere) {
  1308. while (index--) {
  1309. if ((result = baseIsEqual(a[index], value, callback, isWhere, stackA, stackB))) {
  1310. break;
  1311. }
  1312. }
  1313. } else if (!(result = baseIsEqual(a[size], value, callback, isWhere, stackA, stackB))) {
  1314. break;
  1315. }
  1316. }
  1317. }
  1318. }
  1319. else {
  1320. // deep compare objects using `forIn`, instead of `forOwn`, to avoid `Object.keys`
  1321. // which, in this case, is more costly
  1322. forIn(b, function(value, key, b) {
  1323. if (hasOwnProperty.call(b, key)) {
  1324. // count the number of properties.
  1325. size++;
  1326. // deep compare each property value.
  1327. return (result = hasOwnProperty.call(a, key) && baseIsEqual(a[key], value, callback, isWhere, stackA, stackB));
  1328. }
  1329. });
  1330. if (result && !isWhere) {
  1331. // ensure both objects have the same number of properties
  1332. forIn(a, function(value, key, a) {
  1333. if (hasOwnProperty.call(a, key)) {
  1334. // `size` will be `-1` if `a` has more properties than `b`
  1335. return (result = --size > -1);
  1336. }
  1337. });
  1338. }
  1339. }
  1340. stackA.pop();
  1341. stackB.pop();
  1342. if (initedStack) {
  1343. releaseArray(stackA);
  1344. releaseArray(stackB);
  1345. }
  1346. return result;
  1347. }
  1348. /**
  1349. * The base implementation of `_.merge` without argument juggling or support
  1350. * for `thisArg` binding.
  1351. *
  1352. * @private
  1353. * @param {Object} object The destination object.
  1354. * @param {Object} source The source object.
  1355. * @param {Function} [callback] The function to customize merging properties.
  1356. * @param {Array} [stackA=[]] Tracks traversed source objects.
  1357. * @param {Array} [stackB=[]] Associates values with source counterparts.
  1358. */
  1359. function baseMerge(object, source, callback, stackA, stackB) {
  1360. (isArray(source) ? forEach : forOwn)(source, function(source, key) {
  1361. var found,
  1362. isArr,
  1363. result = source,
  1364. value = object[key];
  1365. if (source && ((isArr = isArray(source)) || isPlainObject(source))) {
  1366. // avoid merging previously merged cyclic sources
  1367. var stackLength = stackA.length;
  1368. while (stackLength--) {
  1369. if ((found = stackA[stackLength] == source)) {
  1370. value = stackB[stackLength];
  1371. break;
  1372. }
  1373. }
  1374. if (!found) {
  1375. var isShallow;
  1376. if (callback) {
  1377. result = callback(value, source);
  1378. if ((isShallow = typeof result != 'undefined')) {
  1379. value = result;
  1380. }
  1381. }
  1382. if (!isShallow) {
  1383. value = isArr
  1384. ? (isArray(value) ? value : [])
  1385. : (isPlainObject(value) ? value : {});
  1386. }
  1387. // add `source` and associated `value` to the stack of traversed objects
  1388. stackA.push(source);
  1389. stackB.push(value);
  1390. // recursively merge objects and arrays (susceptible to call stack limits)
  1391. if (!isShallow) {
  1392. baseMerge(value, source, callback, stackA, stackB);
  1393. }
  1394. }
  1395. }
  1396. else {
  1397. if (callback) {
  1398. result = callback(value, source);
  1399. if (typeof result == 'undefined') {
  1400. result = source;
  1401. }
  1402. }
  1403. if (typeof result != 'undefined') {
  1404. value = result;
  1405. }
  1406. }
  1407. object[key] = value;
  1408. });
  1409. }
  1410. /**
  1411. * The base implementation of `_.random` without argument juggling or support
  1412. * for returning floating-point numbers.
  1413. *
  1414. * @private
  1415. * @param {number} min The minimum possible value.
  1416. * @param {number} max The maximum possible value.
  1417. * @returns {number} Returns a random number.
  1418. */
  1419. function baseRandom(min, max) {
  1420. return min + floor(nativeRandom() * (max - min + 1));
  1421. }
  1422. /**
  1423. * The base implementation of `_.uniq` without support for callback shorthands
  1424. * or `thisArg` binding.
  1425. *
  1426. * @private
  1427. * @param {Array} array The array to process.
  1428. * @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted.
  1429. * @param {Function} [callback] The function called per iteration.
  1430. * @returns {Array} Returns a duplicate-value-free array.
  1431. */
  1432. function baseUniq(array, isSorted, callback) {
  1433. var index = -1,
  1434. indexOf = getIndexOf(),
  1435. length = array ? array.length : 0,
  1436. result = [];
  1437. var isLarge = !isSorted && length >= largeArraySize && indexOf === baseIndexOf,
  1438. seen = (callback || isLarge) ? getArray() : result;
  1439. if (isLarge) {
  1440. var cache = createCache(seen);
  1441. indexOf = cacheIndexOf;
  1442. seen = cache;
  1443. }
  1444. while (++index < length) {
  1445. var value = array[index],
  1446. computed = callback ? callback(value, index, array) : value;
  1447. if (isSorted
  1448. ? !index || seen[seen.length - 1] !== computed
  1449. : indexOf(seen, computed) < 0
  1450. ) {
  1451. if (callback || isLarge) {
  1452. seen.push(computed);
  1453. }
  1454. result.push(value);
  1455. }
  1456. }
  1457. if (isLarge) {
  1458. releaseArray(seen.array);
  1459. releaseObject(seen);
  1460. } else if (callback) {
  1461. releaseArray(seen);
  1462. }
  1463. return result;
  1464. }
  1465. /**
  1466. * Creates a function that aggregates a collection, creating an object composed
  1467. * of keys generated from the results of running each element of the collection
  1468. * through a callback. The given `setter` function sets the keys and values
  1469. * of the composed object.
  1470. *
  1471. * @private
  1472. * @param {Function} setter The setter function.
  1473. * @returns {Function} Returns the new aggregator function.
  1474. */
  1475. function createAggregator(setter) {
  1476. return function(collection, callback, thisArg) {
  1477. var result = {};
  1478. callback = lodash.createCallback(callback, thisArg, 3);
  1479. if (isArray(collection)) {
  1480. var index = -1,
  1481. length = collection.length;
  1482. while (++index < length) {
  1483. var value = collection[index];
  1484. setter(result, value, callback(value, index, collection), collection);
  1485. }
  1486. } else {
  1487. baseEach(collection, function(value, key, collection) {
  1488. setter(result, value, callback(value, key, collection), collection);
  1489. });
  1490. }
  1491. return result;
  1492. };
  1493. }
  1494. /**
  1495. * Creates a function that, when called, either curries or invokes `func`
  1496. * with an optional `this` binding and partially applied arguments.
  1497. *
  1498. * @private
  1499. * @param {Function|string} func The function or method name to reference.
  1500. * @param {number} bitmask The bitmask of method flags to compose.
  1501. * The bitmask may be composed of the following flags:
  1502. * 1 - `_.bind`
  1503. * 2 - `_.bindKey`
  1504. * 4 - `_.curry`
  1505. * 8 - `_.curry` (bound)
  1506. * 16 - `_.partial`
  1507. * 32 - `_.partialRight`
  1508. * @param {Array} [partialArgs] An array of arguments to prepend to those
  1509. * provided to the new function.
  1510. * @param {Array} [partialRightArgs] An array of arguments to append to those
  1511. * provided to the new function.
  1512. * @param {*} [thisArg] The `this` binding of `func`.
  1513. * @param {number} [arity] The arity of `func`.
  1514. * @returns {Function} Returns the new function.
  1515. */
  1516. function createWrapper(func, bitmask, partialArgs, partialRightArgs, thisArg, arity) {
  1517. var isBind = bitmask & 1,
  1518. isBindKey = bitmask & 2,
  1519. isCurry = bitmask & 4,
  1520. isCurryBound = bitmask & 8,
  1521. isPartial = bitmask & 16,
  1522. isPartialRight = bitmask & 32;
  1523. if (!isBindKey && !isFunction(func)) {
  1524. throw new TypeError;
  1525. }
  1526. if (isPartial && !partialArgs.length) {
  1527. bitmask &= ~16;
  1528. isPartial = partialArgs = false;
  1529. }
  1530. if (isPartialRight && !partialRightArgs.length) {
  1531. bitmask &= ~32;
  1532. isPartialRight = partialRightArgs = false;
  1533. }
  1534. var bindData = func && func.__bindData__;
  1535. if (bindData && bindData !== true) {
  1536. // clone `bindData`
  1537. bindData = slice(bindData);
  1538. if (bindData[2]) {
  1539. bindData[2] = slice(bindData[2]);
  1540. }
  1541. if (bindData[3]) {
  1542. bindData[3] = slice(bindData[3]);
  1543. }
  1544. // set `thisBinding` is not previously bound
  1545. if (isBind && !(bindData[1] & 1)) {
  1546. bindData[4] = thisArg;
  1547. }
  1548. // set if previously bound but not currently (subsequent curried functions)
  1549. if (!isBind && bindData[1] & 1) {
  1550. bitmask |= 8;
  1551. }
  1552. // set curried arity if not yet set
  1553. if (isCurry && !(bindData[1] & 4)) {
  1554. bindData[5] = arity;
  1555. }
  1556. // append partial left arguments
  1557. if (isPartial) {
  1558. push.apply(bindData[2] || (bindData[2] = []), partialArgs);
  1559. }
  1560. // append partial right arguments
  1561. if (isPartialRight) {
  1562. unshift.apply(bindData[3] || (bindData[3] = []), partialRightArgs);
  1563. }
  1564. // merge flags
  1565. bindData[1] |= bitmask;
  1566. return createWrapper.apply(null, bindData);
  1567. }
  1568. // fast path for `_.bind`
  1569. var creater = (bitmask == 1 || bitmask === 17) ? baseBind : baseCreateWrapper;
  1570. return creater([func, bitmask, partialArgs, partialRightArgs, thisArg, arity]);
  1571. }
  1572. /**
  1573. * Creates compiled iteration functions.
  1574. *
  1575. * @private
  1576. * @param {...Object} [options] The compile options object(s).
  1577. * @param {string} [options.array] Code to determine if the iterable is an array or array-like.
  1578. * @param {boolean} [options.useHas] Specify using `hasOwnProperty` checks in the object loop.
  1579. * @param {Function} [options.keys] A reference to `_.keys` for use in own property iteration.
  1580. * @param {string} [options.args] A comma separated string of iteration function arguments.
  1581. * @param {string} [options.top] Code to execute before the iteration branches.
  1582. * @param {string} [options.loop] Code to execute in the object loop.
  1583. * @param {string} [options.bottom] Code to execute after the iteration branches.
  1584. * @returns {Function} Returns the compiled function.
  1585. */
  1586. function createIterator() {
  1587. // data properties
  1588. iteratorData.shadowedProps = shadowedProps;
  1589. // iterator options
  1590. iteratorData.array = iteratorData.bottom = iteratorData.loop = iteratorData.top = '';
  1591. iteratorData.init = 'iterable';
  1592. iteratorData.useHas = true;
  1593. // merge options into a template data object
  1594. for (var object, index = 0; object = arguments[index]; index++) {
  1595. for (var key in object) {
  1596. iteratorData[key] = object[key];
  1597. }
  1598. }
  1599. var args = iteratorData.args;
  1600. iteratorData.firstArg = /^[^,]+/.exec(args)[0];
  1601. // create the function factory
  1602. var factory = Function(
  1603. 'baseCreateCallback, errorClass, errorProto, hasOwnProperty, ' +
  1604. 'indicatorObject, isArguments, isArray, isString, keys, objectProto, ' +
  1605. 'objectTypes, nonEnumProps, stringClass, stringProto, toString',
  1606. 'return function(' + args + ') {\n' + iteratorTemplate(iteratorData) + '\n}'
  1607. );
  1608. // return the compiled function
  1609. return factory(
  1610. baseCreateCallback, errorClass, errorProto, hasOwnProperty,
  1611. indicatorObject, isArguments, isArray, isString, iteratorData.keys, objectProto,
  1612. objectTypes, nonEnumProps, stringClass, stringProto, toString
  1613. );
  1614. }
  1615. /**
  1616. * Used by `escape` to convert characters to HTML entities.
  1617. *
  1618. * @private
  1619. * @param {string} match The matched character to escape.
  1620. * @returns {string} Returns the escaped character.
  1621. */
  1622. function escapeHtmlChar(match) {
  1623. return htmlEscapes[match];
  1624. }
  1625. /**
  1626. * Gets the appropriate "indexOf" function. If the `_.indexOf` method is
  1627. * customized, this method returns the custom method, otherwise it returns
  1628. * the `baseIndexOf` function.
  1629. *
  1630. * @private
  1631. * @returns {Function} Returns the "indexOf" function.
  1632. */
  1633. function getIndexOf() {
  1634. var result = (result = lodash.indexOf) === indexOf ? baseIndexOf : result;
  1635. return result;
  1636. }
  1637. /**
  1638. * Checks if `value` is a native function.
  1639. *
  1640. * @private
  1641. * @param {*} value The value to check.
  1642. * @returns {boolean} Returns `true` if the `value` is a native function, else `false`.
  1643. */
  1644. function isNative(value) {
  1645. return typeof value == 'function' && reNative.test(value);
  1646. }
  1647. /**
  1648. * Sets `this` binding data on a given function.
  1649. *
  1650. * @private
  1651. * @param {Function} func The function to set data on.
  1652. * @param {Array} value The data array to set.
  1653. */
  1654. var setBindData = !defineProperty ? noop : function(func, value) {
  1655. descriptor.value = value;
  1656. defineProperty(func, '__bindData__', descriptor);
  1657. descriptor.value = null;
  1658. };
  1659. /**
  1660. * A fallback implementation of `isPlainObject` which checks if a given value
  1661. * is an object created by the `Object` constructor, assuming objects created
  1662. * by the `Object` constructor have no inherited enumerable properties and that
  1663. * there are no `Object.prototype` extensions.
  1664. *
  1665. * @private
  1666. * @param {*} value The value to check.
  1667. * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
  1668. */
  1669. function shimIsPlainObject(value) {
  1670. var ctor,
  1671. result;
  1672. // avoid non Object objects, `arguments` objects, and DOM elements
  1673. if (!(value && toString.call(value) == objectClass) ||
  1674. (ctor = value.constructor, isFunction(ctor) && !(ctor instanceof ctor)) ||
  1675. (!support.argsClass && isArguments(value)) ||
  1676. (!support.nodeClass && isNode(value))) {
  1677. return false;
  1678. }
  1679. // IE < 9 iterates inherited properties before own properties. If the first
  1680. // iterated property is an object's own property then there are no inherited
  1681. // enumerable properties.
  1682. if (support.ownLast) {
  1683. forIn(value, function(value, key, object) {
  1684. result = hasOwnProperty.call(object, key);
  1685. return false;
  1686. });
  1687. return result !== false;
  1688. }
  1689. // In most environments an object's own properties are iterated before
  1690. // its inherited properties. If the last iterated property is an object's
  1691. // own property then there are no inherited enumerable properties.
  1692. forIn(value, function(value, key) {
  1693. result = key;
  1694. });
  1695. return typeof result == 'undefined' || hasOwnProperty.call(value, result);
  1696. }
  1697. /**
  1698. * Used by `unescape` to convert HTML entities to characters.
  1699. *
  1700. * @private
  1701. * @param {string} match The matched character to unescape.
  1702. * @returns {string} Returns the unescaped character.
  1703. */
  1704. function unescapeHtmlChar(match) {
  1705. return htmlUnescapes[match];
  1706. }
  1707. /*--------------------------------------------------------------------------*/
  1708. /**
  1709. * Checks if `value` is an `arguments` object.
  1710. *
  1711. * @static
  1712. * @memberOf _
  1713. * @category Objects
  1714. * @param {*} value The value to check.
  1715. * @returns {boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
  1716. * @example
  1717. *
  1718. * (function() { return _.isArguments(arguments); })(1, 2, 3);
  1719. * // => true
  1720. *
  1721. * _.isArguments([1, 2, 3]);
  1722. * // => false
  1723. */
  1724. function isArguments(value) {
  1725. return value && typeof value == 'object' && typeof value.length == 'number' &&
  1726. toString.call(value) == argsClass || false;
  1727. }
  1728. // fallback for browsers that can't detect `arguments` objects by [[Class]]
  1729. if (!support.argsClass) {
  1730. isArguments = function(value) {
  1731. return value && typeof value == 'object' && typeof value.length == 'number' &&
  1732. hasOwnProperty.call(value, 'callee') && !propertyIsEnumerable.call(value, 'callee') || false;
  1733. };
  1734. }
  1735. /**
  1736. * Checks if `value` is an array.
  1737. *
  1738. * @static
  1739. * @memberOf _
  1740. * @type Function
  1741. * @category Objects
  1742. * @param {*} value The value to check.
  1743. * @returns {boolean} Returns `true` if the `value` is an array, else `false`.
  1744. * @example
  1745. *
  1746. * (function() { return _.isArray(arguments); })();
  1747. * // => false
  1748. *
  1749. * _.isArray([1, 2, 3]);
  1750. * // => true
  1751. */
  1752. var isArray = nativeIsArray || function(value) {
  1753. return value && typeof value == 'object' && typeof value.length == 'number' &&
  1754. toString.call(value) == arrayClass || false;
  1755. };
  1756. /**
  1757. * A fallback implementation of `Object.keys` which produces an array of the
  1758. * given object's own enumerable property names.
  1759. *
  1760. * @private
  1761. * @type Function
  1762. * @param {Object} object The object to inspect.
  1763. * @returns {Array} Returns an array of property names.
  1764. */
  1765. var shimKeys = createIterator({
  1766. 'args': 'object',
  1767. 'init': '[]',
  1768. 'top': 'if (!(objectTypes[typeof object])) return result',
  1769. 'loop': 'result.push(index)'
  1770. });
  1771. /**
  1772. * Creates an array composed of the own enumerable property names of an object.
  1773. *
  1774. * @static
  1775. * @memberOf _
  1776. * @category Objects
  1777. * @param {Object} object The object to inspect.
  1778. * @returns {Array} Returns an array of property names.
  1779. * @example
  1780. *
  1781. * _.keys({ 'one': 1, 'two': 2, 'three': 3 });
  1782. * // => ['one', 'two', 'three'] (property order is not guaranteed across environments)
  1783. */
  1784. var keys = !nativeKeys ? shimKeys : function(object) {
  1785. if (!isObject(object)) {
  1786. return [];
  1787. }
  1788. if ((support.enumPrototypes && typeof object == 'function') ||
  1789. (support.nonEnumArgs && object.length && isArguments(object))) {
  1790. return shimKeys(object);
  1791. }
  1792. return nativeKeys(object);
  1793. };
  1794. /** Reusable iterator options shared by `each`, `forIn`, and `forOwn` */
  1795. var eachIteratorOptions = {
  1796. 'args': 'collection, callback, thisArg',
  1797. 'top': "callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3)",
  1798. 'array': "typeof length == 'number'",
  1799. 'keys': keys,
  1800. 'loop': 'if (callback(iterable[index], index, collection) === false) return result'
  1801. };
  1802. /** Reusable iterator options for `assign` and `defaults` */
  1803. var defaultsIteratorOptions = {
  1804. 'args': 'object, source, guard',
  1805. 'top':
  1806. 'var args = arguments,\n' +
  1807. ' argsIndex = 0,\n' +
  1808. " argsLength = typeof guard == 'number' ? 2 : args.length;\n" +
  1809. 'while (++argsIndex < argsLength) {\n' +
  1810. ' iterable = args[argsIndex];\n' +
  1811. ' if (iterable && objectTypes[typeof iterable]) {',
  1812. 'keys': keys,
  1813. 'loop': "if (typeof result[index] == 'undefined') result[index] = iterable[index]",
  1814. 'bottom': ' }\n}'
  1815. };
  1816. /** Reusable iterator options for `forIn` and `forOwn` */
  1817. var forOwnIteratorOptions = {
  1818. 'top': 'if (!objectTypes[typeof iterable]) return result;\n' + eachIteratorOptions.top,
  1819. 'array': false
  1820. };
  1821. /**
  1822. * Used to convert characters to HTML entities:
  1823. *
  1824. * Though the `>` character is escaped for symmetry, characters like `>` and `/`
  1825. * don't require escaping in HTML and have no special meaning unless they're part
  1826. * of a tag or an unquoted attribute value.
  1827. * http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
  1828. */
  1829. var htmlEscapes = {
  1830. '&': '&amp;',
  1831. '<': '&lt;',
  1832. '>': '&gt;',
  1833. '"': '&quot;',
  1834. "'": '&#39;'
  1835. };
  1836. /** Used to convert HTML entities to characters */
  1837. var htmlUnescapes = invert(htmlEscapes);
  1838. /** Used to match HTML entities and HTML characters */
  1839. var reEscapedHtml = RegExp('(' + keys(htmlUnescapes).join('|') + ')', 'g'),
  1840. reUnescapedHtml = RegExp('[' + keys(htmlEscapes).join('') + ']', 'g');
  1841. /**
  1842. * A function compiled to iterate `arguments` objects, arrays, objects, and
  1843. * strings consistenly across environments, executing the callback for each
  1844. * element in the collection. The callback is bound to `thisArg` and invoked
  1845. * with three arguments; (value, index|key, collection). Callbacks may exit
  1846. * iteration early by explicitly returning `false`.
  1847. *
  1848. * @private
  1849. * @type Function
  1850. * @param {Array|Object|string} collection The collection to iterate over.
  1851. * @param {Function} [callback=identity] The function called per iteration.
  1852. * @param {*} [thisArg] The `this` binding of `callback`.
  1853. * @returns {Array|Object|string} Returns `collection`.
  1854. */
  1855. var baseEach = createIterator(eachIteratorOptions);
  1856. /*--------------------------------------------------------------------------*/
  1857. /**
  1858. * Assigns own enumerable properties of source object(s) to the destination
  1859. * object. Subsequent sources will overwrite property assignments of previous
  1860. * sources. If a callback is provided it will be executed to produce the
  1861. * assigned values. The callback is bound to `thisArg` and invoked with two
  1862. * arguments; (objectValue, sourceValue).
  1863. *
  1864. * @static
  1865. * @memberOf _
  1866. * @type Function
  1867. * @alias extend
  1868. * @category Objects
  1869. * @param {Object} object The destination object.
  1870. * @param {...Object} [source] The source objects.
  1871. * @param {Function} [callback] The function to customize assigning values.
  1872. * @param {*} [thisArg] The `this` binding of `callback`.
  1873. * @returns {Object} Returns the destination object.
  1874. * @example
  1875. *
  1876. * _.assign({ 'name': 'fred' }, { 'employer': 'slate' });
  1877. * // => { 'name': 'fred', 'employer': 'slate' }
  1878. *
  1879. * var defaults = _.partialRight(_.assign, function(a, b) {
  1880. * return typeof a == 'undefined' ? b : a;
  1881. * });
  1882. *
  1883. * var object = { 'name': 'barney' };
  1884. * defaults(object, { 'name': 'fred', 'employer': 'slate' });
  1885. * // => { 'name': 'barney', 'employer': 'slate' }
  1886. */
  1887. var assign = createIterator(defaultsIteratorOptions, {
  1888. 'top':
  1889. defaultsIteratorOptions.top.replace(';',
  1890. ';\n' +
  1891. "if (argsLength > 3 && typeof args[argsLength - 2] == 'function') {\n" +
  1892. ' var callback = baseCreateCallback(args[--argsLength - 1], args[argsLength--], 2);\n' +
  1893. "} else if (argsLength > 2 && typeof args[argsLength - 1] == 'function') {\n" +
  1894. ' callback = args[--argsLength];\n' +
  1895. '}'
  1896. ),
  1897. 'loop': 'result[index] = callback ? callback(result[index], iterable[index]) : iterable[index]'
  1898. });
  1899. /**
  1900. * Creates a clone of `value`. If `isDeep` is `true` nested objects will also
  1901. * be cloned, otherwise they will be assigned by reference. If a callback
  1902. * is provided it will be executed to produce the cloned values. If the
  1903. * callback returns `undefined` cloning will be handled by the method instead.
  1904. * The callback is bound to `thisArg` and invoked with one argument; (value).
  1905. *
  1906. * @static
  1907. * @memberOf _
  1908. * @category Objects
  1909. * @param {*} value The value to clone.
  1910. * @param {boolean} [isDeep=false] Specify a deep clone.
  1911. * @param {Function} [callback] The function to customize cloning values.
  1912. * @param {*} [thisArg] The `this` binding of `callback`.
  1913. * @returns {*} Returns the cloned value.
  1914. * @example
  1915. *
  1916. * var characters = [
  1917. * { 'name': 'barney', 'age': 36 },
  1918. * { 'name': 'fred', 'age': 40 }
  1919. * ];
  1920. *
  1921. * var shallow = _.clone(characters);
  1922. * shallow[0] === characters[0];
  1923. * // => true
  1924. *
  1925. * var deep = _.clone(characters, true);
  1926. * deep[0] === characters[0];
  1927. * // => false
  1928. *
  1929. * _.mixin({
  1930. * 'clone': _.partialRight(_.clone, function(value) {
  1931. * return _.isElement(value) ? value.cloneNode(false) : undefined;
  1932. * })
  1933. * });
  1934. *
  1935. * var clone = _.clone(document.body);
  1936. * clone.childNodes.length;
  1937. * // => 0
  1938. */
  1939. function clone(value, isDeep, callback, thisArg) {
  1940. // allows working with "Collections" methods without using their `index`
  1941. // and `collection` arguments for `isDeep` and `callback`
  1942. if (typeof isDeep != 'boolean' && isDeep != null) {
  1943. thisArg = callback;
  1944. callback = isDeep;
  1945. isDeep = false;
  1946. }
  1947. return baseClone(value, isDeep, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1));
  1948. }
  1949. /**
  1950. * Creates a deep clone of `value`. If a callback is provided it will be
  1951. * executed to produce the cloned values. If the callback returns `undefined`
  1952. * cloning will be handled by the method instead. The callback is bound to
  1953. * `thisArg` and invoked with one argument; (value).
  1954. *
  1955. * Note: This method is loosely based on the structured clone algorithm. Functions
  1956. * and DOM nodes are **not** cloned. The enumerable properties of `arguments` objects and
  1957. * objects created by constructors other than `Object` are cloned to plain `Object` objects.
  1958. * See http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm.
  1959. *
  1960. * @static
  1961. * @memberOf _
  1962. * @category Objects
  1963. * @param {*} value The value to deep clone.
  1964. * @param {Function} [callback] The function to customize cloning values.
  1965. * @param {*} [thisArg] The `this` binding of `callback`.
  1966. * @returns {*} Returns the deep cloned value.
  1967. * @example
  1968. *
  1969. * var characters = [
  1970. * { 'name': 'barney', 'age': 36 },
  1971. * { 'name': 'fred', 'age': 40 }
  1972. * ];
  1973. *
  1974. * var deep = _.cloneDeep(characters);
  1975. * deep[0] === characters[0];
  1976. * // => false
  1977. *
  1978. * var view = {
  1979. * 'label': 'docs',
  1980. * 'node': element
  1981. * };
  1982. *
  1983. * var clone = _.cloneDeep(view, function(value) {
  1984. * return _.isElement(value) ? value.cloneNode(true) : undefined;
  1985. * });
  1986. *
  1987. * clone.node == view.node;
  1988. * // => false
  1989. */
  1990. function cloneDeep(value, callback, thisArg) {
  1991. return baseClone(value, true, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1));
  1992. }
  1993. /**
  1994. * Creates an object that inherits from the given `prototype` object. If a
  1995. * `properties` object is provided its own enumerable properties are assigned
  1996. * to the created object.
  1997. *
  1998. * @static
  1999. * @memberOf _
  2000. * @category Objects
  2001. * @param {Object} prototype The object to inherit from.
  2002. * @param {Object} [properties] The properties to assign to the object.
  2003. * @returns {Object} Returns the new object.
  2004. * @example
  2005. *
  2006. * function Shape() {
  2007. * this.x = 0;
  2008. * this.y = 0;
  2009. * }
  2010. *
  2011. * function Circle() {
  2012. * Shape.call(this);
  2013. * }
  2014. *
  2015. * Circle.prototype = _.create(Shape.prototype, { 'constructor': Circle });
  2016. *
  2017. * var circle = new Circle;
  2018. * circle instanceof Circle;
  2019. * // => true
  2020. *
  2021. * circle instanceof Shape;
  2022. * // => true
  2023. */
  2024. function create(prototype, properties) {
  2025. var result = baseCreate(prototype);
  2026. return properties ? assign(result, properties) : result;
  2027. }
  2028. /**
  2029. * Assigns own enumerable properties of source object(s) to the destination
  2030. * object for all destination properties that resolve to `undefined`. Once a
  2031. * property is set, additional defaults of the same property will be ignored.
  2032. *
  2033. * @static
  2034. * @memberOf _
  2035. * @type Function
  2036. * @category Objects
  2037. * @param {Object} object The destination object.
  2038. * @param {...Object} [source] The source objects.
  2039. * @param- {Object} [guard] Allows working with `_.reduce` without using its
  2040. * `key` and `object` arguments as sources.
  2041. * @returns {Object} Returns the destination object.
  2042. * @example
  2043. *
  2044. * var object = { 'name': 'barney' };
  2045. * _.defaults(object, { 'name': 'fred', 'employer': 'slate' });
  2046. * // => { 'name': 'barney', 'employer': 'slate' }
  2047. */
  2048. var defaults = createIterator(defaultsIteratorOptions);
  2049. /**
  2050. * This method is like `_.findIndex` except that it returns the key of the
  2051. * first element that passes the callback check, instead of the element itself.
  2052. *
  2053. * If a property name is provided for `callback` the created "_.pluck" style
  2054. * callback will return the property value of the given element.
  2055. *
  2056. * If an object is provided for `callback` the created "_.where" style callback
  2057. * will return `true` for elements that have the properties of the given object,
  2058. * else `false`.
  2059. *
  2060. * @static
  2061. * @memberOf _
  2062. * @category Objects
  2063. * @param {Object} object The object to search.
  2064. * @param {Function|Object|string} [callback=identity] The function called per
  2065. * iteration. If a property name or object is provided it will be used to
  2066. * create a "_.pluck" or "_.where" style callback, respectively.
  2067. * @param {*} [thisArg] The `this` binding of `callback`.
  2068. * @returns {string|undefined} Returns the key of the found element, else `undefined`.
  2069. * @example
  2070. *
  2071. * var characters = {
  2072. * 'barney': { 'age': 36, 'blocked': false },
  2073. * 'fred': { 'age': 40, 'blocked': true },
  2074. * 'pebbles': { 'age': 1, 'blocked': false }
  2075. * };
  2076. *
  2077. * _.findKey(characters, function(chr) {
  2078. * return chr.age < 40;
  2079. * });
  2080. * // => 'barney' (property order is not guaranteed across environments)
  2081. *
  2082. * // using "_.where" callback shorthand
  2083. * _.findKey(characters, { 'age': 1 });
  2084. * // => 'pebbles'
  2085. *
  2086. * // using "_.pluck" callback shorthand
  2087. * _.findKey(characters, 'blocked');
  2088. * // => 'fred'
  2089. */
  2090. function findKey(object, callback, thisArg) {
  2091. var result;
  2092. callback = lodash.createCallback(callback, thisArg, 3);
  2093. forOwn(object, function(value, key, object) {
  2094. if (callback(value, key, object)) {
  2095. result = key;
  2096. return false;
  2097. }
  2098. });
  2099. return result;
  2100. }
  2101. /**
  2102. * This method is like `_.findKey` except that it iterates over elements
  2103. * of a `collection` in the opposite order.
  2104. *
  2105. * If a property name is provided for `callback` the created "_.pluck" style
  2106. * callback will return the property value of the given element.
  2107. *
  2108. * If an object is provided for `callback` the created "_.where" style callback
  2109. * will return `true` for elements that have the properties of the given object,
  2110. * else `false`.
  2111. *
  2112. * @static
  2113. * @memberOf _
  2114. * @category Objects
  2115. * @param {Object} object The object to search.
  2116. * @param {Function|Object|string} [callback=identity] The function called per
  2117. * iteration. If a property name or object is provided it will be used to
  2118. * create a "_.pluck" or "_.where" style callback, respectively.
  2119. * @param {*} [thisArg] The `this` binding of `callback`.
  2120. * @returns {string|undefined} Returns the key of the found element, else `undefined`.
  2121. * @example
  2122. *
  2123. * var characters = {
  2124. * 'barney': { 'age': 36, 'blocked': true },
  2125. * 'fred': { 'age': 40, 'blocked': false },
  2126. * 'pebbles': { 'age': 1, 'blocked': true }
  2127. * };
  2128. *
  2129. * _.findLastKey(characters, function(chr) {
  2130. * return chr.age < 40;
  2131. * });
  2132. * // => returns `pebbles`, assuming `_.findKey` returns `barney`
  2133. *
  2134. * // using "_.where" callback shorthand
  2135. * _.findLastKey(characters, { 'age': 40 });
  2136. * // => 'fred'
  2137. *
  2138. * // using "_.pluck" callback shorthand
  2139. * _.findLastKey(characters, 'blocked');
  2140. * // => 'pebbles'
  2141. */
  2142. function findLastKey(object, callback, thisArg) {
  2143. var result;
  2144. callback = lodash.createCallback(callback, thisArg, 3);
  2145. forOwnRight(object, function(value, key, object) {
  2146. if (callback(value, key, object)) {
  2147. result = key;
  2148. return false;
  2149. }
  2150. });
  2151. return result;
  2152. }
  2153. /**
  2154. * Iterates over own and inherited enumerable properties of an object,
  2155. * executing the callback for each property. The callback is bound to `thisArg`
  2156. * and invoked with three arguments; (value, key, object). Callbacks may exit
  2157. * iteration early by explicitly returning `false`.
  2158. *
  2159. * @static
  2160. * @memberOf _
  2161. * @type Function
  2162. * @category Objects
  2163. * @param {Object} object The object to iterate over.
  2164. * @param {Function} [callback=identity] The function called per iteration.
  2165. * @param {*} [thisArg] The `this` binding of `callback`.
  2166. * @returns {Object} Returns `object`.
  2167. * @example
  2168. *
  2169. * function Shape() {
  2170. * this.x = 0;
  2171. * this.y = 0;
  2172. * }
  2173. *
  2174. * Shape.prototype.move = function(x, y) {
  2175. * this.x += x;
  2176. * this.y += y;
  2177. * };
  2178. *
  2179. * _.forIn(new Shape, function(value, key) {
  2180. * console.log(key);
  2181. * });
  2182. * // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments)
  2183. */
  2184. var forIn = createIterator(eachIteratorOptions, forOwnIteratorOptions, {
  2185. 'useHas': false
  2186. });
  2187. /**
  2188. * This method is like `_.forIn` except that it iterates over elements
  2189. * of a `collection` in the opposite order.
  2190. *
  2191. * @static
  2192. * @memberOf _
  2193. * @category Objects
  2194. * @param {Object} object The object to iterate over.
  2195. * @param {Function} [callback=identity] The function called per iteration.
  2196. * @param {*} [thisArg] The `this` binding of `callback`.
  2197. * @returns {Object} Returns `object`.
  2198. * @example
  2199. *
  2200. * function Shape() {
  2201. * this.x = 0;
  2202. * this.y = 0;
  2203. * }
  2204. *
  2205. * Shape.prototype.move = function(x, y) {
  2206. * this.x += x;
  2207. * this.y += y;
  2208. * };
  2209. *
  2210. * _.forInRight(new Shape, function(value, key) {
  2211. * console.log(key);
  2212. * });
  2213. * // => logs 'move', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'move'
  2214. */
  2215. function forInRight(object, callback, thisArg) {
  2216. var pairs = [];
  2217. forIn(object, function(value, key) {
  2218. pairs.push(key, value);
  2219. });
  2220. var length = pairs.length;
  2221. callback = baseCreateCallback(callback, thisArg, 3);
  2222. while (length--) {
  2223. if (callback(pairs[length--], pairs[length], object) === false) {
  2224. break;
  2225. }
  2226. }
  2227. return object;
  2228. }
  2229. /**
  2230. * Iterates over own enumerable properties of an object, executing the callback
  2231. * for each property. The callback is bound to `thisArg` and invoked with three
  2232. * arguments; (value, key, object). Callbacks may exit iteration early by
  2233. * explicitly returning `false`.
  2234. *
  2235. * @static
  2236. * @memberOf _
  2237. * @type Function
  2238. * @category Objects
  2239. * @param {Object} object The object to iterate over.
  2240. * @param {Function} [callback=identity] The function called per iteration.
  2241. * @param {*} [thisArg] The `this` binding of `callback`.
  2242. * @returns {Object} Returns `object`.
  2243. * @example
  2244. *
  2245. * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  2246. * console.log(key);
  2247. * });
  2248. * // => logs '0', '1', and 'length' (property order is not guaranteed across environments)
  2249. */
  2250. var forOwn = createIterator(eachIteratorOptions, forOwnIteratorOptions);
  2251. /**
  2252. * This method is like `_.forOwn` except that it iterates over elements
  2253. * of a `collection` in the opposite order.
  2254. *
  2255. * @static
  2256. * @memberOf _
  2257. * @category Objects
  2258. * @param {Object} object The object to iterate over.
  2259. * @param {Function} [callback=identity] The function called per iteration.
  2260. * @param {*} [thisArg] The `this` binding of `callback`.
  2261. * @returns {Object} Returns `object`.
  2262. * @example
  2263. *
  2264. * _.forOwnRight({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  2265. * console.log(key);
  2266. * });
  2267. * // => logs 'length', '1', and '0' assuming `_.forOwn` logs '0', '1', and 'length'
  2268. */
  2269. function forOwnRight(object, callback, thisArg) {
  2270. var props = keys(object),
  2271. length = props.length;
  2272. callback = baseCreateCallback(callback, thisArg, 3);
  2273. while (length--) {
  2274. var key = props[length];
  2275. if (callback(object[key], key, object) === false) {
  2276. break;
  2277. }
  2278. }
  2279. return object;
  2280. }
  2281. /**
  2282. * Creates a sorted array of property names of all enumerable properties,
  2283. * own and inherited, of `object` that have function values.
  2284. *
  2285. * @static
  2286. * @memberOf _
  2287. * @alias methods
  2288. * @category Objects
  2289. * @param {Object} object The object to inspect.
  2290. * @returns {Array} Returns an array of property names that have function values.
  2291. * @example
  2292. *
  2293. * _.functions(_);
  2294. * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
  2295. */
  2296. function functions(object) {
  2297. var result = [];
  2298. forIn(object, function(value, key) {
  2299. if (isFunction(value)) {
  2300. result.push(key);
  2301. }
  2302. });
  2303. return result.sort();
  2304. }
  2305. /**
  2306. * Checks if the specified property name exists as a direct property of `object`,
  2307. * instead of an inherited property.
  2308. *
  2309. * @static
  2310. * @memberOf _
  2311. * @category Objects
  2312. * @param {Object} object The object to inspect.
  2313. * @param {string} key The name of the property to check.
  2314. * @returns {boolean} Returns `true` if key is a direct property, else `false`.
  2315. * @example
  2316. *
  2317. * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
  2318. * // => true
  2319. */
  2320. function has(object, key) {
  2321. return object ? hasOwnProperty.call(object, key) : false;
  2322. }
  2323. /**
  2324. * Creates an object composed of the inverted keys and values of the given object.
  2325. *
  2326. * @static
  2327. * @memberOf _
  2328. * @category Objects
  2329. * @param {Object} object The object to invert.
  2330. * @returns {Object} Returns the created inverted object.
  2331. * @example
  2332. *
  2333. * _.invert({ 'first': 'fred', 'second': 'barney' });
  2334. * // => { 'fred': 'first', 'barney': 'second' }
  2335. */
  2336. function invert(object) {
  2337. var index = -1,
  2338. props = keys(object),
  2339. length = props.length,
  2340. result = {};
  2341. while (++index < length) {
  2342. var key = props[index];
  2343. result[object[key]] = key;
  2344. }
  2345. return result;
  2346. }
  2347. /**
  2348. * Checks if `value` is a boolean value.
  2349. *
  2350. * @static
  2351. * @memberOf _
  2352. * @category Objects
  2353. * @param {*} value The value to check.
  2354. * @returns {boolean} Returns `true` if the `value` is a boolean value, else `false`.
  2355. * @example
  2356. *
  2357. * _.isBoolean(null);
  2358. * // => false
  2359. */
  2360. function isBoolean(value) {
  2361. return value === true || value === false ||
  2362. value && typeof value == 'object' && toString.call(value) == boolClass || false;
  2363. }
  2364. /**
  2365. * Checks if `value` is a date.
  2366. *
  2367. * @static
  2368. * @memberOf _
  2369. * @category Objects
  2370. * @param {*} value The value to check.
  2371. * @returns {boolean} Returns `true` if the `value` is a date, else `false`.
  2372. * @example
  2373. *
  2374. * _.isDate(new Date);
  2375. * // => true
  2376. */
  2377. function isDate(value) {
  2378. return value && typeof value == 'object' && toString.call(value) == dateClass || false;
  2379. }
  2380. /**
  2381. * Checks if `value` is a DOM element.
  2382. *
  2383. * @static
  2384. * @memberOf _
  2385. * @category Objects
  2386. * @param {*} value The value to check.
  2387. * @returns {boolean} Returns `true` if the `value` is a DOM element, else `false`.
  2388. * @example
  2389. *
  2390. * _.isElement(document.body);
  2391. * // => true
  2392. */
  2393. function isElement(value) {
  2394. return value && value.nodeType === 1 || false;
  2395. }
  2396. /**
  2397. * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
  2398. * length of `0` and objects with no own enumerable properties are considered
  2399. * "empty".
  2400. *
  2401. * @static
  2402. * @memberOf _
  2403. * @category Objects
  2404. * @param {Array|Object|string} value The value to inspect.
  2405. * @returns {boolean} Returns `true` if the `value` is empty, else `false`.
  2406. * @example
  2407. *
  2408. * _.isEmpty([1, 2, 3]);
  2409. * // => false
  2410. *
  2411. * _.isEmpty({});
  2412. * // => true
  2413. *
  2414. * _.isEmpty('');
  2415. * // => true
  2416. */
  2417. function isEmpty(value) {
  2418. var result = true;
  2419. if (!value) {
  2420. return result;
  2421. }
  2422. var className = toString.call(value),
  2423. length = value.length;
  2424. if ((className == arrayClass || className == stringClass ||
  2425. (support.argsClass ? className == argsClass : isArguments(value))) ||
  2426. (className == objectClass && typeof length == 'number' && isFunction(value.splice))) {
  2427. return !length;
  2428. }
  2429. forOwn(value, function() {
  2430. return (result = false);
  2431. });
  2432. return result;
  2433. }
  2434. /**
  2435. * Performs a deep comparison between two values to determine if they are
  2436. * equivalent to each other. If a callback is provided it will be executed
  2437. * to compare values. If the callback returns `undefined` comparisons will
  2438. * be handled by the method instead. The callback is bound to `thisArg` and
  2439. * invoked with two arguments; (a, b).
  2440. *
  2441. * @static
  2442. * @memberOf _
  2443. * @category Objects
  2444. * @param {*} a The value to compare.
  2445. * @param {*} b The other value to compare.
  2446. * @param {Function} [callback] The function to customize comparing values.
  2447. * @param {*} [thisArg] The `this` binding of `callback`.
  2448. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  2449. * @example
  2450. *
  2451. * var object = { 'name': 'fred' };
  2452. * var copy = { 'name': 'fred' };
  2453. *
  2454. * object == copy;
  2455. * // => false
  2456. *
  2457. * _.isEqual(object, copy);
  2458. * // => true
  2459. *
  2460. * var words = ['hello', 'goodbye'];
  2461. * var otherWords = ['hi', 'goodbye'];
  2462. *
  2463. * _.isEqual(words, otherWords, function(a, b) {
  2464. * var reGreet = /^(?:hello|hi)$/i,
  2465. * aGreet = _.isString(a) && reGreet.test(a),
  2466. * bGreet = _.isString(b) && reGreet.test(b);
  2467. *
  2468. * return (aGreet || bGreet) ? (aGreet == bGreet) : undefined;
  2469. * });
  2470. * // => true
  2471. */
  2472. function isEqual(a, b, callback, thisArg) {
  2473. return baseIsEqual(a, b, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 2));
  2474. }
  2475. /**
  2476. * Checks if `value` is, or can be coerced to, a finite number.
  2477. *
  2478. * Note: This is not the same as native `isFinite` which will return true for
  2479. * booleans and empty strings. See http://es5.github.io/#x15.1.2.5.
  2480. *
  2481. * @static
  2482. * @memberOf _
  2483. * @category Objects
  2484. * @param {*} value The value to check.
  2485. * @returns {boolean} Returns `true` if the `value` is finite, else `false`.
  2486. * @example
  2487. *
  2488. * _.isFinite(-101);
  2489. * // => true
  2490. *
  2491. * _.isFinite('10');
  2492. * // => true
  2493. *
  2494. * _.isFinite(true);
  2495. * // => false
  2496. *
  2497. * _.isFinite('');
  2498. * // => false
  2499. *
  2500. * _.isFinite(Infinity);
  2501. * // => false
  2502. */
  2503. function isFinite(value) {
  2504. return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value));
  2505. }
  2506. /**
  2507. * Checks if `value` is a function.
  2508. *
  2509. * @static
  2510. * @memberOf _
  2511. * @category Objects
  2512. * @param {*} value The value to check.
  2513. * @returns {boolean} Returns `true` if the `value` is a function, else `false`.
  2514. * @example
  2515. *
  2516. * _.isFunction(_);
  2517. * // => true
  2518. */
  2519. function isFunction(value) {
  2520. return typeof value == 'function';
  2521. }
  2522. // fallback for older versions of Chrome and Safari
  2523. if (isFunction(/x/)) {
  2524. isFunction = function(value) {
  2525. return typeof value == 'function' && toString.call(value) == funcClass;
  2526. };
  2527. }
  2528. /**
  2529. * Checks if `value` is the language type of Object.
  2530. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  2531. *
  2532. * @static
  2533. * @memberOf _
  2534. * @category Objects
  2535. * @param {*} value The value to check.
  2536. * @returns {boolean} Returns `true` if the `value` is an object, else `false`.
  2537. * @example
  2538. *
  2539. * _.isObject({});
  2540. * // => true
  2541. *
  2542. * _.isObject([1, 2, 3]);
  2543. * // => true
  2544. *
  2545. * _.isObject(1);
  2546. * // => false
  2547. */
  2548. function isObject(value) {
  2549. // check if the value is the ECMAScript language type of Object
  2550. // http://es5.github.io/#x8
  2551. // and avoid a V8 bug
  2552. // http://code.google.com/p/v8/issues/detail?id=2291
  2553. return !!(value && objectTypes[typeof value]);
  2554. }
  2555. /**
  2556. * Checks if `value` is `NaN`.
  2557. *
  2558. * Note: This is not the same as native `isNaN` which will return `true` for
  2559. * `undefined` and other non-numeric values. See http://es5.github.io/#x15.1.2.4.
  2560. *
  2561. * @static
  2562. * @memberOf _
  2563. * @category Objects
  2564. * @param {*} value The value to check.
  2565. * @returns {boolean} Returns `true` if the `value` is `NaN`, else `false`.
  2566. * @example
  2567. *
  2568. * _.isNaN(NaN);
  2569. * // => true
  2570. *
  2571. * _.isNaN(new Number(NaN));
  2572. * // => true
  2573. *
  2574. * isNaN(undefined);
  2575. * // => true
  2576. *
  2577. * _.isNaN(undefined);
  2578. * // => false
  2579. */
  2580. function isNaN(value) {
  2581. // `NaN` as a primitive is the only value that is not equal to itself
  2582. // (perform the [[Class]] check first to avoid errors with some host objects in IE)
  2583. return isNumber(value) && value != +value;
  2584. }
  2585. /**
  2586. * Checks if `value` is `null`.
  2587. *
  2588. * @static
  2589. * @memberOf _
  2590. * @category Objects
  2591. * @param {*} value The value to check.
  2592. * @returns {boolean} Returns `true` if the `value` is `null`, else `false`.
  2593. * @example
  2594. *
  2595. * _.isNull(null);
  2596. * // => true
  2597. *
  2598. * _.isNull(undefined);
  2599. * // => false
  2600. */
  2601. function isNull(value) {
  2602. return value === null;
  2603. }
  2604. /**
  2605. * Checks if `value` is a number.
  2606. *
  2607. * Note: `NaN` is considered a number. See http://es5.github.io/#x8.5.
  2608. *
  2609. * @static
  2610. * @memberOf _
  2611. * @category Objects
  2612. * @param {*} value The value to check.
  2613. * @returns {boolean} Returns `true` if the `value` is a number, else `false`.
  2614. * @example
  2615. *
  2616. * _.isNumber(8.4 * 5);
  2617. * // => true
  2618. */
  2619. function isNumber(value) {
  2620. return typeof value == 'number' ||
  2621. value && typeof value == 'object' && toString.call(value) == numberClass || false;
  2622. }
  2623. /**
  2624. * Checks if `value` is an object created by the `Object` constructor.
  2625. *
  2626. * @static
  2627. * @memberOf _
  2628. * @category Objects
  2629. * @param {*} value The value to check.
  2630. * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
  2631. * @example
  2632. *
  2633. * function Shape() {
  2634. * this.x = 0;
  2635. * this.y = 0;
  2636. * }
  2637. *
  2638. * _.isPlainObject(new Shape);
  2639. * // => false
  2640. *
  2641. * _.isPlainObject([1, 2, 3]);
  2642. * // => false
  2643. *
  2644. * _.isPlainObject({ 'x': 0, 'y': 0 });
  2645. * // => true
  2646. */
  2647. var isPlainObject = !getPrototypeOf ? shimIsPlainObject : function(value) {
  2648. if (!(value && toString.call(value) == objectClass) || (!support.argsClass && isArguments(value))) {
  2649. return false;
  2650. }
  2651. var valueOf = value.valueOf,
  2652. objProto = isNative(valueOf) && (objProto = getPrototypeOf(valueOf)) && getPrototypeOf(objProto);
  2653. return objProto
  2654. ? (value == objProto || getPrototypeOf(value) == objProto)
  2655. : shimIsPlainObject(value);
  2656. };
  2657. /**
  2658. * Checks if `value` is a regular expression.
  2659. *
  2660. * @static
  2661. * @memberOf _
  2662. * @category Objects
  2663. * @param {*} value The value to check.
  2664. * @returns {boolean} Returns `true` if the `value` is a regular expression, else `false`.
  2665. * @example
  2666. *
  2667. * _.isRegExp(/fred/);
  2668. * // => true
  2669. */
  2670. function isRegExp(value) {
  2671. return value && objectTypes[typeof value] && toString.call(value) == regexpClass || false;
  2672. }
  2673. /**
  2674. * Checks if `value` is a string.
  2675. *
  2676. * @static
  2677. * @memberOf _
  2678. * @category Objects
  2679. * @param {*} value The value to check.
  2680. * @returns {boolean} Returns `true` if the `value` is a string, else `false`.
  2681. * @example
  2682. *
  2683. * _.isString('fred');
  2684. * // => true
  2685. */
  2686. function isString(value) {
  2687. return typeof value == 'string' ||
  2688. value && typeof value == 'object' && toString.call(value) == stringClass || false;
  2689. }
  2690. /**
  2691. * Checks if `value` is `undefined`.
  2692. *
  2693. * @static
  2694. * @memberOf _
  2695. * @category Objects
  2696. * @param {*} value The value to check.
  2697. * @returns {boolean} Returns `true` if the `value` is `undefined`, else `false`.
  2698. * @example
  2699. *
  2700. * _.isUndefined(void 0);
  2701. * // => true
  2702. */
  2703. function isUndefined(value) {
  2704. return typeof value == 'undefined';
  2705. }
  2706. /**
  2707. * Creates an object with the same keys as `object` and values generated by
  2708. * running each own enumerable property of `object` through the callback.
  2709. * The callback is bound to `thisArg` and invoked with three arguments;
  2710. * (value, key, object).
  2711. *
  2712. * If a property name is provided for `callback` the created "_.pluck" style
  2713. * callback will return the property value of the given element.
  2714. *
  2715. * If an object is provided for `callback` the created "_.where" style callback
  2716. * will return `true` for elements that have the properties of the given object,
  2717. * else `false`.
  2718. *
  2719. * @static
  2720. * @memberOf _
  2721. * @category Objects
  2722. * @param {Object} object The object to iterate over.
  2723. * @param {Function|Object|string} [callback=identity] The function called
  2724. * per iteration. If a property name or object is provided it will be used
  2725. * to create a "_.pluck" or "_.where" style callback, respectively.
  2726. * @param {*} [thisArg] The `this` binding of `callback`.
  2727. * @returns {Array} Returns a new object with values of the results of each `callback` execution.
  2728. * @example
  2729. *
  2730. * _.mapValues({ 'a': 1, 'b': 2, 'c': 3} , function(num) { return num * 3; });
  2731. * // => { 'a': 3, 'b': 6, 'c': 9 }
  2732. *
  2733. * var characters = {
  2734. * 'fred': { 'name': 'fred', 'age': 40 },
  2735. * 'pebbles': { 'name': 'pebbles', 'age': 1 }
  2736. * };
  2737. *
  2738. * // using "_.pluck" callback shorthand
  2739. * _.mapValues(characters, 'age');
  2740. * // => { 'fred': 40, 'pebbles': 1 }
  2741. */
  2742. function mapValues(object, callback, thisArg) {
  2743. var result = {};
  2744. callback = lodash.createCallback(callback, thisArg, 3);
  2745. forOwn(object, function(value, key, object) {
  2746. result[key] = callback(value, key, object);
  2747. });
  2748. return result;
  2749. }
  2750. /**
  2751. * Recursively merges own enumerable properties of the source object(s), that
  2752. * don't resolve to `undefined` into the destination object. Subsequent sources
  2753. * will overwrite property assignments of previous sources. If a callback is
  2754. * provided it will be executed to produce the merged values of the destination
  2755. * and source properties. If the callback returns `undefined` merging will
  2756. * be handled by the method instead. The callback is bound to `thisArg` and
  2757. * invoked with two arguments; (objectValue, sourceValue).
  2758. *
  2759. * @static
  2760. * @memberOf _
  2761. * @category Objects
  2762. * @param {Object} object The destination object.
  2763. * @param {...Object} [source] The source objects.
  2764. * @param {Function} [callback] The function to customize merging properties.
  2765. * @param {*} [thisArg] The `this` binding of `callback`.
  2766. * @returns {Object} Returns the destination object.
  2767. * @example
  2768. *
  2769. * var names = {
  2770. * 'characters': [
  2771. * { 'name': 'barney' },
  2772. * { 'name': 'fred' }
  2773. * ]
  2774. * };
  2775. *
  2776. * var ages = {
  2777. * 'characters': [
  2778. * { 'age': 36 },
  2779. * { 'age': 40 }
  2780. * ]
  2781. * };
  2782. *
  2783. * _.merge(names, ages);
  2784. * // => { 'characters': [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }] }
  2785. *
  2786. * var food = {
  2787. * 'fruits': ['apple'],
  2788. * 'vegetables': ['beet']
  2789. * };
  2790. *
  2791. * var otherFood = {
  2792. * 'fruits': ['banana'],
  2793. * 'vegetables': ['carrot']
  2794. * };
  2795. *
  2796. * _.merge(food, otherFood, function(a, b) {
  2797. * return _.isArray(a) ? a.concat(b) : undefined;
  2798. * });
  2799. * // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot] }
  2800. */
  2801. function merge(object) {
  2802. var args = arguments,
  2803. length = 2;
  2804. if (!isObject(object)) {
  2805. return object;
  2806. }
  2807. // allows working with `_.reduce` and `_.reduceRight` without using
  2808. // their `index` and `collection` arguments
  2809. if (typeof args[2] != 'number') {
  2810. length = args.length;
  2811. }
  2812. if (length > 3 && typeof args[length - 2] == 'function') {
  2813. var callback = baseCreateCallback(args[--length - 1], args[length--], 2);
  2814. } else if (length > 2 && typeof args[length - 1] == 'function') {
  2815. callback = args[--length];
  2816. }
  2817. var sources = slice(arguments, 1, length),
  2818. index = -1,
  2819. stackA = getArray(),
  2820. stackB = getArray();
  2821. while (++index < length) {
  2822. baseMerge(object, sources[index], callback, stackA, stackB);
  2823. }
  2824. releaseArray(stackA);
  2825. releaseArray(stackB);
  2826. return object;
  2827. }
  2828. /**
  2829. * Creates a shallow clone of `object` excluding the specified properties.
  2830. * Property names may be specified as individual arguments or as arrays of
  2831. * property names. If a callback is provided it will be executed for each
  2832. * property of `object` omitting the properties the callback returns truey
  2833. * for. The callback is bound to `thisArg` and invoked with three arguments;
  2834. * (value, key, object).
  2835. *
  2836. * @static
  2837. * @memberOf _
  2838. * @category Objects
  2839. * @param {Object} object The source object.
  2840. * @param {Function|...string|string[]} [callback] The properties to omit or the
  2841. * function called per iteration.
  2842. * @param {*} [thisArg] The `this` binding of `callback`.
  2843. * @returns {Object} Returns an object without the omitted properties.
  2844. * @example
  2845. *
  2846. * _.omit({ 'name': 'fred', 'age': 40 }, 'age');
  2847. * // => { 'name': 'fred' }
  2848. *
  2849. * _.omit({ 'name': 'fred', 'age': 40 }, function(value) {
  2850. * return typeof value == 'number';
  2851. * });
  2852. * // => { 'name': 'fred' }
  2853. */
  2854. function omit(object, callback, thisArg) {
  2855. var result = {};
  2856. if (typeof callback != 'function') {
  2857. var props = [];
  2858. forIn(object, function(value, key) {
  2859. props.push(key);
  2860. });
  2861. props = baseDifference(props, baseFlatten(arguments, true, false, 1));
  2862. var index = -1,
  2863. length = props.length;
  2864. while (++index < length) {
  2865. var key = props[index];
  2866. result[key] = object[key];
  2867. }
  2868. } else {
  2869. callback = lodash.createCallback(callback, thisArg, 3);
  2870. forIn(object, function(value, key, object) {
  2871. if (!callback(value, key, object)) {
  2872. result[key] = value;
  2873. }
  2874. });
  2875. }
  2876. return result;
  2877. }
  2878. /**
  2879. * Creates a two dimensional array of an object's key-value pairs,
  2880. * i.e. `[[key1, value1], [key2, value2]]`.
  2881. *
  2882. * @static
  2883. * @memberOf _
  2884. * @category Objects
  2885. * @param {Object} object The object to inspect.
  2886. * @returns {Array} Returns new array of key-value pairs.
  2887. * @example
  2888. *
  2889. * _.pairs({ 'barney': 36, 'fred': 40 });
  2890. * // => [['barney', 36], ['fred', 40]] (property order is not guaranteed across environments)
  2891. */
  2892. function pairs(object) {
  2893. var index = -1,
  2894. props = keys(object),
  2895. length = props.length,
  2896. result = Array(length);
  2897. while (++index < length) {
  2898. var key = props[index];
  2899. result[index] = [key, object[key]];
  2900. }
  2901. return result;
  2902. }
  2903. /**
  2904. * Creates a shallow clone of `object` composed of the specified properties.
  2905. * Property names may be specified as individual arguments or as arrays of
  2906. * property names. If a callback is provided it will be executed for each
  2907. * property of `object` picking the properties the callback returns truey
  2908. * for. The callback is bound to `thisArg` and invoked with three arguments;
  2909. * (value, key, object).
  2910. *
  2911. * @static
  2912. * @memberOf _
  2913. * @category Objects
  2914. * @param {Object} object The source object.
  2915. * @param {Function|...string|string[]} [callback] The function called per
  2916. * iteration or property names to pick, specified as individual property
  2917. * names or arrays of property names.
  2918. * @param {*} [thisArg] The `this` binding of `callback`.
  2919. * @returns {Object} Returns an object composed of the picked properties.
  2920. * @example
  2921. *
  2922. * _.pick({ 'name': 'fred', '_userid': 'fred1' }, 'name');
  2923. * // => { 'name': 'fred' }
  2924. *
  2925. * _.pick({ 'name': 'fred', '_userid': 'fred1' }, function(value, key) {
  2926. * return key.charAt(0) != '_';
  2927. * });
  2928. * // => { 'name': 'fred' }
  2929. */
  2930. function pick(object, callback, thisArg) {
  2931. var result = {};
  2932. if (typeof callback != 'function') {
  2933. var index = -1,
  2934. props = baseFlatten(arguments, true, false, 1),
  2935. length = isObject(object) ? props.length : 0;
  2936. while (++index < length) {
  2937. var key = props[index];
  2938. if (key in object) {
  2939. result[key] = object[key];
  2940. }
  2941. }
  2942. } else {
  2943. callback = lodash.createCallback(callback, thisArg, 3);
  2944. forIn(object, function(value, key, object) {
  2945. if (callback(value, key, object)) {
  2946. result[key] = value;
  2947. }
  2948. });
  2949. }
  2950. return result;
  2951. }
  2952. /**
  2953. * An alternative to `_.reduce` this method transforms `object` to a new
  2954. * `accumulator` object which is the result of running each of its own
  2955. * enumerable properties through a callback, with each callback execution
  2956. * potentially mutating the `accumulator` object. The callback is bound to
  2957. * `thisArg` and invoked with four arguments; (accumulator, value, key, object).
  2958. * Callbacks may exit iteration early by explicitly returning `false`.
  2959. *
  2960. * @static
  2961. * @memberOf _
  2962. * @category Objects
  2963. * @param {Array|Object} object The object to iterate over.
  2964. * @param {Function} [callback=identity] The function called per iteration.
  2965. * @param {*} [accumulator] The custom accumulator value.
  2966. * @param {*} [thisArg] The `this` binding of `callback`.
  2967. * @returns {*} Returns the accumulated value.
  2968. * @example
  2969. *
  2970. * var squares = _.transform([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(result, num) {
  2971. * num *= num;
  2972. * if (num % 2) {
  2973. * return result.push(num) < 3;
  2974. * }
  2975. * });
  2976. * // => [1, 9, 25]
  2977. *
  2978. * var mapped = _.transform({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
  2979. * result[key] = num * 3;
  2980. * });
  2981. * // => { 'a': 3, 'b': 6, 'c': 9 }
  2982. */
  2983. function transform(object, callback, accumulator, thisArg) {
  2984. var isArr = isArray(object);
  2985. if (accumulator == null) {
  2986. if (isArr) {
  2987. accumulator = [];
  2988. } else {
  2989. var ctor = object && object.constructor,
  2990. proto = ctor && ctor.prototype;
  2991. accumulator = baseCreate(proto);
  2992. }
  2993. }
  2994. if (callback) {
  2995. callback = lodash.createCallback(callback, thisArg, 4);
  2996. (isArr ? baseEach : forOwn)(object, function(value, index, object) {
  2997. return callback(accumulator, value, index, object);
  2998. });
  2999. }
  3000. return accumulator;
  3001. }
  3002. /**
  3003. * Creates an array composed of the own enumerable property values of `object`.
  3004. *
  3005. * @static
  3006. * @memberOf _
  3007. * @category Objects
  3008. * @param {Object} object The object to inspect.
  3009. * @returns {Array} Returns an array of property values.
  3010. * @example
  3011. *
  3012. * _.values({ 'one': 1, 'two': 2, 'three': 3 });
  3013. * // => [1, 2, 3] (property order is not guaranteed across environments)
  3014. */
  3015. function values(object) {
  3016. var index = -1,
  3017. props = keys(object),
  3018. length = props.length,
  3019. result = Array(length);
  3020. while (++index < length) {
  3021. result[index] = object[props[index]];
  3022. }
  3023. return result;
  3024. }
  3025. /*--------------------------------------------------------------------------*/
  3026. /**
  3027. * Creates an array of elements from the specified indexes, or keys, of the
  3028. * `collection`. Indexes may be specified as individual arguments or as arrays
  3029. * of indexes.
  3030. *
  3031. * @static
  3032. * @memberOf _
  3033. * @category Collections
  3034. * @param {Array|Object|string} collection The collection to iterate over.
  3035. * @param {...(number|number[]|string|string[])} [index] The indexes of `collection`
  3036. * to retrieve, specified as individual indexes or arrays of indexes.
  3037. * @returns {Array} Returns a new array of elements corresponding to the
  3038. * provided indexes.
  3039. * @example
  3040. *
  3041. * _.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]);
  3042. * // => ['a', 'c', 'e']
  3043. *
  3044. * _.at(['fred', 'barney', 'pebbles'], 0, 2);
  3045. * // => ['fred', 'pebbles']
  3046. */
  3047. function at(collection) {
  3048. var args = arguments,
  3049. index = -1,
  3050. props = baseFlatten(args, true, false, 1),
  3051. length = (args[2] && args[2][args[1]] === collection) ? 1 : props.length,
  3052. result = Array(length);
  3053. if (support.unindexedChars && isString(collection)) {
  3054. collection = collection.split('');
  3055. }
  3056. while(++index < length) {
  3057. result[index] = collection[props[index]];
  3058. }
  3059. return result;
  3060. }
  3061. /**
  3062. * Checks if a given value is present in a collection using strict equality
  3063. * for comparisons, i.e. `===`. If `fromIndex` is negative, it is used as the
  3064. * offset from the end of the collection.
  3065. *
  3066. * @static
  3067. * @memberOf _
  3068. * @alias include
  3069. * @category Collections
  3070. * @param {Array|Object|string} collection The collection to iterate over.
  3071. * @param {*} target The value to check for.
  3072. * @param {number} [fromIndex=0] The index to search from.
  3073. * @returns {boolean} Returns `true` if the `target` element is found, else `false`.
  3074. * @example
  3075. *
  3076. * _.contains([1, 2, 3], 1);
  3077. * // => true
  3078. *
  3079. * _.contains([1, 2, 3], 1, 2);
  3080. * // => false
  3081. *
  3082. * _.contains({ 'name': 'fred', 'age': 40 }, 'fred');
  3083. * // => true
  3084. *
  3085. * _.contains('pebbles', 'eb');
  3086. * // => true
  3087. */
  3088. function contains(collection, target, fromIndex) {
  3089. var index = -1,
  3090. indexOf = getIndexOf(),
  3091. length = collection ? collection.length : 0,
  3092. result = false;
  3093. fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) || 0;
  3094. if (isArray(collection)) {
  3095. result = indexOf(collection, target, fromIndex) > -1;
  3096. } else if (typeof length == 'number') {
  3097. result = (isString(collection) ? collection.indexOf(target, fromIndex) : indexOf(collection, target, fromIndex)) > -1;
  3098. } else {
  3099. baseEach(collection, function(value) {
  3100. if (++index >= fromIndex) {
  3101. return !(result = value === target);
  3102. }
  3103. });
  3104. }
  3105. return result;
  3106. }
  3107. /**
  3108. * Creates an object composed of keys generated from the results of running
  3109. * each element of `collection` through the callback. The corresponding value
  3110. * of each key is the number of times the key was returned by the callback.
  3111. * The callback is bound to `thisArg` and invoked with three arguments;
  3112. * (value, index|key, collection).
  3113. *
  3114. * If a property name is provided for `callback` the created "_.pluck" style
  3115. * callback will return the property value of the given element.
  3116. *
  3117. * If an object is provided for `callback` the created "_.where" style callback
  3118. * will return `true` for elements that have the properties of the given object,
  3119. * else `false`.
  3120. *
  3121. * @static
  3122. * @memberOf _
  3123. * @category Collections
  3124. * @param {Array|Object|string} collection The collection to iterate over.
  3125. * @param {Function|Object|string} [callback=identity] The function called
  3126. * per iteration. If a property name or object is provided it will be used
  3127. * to create a "_.pluck" or "_.where" style callback, respectively.
  3128. * @param {*} [thisArg] The `this` binding of `callback`.
  3129. * @returns {Object} Returns the composed aggregate object.
  3130. * @example
  3131. *
  3132. * _.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); });
  3133. * // => { '4': 1, '6': 2 }
  3134. *
  3135. * _.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
  3136. * // => { '4': 1, '6': 2 }
  3137. *
  3138. * _.countBy(['one', 'two', 'three'], 'length');
  3139. * // => { '3': 2, '5': 1 }
  3140. */
  3141. var countBy = createAggregator(function(result, value, key) {
  3142. (hasOwnProperty.call(result, key) ? result[key]++ : result[key] = 1);
  3143. });
  3144. /**
  3145. * Checks if the given callback returns truey value for **all** elements of
  3146. * a collection. The callback is bound to `thisArg` and invoked with three
  3147. * arguments; (value, index|key, collection).
  3148. *
  3149. * If a property name is provided for `callback` the created "_.pluck" style
  3150. * callback will return the property value of the given element.
  3151. *
  3152. * If an object is provided for `callback` the created "_.where" style callback
  3153. * will return `true` for elements that have the properties of the given object,
  3154. * else `false`.
  3155. *
  3156. * @static
  3157. * @memberOf _
  3158. * @alias all
  3159. * @category Collections
  3160. * @param {Array|Object|string} collection The collection to iterate over.
  3161. * @param {Function|Object|string} [callback=identity] The function called
  3162. * per iteration. If a property name or object is provided it will be used
  3163. * to create a "_.pluck" or "_.where" style callback, respectively.
  3164. * @param {*} [thisArg] The `this` binding of `callback`.
  3165. * @returns {boolean} Returns `true` if all elements passed the callback check,
  3166. * else `false`.
  3167. * @example
  3168. *
  3169. * _.every([true, 1, null, 'yes']);
  3170. * // => false
  3171. *
  3172. * var characters = [
  3173. * { 'name': 'barney', 'age': 36 },
  3174. * { 'name': 'fred', 'age': 40 }
  3175. * ];
  3176. *
  3177. * // using "_.pluck" callback shorthand
  3178. * _.every(characters, 'age');
  3179. * // => true
  3180. *
  3181. * // using "_.where" callback shorthand
  3182. * _.every(characters, { 'age': 36 });
  3183. * // => false
  3184. */
  3185. function every(collection, callback, thisArg) {
  3186. var result = true;
  3187. callback = lodash.createCallback(callback, thisArg, 3);
  3188. if (isArray(collection)) {
  3189. var index = -1,
  3190. length = collection.length;
  3191. while (++index < length) {
  3192. if (!(result = !!callback(collection[index], index, collection))) {
  3193. break;
  3194. }
  3195. }
  3196. } else {
  3197. baseEach(collection, function(value, index, collection) {
  3198. return (result = !!callback(value, index, collection));
  3199. });
  3200. }
  3201. return result;
  3202. }
  3203. /**
  3204. * Iterates over elements of a collection, returning an array of all elements
  3205. * the callback returns truey for. The callback is bound to `thisArg` and
  3206. * invoked with three arguments; (value, index|key, collection).
  3207. *
  3208. * If a property name is provided for `callback` the created "_.pluck" style
  3209. * callback will return the property value of the given element.
  3210. *
  3211. * If an object is provided for `callback` the created "_.where" style callback
  3212. * will return `true` for elements that have the properties of the given object,
  3213. * else `false`.
  3214. *
  3215. * @static
  3216. * @memberOf _
  3217. * @alias select
  3218. * @category Collections
  3219. * @param {Array|Object|string} collection The collection to iterate over.
  3220. * @param {Function|Object|string} [callback=identity] The function called
  3221. * per iteration. If a property name or object is provided it will be used
  3222. * to create a "_.pluck" or "_.where" style callback, respectively.
  3223. * @param {*} [thisArg] The `this` binding of `callback`.
  3224. * @returns {Array} Returns a new array of elements that passed the callback check.
  3225. * @example
  3226. *
  3227. * var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  3228. * // => [2, 4, 6]
  3229. *
  3230. * var characters = [
  3231. * { 'name': 'barney', 'age': 36, 'blocked': false },
  3232. * { 'name': 'fred', 'age': 40, 'blocked': true }
  3233. * ];
  3234. *
  3235. * // using "_.pluck" callback shorthand
  3236. * _.filter(characters, 'blocked');
  3237. * // => [{ 'name': 'fred', 'age': 40, 'blocked': true }]
  3238. *
  3239. * // using "_.where" callback shorthand
  3240. * _.filter(characters, { 'age': 36 });
  3241. * // => [{ 'name': 'barney', 'age': 36, 'blocked': false }]
  3242. */
  3243. function filter(collection, callback, thisArg) {
  3244. var result = [];
  3245. callback = lodash.createCallback(callback, thisArg, 3);
  3246. if (isArray(collection)) {
  3247. var index = -1,
  3248. length = collection.length;
  3249. while (++index < length) {
  3250. var value = collection[index];
  3251. if (callback(value, index, collection)) {
  3252. result.push(value);
  3253. }
  3254. }
  3255. } else {
  3256. baseEach(collection, function(value, index, collection) {
  3257. if (callback(value, index, collection)) {
  3258. result.push(value);
  3259. }
  3260. });
  3261. }
  3262. return result;
  3263. }
  3264. /**
  3265. * Iterates over elements of a collection, returning the first element that
  3266. * the callback returns truey for. The callback is bound to `thisArg` and
  3267. * invoked with three arguments; (value, index|key, collection).
  3268. *
  3269. * If a property name is provided for `callback` the created "_.pluck" style
  3270. * callback will return the property value of the given element.
  3271. *
  3272. * If an object is provided for `callback` the created "_.where" style callback
  3273. * will return `true` for elements that have the properties of the given object,
  3274. * else `false`.
  3275. *
  3276. * @static
  3277. * @memberOf _
  3278. * @alias detect, findWhere
  3279. * @category Collections
  3280. * @param {Array|Object|string} collection The collection to iterate over.
  3281. * @param {Function|Object|string} [callback=identity] The function called
  3282. * per iteration. If a property name or object is provided it will be used
  3283. * to create a "_.pluck" or "_.where" style callback, respectively.
  3284. * @param {*} [thisArg] The `this` binding of `callback`.
  3285. * @returns {*} Returns the found element, else `undefined`.
  3286. * @example
  3287. *
  3288. * var characters = [
  3289. * { 'name': 'barney', 'age': 36, 'blocked': false },
  3290. * { 'name': 'fred', 'age': 40, 'blocked': true },
  3291. * { 'name': 'pebbles', 'age': 1, 'blocked': false }
  3292. * ];
  3293. *
  3294. * _.find(characters, function(chr) {
  3295. * return chr.age < 40;
  3296. * });
  3297. * // => { 'name': 'barney', 'age': 36, 'blocked': false }
  3298. *
  3299. * // using "_.where" callback shorthand
  3300. * _.find(characters, { 'age': 1 });
  3301. * // => { 'name': 'pebbles', 'age': 1, 'blocked': false }
  3302. *
  3303. * // using "_.pluck" callback shorthand
  3304. * _.find(characters, 'blocked');
  3305. * // => { 'name': 'fred', 'age': 40, 'blocked': true }
  3306. */
  3307. function find(collection, callback, thisArg) {
  3308. callback = lodash.createCallback(callback, thisArg, 3);
  3309. if (isArray(collection)) {
  3310. var index = -1,
  3311. length = collection.length;
  3312. while (++index < length) {
  3313. var value = collection[index];
  3314. if (callback(value, index, collection)) {
  3315. return value;
  3316. }
  3317. }
  3318. } else {
  3319. var result;
  3320. baseEach(collection, function(value, index, collection) {
  3321. if (callback(value, index, collection)) {
  3322. result = value;
  3323. return false;
  3324. }
  3325. });
  3326. return result;
  3327. }
  3328. }
  3329. /**
  3330. * This method is like `_.find` except that it iterates over elements
  3331. * of a `collection` from right to left.
  3332. *
  3333. * @static
  3334. * @memberOf _
  3335. * @category Collections
  3336. * @param {Array|Object|string} collection The collection to iterate over.
  3337. * @param {Function|Object|string} [callback=identity] The function called
  3338. * per iteration. If a property name or object is provided it will be used
  3339. * to create a "_.pluck" or "_.where" style callback, respectively.
  3340. * @param {*} [thisArg] The `this` binding of `callback`.
  3341. * @returns {*} Returns the found element, else `undefined`.
  3342. * @example
  3343. *
  3344. * _.findLast([1, 2, 3, 4], function(num) {
  3345. * return num % 2 == 1;
  3346. * });
  3347. * // => 3
  3348. */
  3349. function findLast(collection, callback, thisArg) {
  3350. var result;
  3351. callback = lodash.createCallback(callback, thisArg, 3);
  3352. forEachRight(collection, function(value, index, collection) {
  3353. if (callback(value, index, collection)) {
  3354. result = value;
  3355. return false;
  3356. }
  3357. });
  3358. return result;
  3359. }
  3360. /**
  3361. * Iterates over elements of a collection, executing the callback for each
  3362. * element. The callback is bound to `thisArg` and invoked with three arguments;
  3363. * (value, index|key, collection). Callbacks may exit iteration early by
  3364. * explicitly returning `false`.
  3365. *
  3366. * Note: As with other "Collections" methods, objects with a `length` property
  3367. * are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn`
  3368. * may be used for object iteration.
  3369. *
  3370. * @static
  3371. * @memberOf _
  3372. * @alias each
  3373. * @category Collections
  3374. * @param {Array|Object|string} collection The collection to iterate over.
  3375. * @param {Function} [callback=identity] The function called per iteration.
  3376. * @param {*} [thisArg] The `this` binding of `callback`.
  3377. * @returns {Array|Object|string} Returns `collection`.
  3378. * @example
  3379. *
  3380. * _([1, 2, 3]).forEach(function(num) { console.log(num); }).join(',');
  3381. * // => logs each number and returns '1,2,3'
  3382. *
  3383. * _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); });
  3384. * // => logs each number and returns the object (property order is not guaranteed across environments)
  3385. */
  3386. function forEach(collection, callback, thisArg) {
  3387. if (callback && typeof thisArg == 'undefined' && isArray(collection)) {
  3388. var index = -1,
  3389. length = collection.length;
  3390. while (++index < length) {
  3391. if (callback(collection[index], index, collection) === false) {
  3392. break;
  3393. }
  3394. }
  3395. } else {
  3396. baseEach(collection, callback, thisArg);
  3397. }
  3398. return collection;
  3399. }
  3400. /**
  3401. * This method is like `_.forEach` except that it iterates over elements
  3402. * of a `collection` from right to left.
  3403. *
  3404. * @static
  3405. * @memberOf _
  3406. * @alias eachRight
  3407. * @category Collections
  3408. * @param {Array|Object|string} collection The collection to iterate over.
  3409. * @param {Function} [callback=identity] The function called per iteration.
  3410. * @param {*} [thisArg] The `this` binding of `callback`.
  3411. * @returns {Array|Object|string} Returns `collection`.
  3412. * @example
  3413. *
  3414. * _([1, 2, 3]).forEachRight(function(num) { console.log(num); }).join(',');
  3415. * // => logs each number from right to left and returns '3,2,1'
  3416. */
  3417. function forEachRight(collection, callback, thisArg) {
  3418. var iterable = collection,
  3419. length = collection ? collection.length : 0;
  3420. callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3);
  3421. if (isArray(collection)) {
  3422. while (length--) {
  3423. if (callback(collection[length], length, collection) === false) {
  3424. break;
  3425. }
  3426. }
  3427. } else {
  3428. if (typeof length != 'number') {
  3429. var props = keys(collection);
  3430. length = props.length;
  3431. } else if (support.unindexedChars && isString(collection)) {
  3432. iterable = collection.split('');
  3433. }
  3434. baseEach(collection, function(value, key, collection) {
  3435. key = props ? props[--length] : --length;
  3436. return callback(iterable[key], key, collection);
  3437. });
  3438. }
  3439. return collection;
  3440. }
  3441. /**
  3442. * Creates an object composed of keys generated from the results of running
  3443. * each element of a collection through the callback. The corresponding value
  3444. * of each key is an array of the elements responsible for generating the key.
  3445. * The callback is bound to `thisArg` and invoked with three arguments;
  3446. * (value, index|key, collection).
  3447. *
  3448. * If a property name is provided for `callback` the created "_.pluck" style
  3449. * callback will return the property value of the given element.
  3450. *
  3451. * If an object is provided for `callback` the created "_.where" style callback
  3452. * will return `true` for elements that have the properties of the given object,
  3453. * else `false`
  3454. *
  3455. * @static
  3456. * @memberOf _
  3457. * @category Collections
  3458. * @param {Array|Object|string} collection The collection to iterate over.
  3459. * @param {Function|Object|string} [callback=identity] The function called
  3460. * per iteration. If a property name or object is provided it will be used
  3461. * to create a "_.pluck" or "_.where" style callback, respectively.
  3462. * @param {*} [thisArg] The `this` binding of `callback`.
  3463. * @returns {Object} Returns the composed aggregate object.
  3464. * @example
  3465. *
  3466. * _.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); });
  3467. * // => { '4': [4.2], '6': [6.1, 6.4] }
  3468. *
  3469. * _.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
  3470. * // => { '4': [4.2], '6': [6.1, 6.4] }
  3471. *
  3472. * // using "_.pluck" callback shorthand
  3473. * _.groupBy(['one', 'two', 'three'], 'length');
  3474. * // => { '3': ['one', 'two'], '5': ['three'] }
  3475. */
  3476. var groupBy = createAggregator(function(result, value, key) {
  3477. (hasOwnProperty.call(result, key) ? result[key] : result[key] = []).push(value);
  3478. });
  3479. /**
  3480. * Creates an object composed of keys generated from the results of running
  3481. * each element of the collection through the given callback. The corresponding
  3482. * value of each key is the last element responsible for generating the key.
  3483. * The callback is bound to `thisArg` and invoked with three arguments;
  3484. * (value, index|key, collection).
  3485. *
  3486. * If a property name is provided for `callback` the created "_.pluck" style
  3487. * callback will return the property value of the given element.
  3488. *
  3489. * If an object is provided for `callback` the created "_.where" style callback
  3490. * will return `true` for elements that have the properties of the given object,
  3491. * else `false`.
  3492. *
  3493. * @static
  3494. * @memberOf _
  3495. * @category Collections
  3496. * @param {Array|Object|string} collection The collection to iterate over.
  3497. * @param {Function|Object|string} [callback=identity] The function called
  3498. * per iteration. If a property name or object is provided it will be used
  3499. * to create a "_.pluck" or "_.where" style callback, respectively.
  3500. * @param {*} [thisArg] The `this` binding of `callback`.
  3501. * @returns {Object} Returns the composed aggregate object.
  3502. * @example
  3503. *
  3504. * var keys = [
  3505. * { 'dir': 'left', 'code': 97 },
  3506. * { 'dir': 'right', 'code': 100 }
  3507. * ];
  3508. *
  3509. * _.indexBy(keys, 'dir');
  3510. * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
  3511. *
  3512. * _.indexBy(keys, function(key) { return String.fromCharCode(key.code); });
  3513. * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  3514. *
  3515. * _.indexBy(characters, function(key) { this.fromCharCode(key.code); }, String);
  3516. * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  3517. */
  3518. var indexBy = createAggregator(function(result, value, key) {
  3519. result[key] = value;
  3520. });
  3521. /**
  3522. * Invokes the method named by `methodName` on each element in the `collection`
  3523. * returning an array of the results of each invoked method. Additional arguments
  3524. * will be provided to each invoked method. If `methodName` is a function it
  3525. * will be invoked for, and `this` bound to, each element in the `collection`.
  3526. *
  3527. * @static
  3528. * @memberOf _
  3529. * @category Collections
  3530. * @param {Array|Object|string} collection The collection to iterate over.
  3531. * @param {Function|string} methodName The name of the method to invoke or
  3532. * the function invoked per iteration.
  3533. * @param {...*} [arg] Arguments to invoke the method with.
  3534. * @returns {Array} Returns a new array of the results of each invoked method.
  3535. * @example
  3536. *
  3537. * _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  3538. * // => [[1, 5, 7], [1, 2, 3]]
  3539. *
  3540. * _.invoke([123, 456], String.prototype.split, '');
  3541. * // => [['1', '2', '3'], ['4', '5', '6']]
  3542. */
  3543. function invoke(collection, methodName) {
  3544. var args = slice(arguments, 2),
  3545. index = -1,
  3546. isFunc = typeof methodName == 'function',
  3547. length = collection ? collection.length : 0,
  3548. result = Array(typeof length == 'number' ? length : 0);
  3549. forEach(collection, function(value) {
  3550. result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
  3551. });
  3552. return result;
  3553. }
  3554. /**
  3555. * Creates an array of values by running each element in the collection
  3556. * through the callback. The callback is bound to `thisArg` and invoked with
  3557. * three arguments; (value, index|key, collection).
  3558. *
  3559. * If a property name is provided for `callback` the created "_.pluck" style
  3560. * callback will return the property value of the given element.
  3561. *
  3562. * If an object is provided for `callback` the created "_.where" style callback
  3563. * will return `true` for elements that have the properties of the given object,
  3564. * else `false`.
  3565. *
  3566. * @static
  3567. * @memberOf _
  3568. * @alias collect
  3569. * @category Collections
  3570. * @param {Array|Object|string} collection The collection to iterate over.
  3571. * @param {Function|Object|string} [callback=identity] The function called
  3572. * per iteration. If a property name or object is provided it will be used
  3573. * to create a "_.pluck" or "_.where" style callback, respectively.
  3574. * @param {*} [thisArg] The `this` binding of `callback`.
  3575. * @returns {Array} Returns a new array of the results of each `callback` execution.
  3576. * @example
  3577. *
  3578. * _.map([1, 2, 3], function(num) { return num * 3; });
  3579. * // => [3, 6, 9]
  3580. *
  3581. * _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
  3582. * // => [3, 6, 9] (property order is not guaranteed across environments)
  3583. *
  3584. * var characters = [
  3585. * { 'name': 'barney', 'age': 36 },
  3586. * { 'name': 'fred', 'age': 40 }
  3587. * ];
  3588. *
  3589. * // using "_.pluck" callback shorthand
  3590. * _.map(characters, 'name');
  3591. * // => ['barney', 'fred']
  3592. */
  3593. function map(collection, callback, thisArg) {
  3594. var index = -1,
  3595. length = collection ? collection.length : 0,
  3596. result = Array(typeof length == 'number' ? length : 0);
  3597. callback = lodash.createCallback(callback, thisArg, 3);
  3598. if (isArray(collection)) {
  3599. while (++index < length) {
  3600. result[index] = callback(collection[index], index, collection);
  3601. }
  3602. } else {
  3603. baseEach(collection, function(value, key, collection) {
  3604. result[++index] = callback(value, key, collection);
  3605. });
  3606. }
  3607. return result;
  3608. }
  3609. /**
  3610. * Retrieves the maximum value of a collection. If the collection is empty or
  3611. * falsey `-Infinity` is returned. If a callback is provided it will be executed
  3612. * for each value in the collection to generate the criterion by which the value
  3613. * is ranked. The callback is bound to `thisArg` and invoked with three
  3614. * arguments; (value, index, collection).
  3615. *
  3616. * If a property name is provided for `callback` the created "_.pluck" style
  3617. * callback will return the property value of the given element.
  3618. *
  3619. * If an object is provided for `callback` the created "_.where" style callback
  3620. * will return `true` for elements that have the properties of the given object,
  3621. * else `false`.
  3622. *
  3623. * @static
  3624. * @memberOf _
  3625. * @category Collections
  3626. * @param {Array|Object|string} collection The collection to iterate over.
  3627. * @param {Function|Object|string} [callback=identity] The function called
  3628. * per iteration. If a property name or object is provided it will be used
  3629. * to create a "_.pluck" or "_.where" style callback, respectively.
  3630. * @param {*} [thisArg] The `this` binding of `callback`.
  3631. * @returns {*} Returns the maximum value.
  3632. * @example
  3633. *
  3634. * _.max([4, 2, 8, 6]);
  3635. * // => 8
  3636. *
  3637. * var characters = [
  3638. * { 'name': 'barney', 'age': 36 },
  3639. * { 'name': 'fred', 'age': 40 }
  3640. * ];
  3641. *
  3642. * _.max(characters, function(chr) { return chr.age; });
  3643. * // => { 'name': 'fred', 'age': 40 };
  3644. *
  3645. * // using "_.pluck" callback shorthand
  3646. * _.max(characters, 'age');
  3647. * // => { 'name': 'fred', 'age': 40 };
  3648. */
  3649. function max(collection, callback, thisArg) {
  3650. var computed = -Infinity,
  3651. result = computed;
  3652. // allows working with functions like `_.map` without using
  3653. // their `index` argument as a callback
  3654. if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) {
  3655. callback = null;
  3656. }
  3657. if (callback == null && isArray(collection)) {
  3658. var index = -1,
  3659. length = collection.length;
  3660. while (++index < length) {
  3661. var value = collection[index];
  3662. if (value > result) {
  3663. result = value;
  3664. }
  3665. }
  3666. } else {
  3667. callback = (callback == null && isString(collection))
  3668. ? charAtCallback
  3669. : lodash.createCallback(callback, thisArg, 3);
  3670. baseEach(collection, function(value, index, collection) {
  3671. var current = callback(value, index, collection);
  3672. if (current > computed) {
  3673. computed = current;
  3674. result = value;
  3675. }
  3676. });
  3677. }
  3678. return result;
  3679. }
  3680. /**
  3681. * Retrieves the minimum value of a collection. If the collection is empty or
  3682. * falsey `Infinity` is returned. If a callback is provided it will be executed
  3683. * for each value in the collection to generate the criterion by which the value
  3684. * is ranked. The callback is bound to `thisArg` and invoked with three
  3685. * arguments; (value, index, collection).
  3686. *
  3687. * If a property name is provided for `callback` the created "_.pluck" style
  3688. * callback will return the property value of the given element.
  3689. *
  3690. * If an object is provided for `callback` the created "_.where" style callback
  3691. * will return `true` for elements that have the properties of the given object,
  3692. * else `false`.
  3693. *
  3694. * @static
  3695. * @memberOf _
  3696. * @category Collections
  3697. * @param {Array|Object|string} collection The collection to iterate over.
  3698. * @param {Function|Object|string} [callback=identity] The function called
  3699. * per iteration. If a property name or object is provided it will be used
  3700. * to create a "_.pluck" or "_.where" style callback, respectively.
  3701. * @param {*} [thisArg] The `this` binding of `callback`.
  3702. * @returns {*} Returns the minimum value.
  3703. * @example
  3704. *
  3705. * _.min([4, 2, 8, 6]);
  3706. * // => 2
  3707. *
  3708. * var characters = [
  3709. * { 'name': 'barney', 'age': 36 },
  3710. * { 'name': 'fred', 'age': 40 }
  3711. * ];
  3712. *
  3713. * _.min(characters, function(chr) { return chr.age; });
  3714. * // => { 'name': 'barney', 'age': 36 };
  3715. *
  3716. * // using "_.pluck" callback shorthand
  3717. * _.min(characters, 'age');
  3718. * // => { 'name': 'barney', 'age': 36 };
  3719. */
  3720. function min(collection, callback, thisArg) {
  3721. var computed = Infinity,
  3722. result = computed;
  3723. // allows working with functions like `_.map` without using
  3724. // their `index` argument as a callback
  3725. if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) {
  3726. callback = null;
  3727. }
  3728. if (callback == null && isArray(collection)) {
  3729. var index = -1,
  3730. length = collection.length;
  3731. while (++index < length) {
  3732. var value = collection[index];
  3733. if (value < result) {
  3734. result = value;
  3735. }
  3736. }
  3737. } else {
  3738. callback = (callback == null && isString(collection))
  3739. ? charAtCallback
  3740. : lodash.createCallback(callback, thisArg, 3);
  3741. baseEach(collection, function(value, index, collection) {
  3742. var current = callback(value, index, collection);
  3743. if (current < computed) {
  3744. computed = current;
  3745. result = value;
  3746. }
  3747. });
  3748. }
  3749. return result;
  3750. }
  3751. /**
  3752. * Retrieves the value of a specified property from all elements in the collection.
  3753. *
  3754. * @static
  3755. * @memberOf _
  3756. * @type Function
  3757. * @category Collections
  3758. * @param {Array|Object|string} collection The collection to iterate over.
  3759. * @param {string} property The name of the property to pluck.
  3760. * @returns {Array} Returns a new array of property values.
  3761. * @example
  3762. *
  3763. * var characters = [
  3764. * { 'name': 'barney', 'age': 36 },
  3765. * { 'name': 'fred', 'age': 40 }
  3766. * ];
  3767. *
  3768. * _.pluck(characters, 'name');
  3769. * // => ['barney', 'fred']
  3770. */
  3771. var pluck = map;
  3772. /**
  3773. * Reduces a collection to a value which is the accumulated result of running
  3774. * each element in the collection through the callback, where each successive
  3775. * callback execution consumes the return value of the previous execution. If
  3776. * `accumulator` is not provided the first element of the collection will be
  3777. * used as the initial `accumulator` value. The callback is bound to `thisArg`
  3778. * and invoked with four arguments; (accumulator, value, index|key, collection).
  3779. *
  3780. * @static
  3781. * @memberOf _
  3782. * @alias foldl, inject
  3783. * @category Collections
  3784. * @param {Array|Object|string} collection The collection to iterate over.
  3785. * @param {Function} [callback=identity] The function called per iteration.
  3786. * @param {*} [accumulator] Initial value of the accumulator.
  3787. * @param {*} [thisArg] The `this` binding of `callback`.
  3788. * @returns {*} Returns the accumulated value.
  3789. * @example
  3790. *
  3791. * var sum = _.reduce([1, 2, 3], function(sum, num) {
  3792. * return sum + num;
  3793. * });
  3794. * // => 6
  3795. *
  3796. * var mapped = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
  3797. * result[key] = num * 3;
  3798. * return result;
  3799. * }, {});
  3800. * // => { 'a': 3, 'b': 6, 'c': 9 }
  3801. */
  3802. function reduce(collection, callback, accumulator, thisArg) {
  3803. var noaccum = arguments.length < 3;
  3804. callback = lodash.createCallback(callback, thisArg, 4);
  3805. if (isArray(collection)) {
  3806. var index = -1,
  3807. length = collection.length;
  3808. if (noaccum) {
  3809. accumulator = collection[++index];
  3810. }
  3811. while (++index < length) {
  3812. accumulator = callback(accumulator, collection[index], index, collection);
  3813. }
  3814. } else {
  3815. baseEach(collection, function(value, index, collection) {
  3816. accumulator = noaccum
  3817. ? (noaccum = false, value)
  3818. : callback(accumulator, value, index, collection)
  3819. });
  3820. }
  3821. return accumulator;
  3822. }
  3823. /**
  3824. * This method is like `_.reduce` except that it iterates over elements
  3825. * of a `collection` from right to left.
  3826. *
  3827. * @static
  3828. * @memberOf _
  3829. * @alias foldr
  3830. * @category Collections
  3831. * @param {Array|Object|string} collection The collection to iterate over.
  3832. * @param {Function} [callback=identity] The function called per iteration.
  3833. * @param {*} [accumulator] Initial value of the accumulator.
  3834. * @param {*} [thisArg] The `this` binding of `callback`.
  3835. * @returns {*} Returns the accumulated value.
  3836. * @example
  3837. *
  3838. * var list = [[0, 1], [2, 3], [4, 5]];
  3839. * var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
  3840. * // => [4, 5, 2, 3, 0, 1]
  3841. */
  3842. function reduceRight(collection, callback, accumulator, thisArg) {
  3843. var noaccum = arguments.length < 3;
  3844. callback = lodash.createCallback(callback, thisArg, 4);
  3845. forEachRight(collection, function(value, index, collection) {
  3846. accumulator = noaccum
  3847. ? (noaccum = false, value)
  3848. : callback(accumulator, value, index, collection);
  3849. });
  3850. return accumulator;
  3851. }
  3852. /**
  3853. * The opposite of `_.filter` this method returns the elements of a
  3854. * collection that the callback does **not** return truey for.
  3855. *
  3856. * If a property name is provided for `callback` the created "_.pluck" style
  3857. * callback will return the property value of the given element.
  3858. *
  3859. * If an object is provided for `callback` the created "_.where" style callback
  3860. * will return `true` for elements that have the properties of the given object,
  3861. * else `false`.
  3862. *
  3863. * @static
  3864. * @memberOf _
  3865. * @category Collections
  3866. * @param {Array|Object|string} collection The collection to iterate over.
  3867. * @param {Function|Object|string} [callback=identity] The function called
  3868. * per iteration. If a property name or object is provided it will be used
  3869. * to create a "_.pluck" or "_.where" style callback, respectively.
  3870. * @param {*} [thisArg] The `this` binding of `callback`.
  3871. * @returns {Array} Returns a new array of elements that failed the callback check.
  3872. * @example
  3873. *
  3874. * var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  3875. * // => [1, 3, 5]
  3876. *
  3877. * var characters = [
  3878. * { 'name': 'barney', 'age': 36, 'blocked': false },
  3879. * { 'name': 'fred', 'age': 40, 'blocked': true }
  3880. * ];
  3881. *
  3882. * // using "_.pluck" callback shorthand
  3883. * _.reject(characters, 'blocked');
  3884. * // => [{ 'name': 'barney', 'age': 36, 'blocked': false }]
  3885. *
  3886. * // using "_.where" callback shorthand
  3887. * _.reject(characters, { 'age': 36 });
  3888. * // => [{ 'name': 'fred', 'age': 40, 'blocked': true }]
  3889. */
  3890. function reject(collection, callback, thisArg) {
  3891. callback = lodash.createCallback(callback, thisArg, 3);
  3892. return filter(collection, function(value, index, collection) {
  3893. return !callback(value, index, collection);
  3894. });
  3895. }
  3896. /**
  3897. * Retrieves a random element or `n` random elements from a collection.
  3898. *
  3899. * @static
  3900. * @memberOf _
  3901. * @category Collections
  3902. * @param {Array|Object|string} collection The collection to sample.
  3903. * @param {number} [n] The number of elements to sample.
  3904. * @param- {Object} [guard] Allows working with functions like `_.map`
  3905. * without using their `index` arguments as `n`.
  3906. * @returns {Array} Returns the random sample(s) of `collection`.
  3907. * @example
  3908. *
  3909. * _.sample([1, 2, 3, 4]);
  3910. * // => 2
  3911. *
  3912. * _.sample([1, 2, 3, 4], 2);
  3913. * // => [3, 1]
  3914. */
  3915. function sample(collection, n, guard) {
  3916. if (collection && typeof collection.length != 'number') {
  3917. collection = values(collection);
  3918. } else if (support.unindexedChars && isString(collection)) {
  3919. collection = collection.split('');
  3920. }
  3921. if (n == null || guard) {
  3922. return collection ? collection[baseRandom(0, collection.length - 1)] : undefined;
  3923. }
  3924. var result = shuffle(collection);
  3925. result.length = nativeMin(nativeMax(0, n), result.length);
  3926. return result;
  3927. }
  3928. /**
  3929. * Creates an array of shuffled values, using a version of the Fisher-Yates
  3930. * shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle.
  3931. *
  3932. * @static
  3933. * @memberOf _
  3934. * @category Collections
  3935. * @param {Array|Object|string} collection The collection to shuffle.
  3936. * @returns {Array} Returns a new shuffled collection.
  3937. * @example
  3938. *
  3939. * _.shuffle([1, 2, 3, 4, 5, 6]);
  3940. * // => [4, 1, 6, 3, 5, 2]
  3941. */
  3942. function shuffle(collection) {
  3943. var index = -1,
  3944. length = collection ? collection.length : 0,
  3945. result = Array(typeof length == 'number' ? length : 0);
  3946. forEach(collection, function(value) {
  3947. var rand = baseRandom(0, ++index);
  3948. result[index] = result[rand];
  3949. result[rand] = value;
  3950. });
  3951. return result;
  3952. }
  3953. /**
  3954. * Gets the size of the `collection` by returning `collection.length` for arrays
  3955. * and array-like objects or the number of own enumerable properties for objects.
  3956. *
  3957. * @static
  3958. * @memberOf _
  3959. * @category Collections
  3960. * @param {Array|Object|string} collection The collection to inspect.
  3961. * @returns {number} Returns `collection.length` or number of own enumerable properties.
  3962. * @example
  3963. *
  3964. * _.size([1, 2]);
  3965. * // => 2
  3966. *
  3967. * _.size({ 'one': 1, 'two': 2, 'three': 3 });
  3968. * // => 3
  3969. *
  3970. * _.size('pebbles');
  3971. * // => 7
  3972. */
  3973. function size(collection) {
  3974. var length = collection ? collection.length : 0;
  3975. return typeof length == 'number' ? length : keys(collection).length;
  3976. }
  3977. /**
  3978. * Checks if the callback returns a truey value for **any** element of a
  3979. * collection. The function returns as soon as it finds a passing value and
  3980. * does not iterate over the entire collection. The callback is bound to
  3981. * `thisArg` and invoked with three arguments; (value, index|key, collection).
  3982. *
  3983. * If a property name is provided for `callback` the created "_.pluck" style
  3984. * callback will return the property value of the given element.
  3985. *
  3986. * If an object is provided for `callback` the created "_.where" style callback
  3987. * will return `true` for elements that have the properties of the given object,
  3988. * else `false`.
  3989. *
  3990. * @static
  3991. * @memberOf _
  3992. * @alias any
  3993. * @category Collections
  3994. * @param {Array|Object|string} collection The collection to iterate over.
  3995. * @param {Function|Object|string} [callback=identity] The function called
  3996. * per iteration. If a property name or object is provided it will be used
  3997. * to create a "_.pluck" or "_.where" style callback, respectively.
  3998. * @param {*} [thisArg] The `this` binding of `callback`.
  3999. * @returns {boolean} Returns `true` if any element passed the callback check,
  4000. * else `false`.
  4001. * @example
  4002. *
  4003. * _.some([null, 0, 'yes', false], Boolean);
  4004. * // => true
  4005. *
  4006. * var characters = [
  4007. * { 'name': 'barney', 'age': 36, 'blocked': false },
  4008. * { 'name': 'fred', 'age': 40, 'blocked': true }
  4009. * ];
  4010. *
  4011. * // using "_.pluck" callback shorthand
  4012. * _.some(characters, 'blocked');
  4013. * // => true
  4014. *
  4015. * // using "_.where" callback shorthand
  4016. * _.some(characters, { 'age': 1 });
  4017. * // => false
  4018. */
  4019. function some(collection, callback, thisArg) {
  4020. var result;
  4021. callback = lodash.createCallback(callback, thisArg, 3);
  4022. if (isArray(collection)) {
  4023. var index = -1,
  4024. length = collection.length;
  4025. while (++index < length) {
  4026. if ((result = callback(collection[index], index, collection))) {
  4027. break;
  4028. }
  4029. }
  4030. } else {
  4031. baseEach(collection, function(value, index, collection) {
  4032. return !(result = callback(value, index, collection));
  4033. });
  4034. }
  4035. return !!result;
  4036. }
  4037. /**
  4038. * Creates an array of elements, sorted in ascending order by the results of
  4039. * running each element in a collection through the callback. This method
  4040. * performs a stable sort, that is, it will preserve the original sort order
  4041. * of equal elements. The callback is bound to `thisArg` and invoked with
  4042. * three arguments; (value, index|key, collection).
  4043. *
  4044. * If a property name is provided for `callback` the created "_.pluck" style
  4045. * callback will return the property value of the given element.
  4046. *
  4047. * If an array of property names is provided for `callback` the collection
  4048. * will be sorted by each property value.
  4049. *
  4050. * If an object is provided for `callback` the created "_.where" style callback
  4051. * will return `true` for elements that have the properties of the given object,
  4052. * else `false`.
  4053. *
  4054. * @static
  4055. * @memberOf _
  4056. * @category Collections
  4057. * @param {Array|Object|string} collection The collection to iterate over.
  4058. * @param {Array|Function|Object|string} [callback=identity] The function called
  4059. * per iteration. If a property name or object is provided it will be used
  4060. * to create a "_.pluck" or "_.where" style callback, respectively.
  4061. * @param {*} [thisArg] The `this` binding of `callback`.
  4062. * @returns {Array} Returns a new array of sorted elements.
  4063. * @example
  4064. *
  4065. * _.sortBy([1, 2, 3], function(num) { return Math.sin(num); });
  4066. * // => [3, 1, 2]
  4067. *
  4068. * _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
  4069. * // => [3, 1, 2]
  4070. *
  4071. * var characters = [
  4072. * { 'name': 'barney', 'age': 36 },
  4073. * { 'name': 'fred', 'age': 40 },
  4074. * { 'name': 'barney', 'age': 26 },
  4075. * { 'name': 'fred', 'age': 30 }
  4076. * ];
  4077. *
  4078. * // using "_.pluck" callback shorthand
  4079. * _.map(_.sortBy(characters, 'age'), _.values);
  4080. * // => [['barney', 26], ['fred', 30], ['barney', 36], ['fred', 40]]
  4081. *
  4082. * // sorting by multiple properties
  4083. * _.map(_.sortBy(characters, ['name', 'age']), _.values);
  4084. * // = > [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]]
  4085. */
  4086. function sortBy(collection, callback, thisArg) {
  4087. var index = -1,
  4088. isArr = isArray(callback),
  4089. length = collection ? collection.length : 0,
  4090. result = Array(typeof length == 'number' ? length : 0);
  4091. if (!isArr) {
  4092. callback = lodash.createCallback(callback, thisArg, 3);
  4093. }
  4094. forEach(collection, function(value, key, collection) {
  4095. var object = result[++index] = getObject();
  4096. if (isArr) {
  4097. object.criteria = map(callback, function(key) { return value[key]; });
  4098. } else {
  4099. (object.criteria = getArray())[0] = callback(value, key, collection);
  4100. }
  4101. object.index = index;
  4102. object.value = value;
  4103. });
  4104. length = result.length;
  4105. result.sort(compareAscending);
  4106. while (length--) {
  4107. var object = result[length];
  4108. result[length] = object.value;
  4109. if (!isArr) {
  4110. releaseArray(object.criteria);
  4111. }
  4112. releaseObject(object);
  4113. }
  4114. return result;
  4115. }
  4116. /**
  4117. * Converts the `collection` to an array.
  4118. *
  4119. * @static
  4120. * @memberOf _
  4121. * @category Collections
  4122. * @param {Array|Object|string} collection The collection to convert.
  4123. * @returns {Array} Returns the new converted array.
  4124. * @example
  4125. *
  4126. * (function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
  4127. * // => [2, 3, 4]
  4128. */
  4129. function toArray(collection) {
  4130. if (collection && typeof collection.length == 'number') {
  4131. return (support.unindexedChars && isString(collection))
  4132. ? collection.split('')
  4133. : slice(collection);
  4134. }
  4135. return values(collection);
  4136. }
  4137. /**
  4138. * Performs a deep comparison of each element in a `collection` to the given
  4139. * `properties` object, returning an array of all elements that have equivalent
  4140. * property values.
  4141. *
  4142. * @static
  4143. * @memberOf _
  4144. * @type Function
  4145. * @category Collections
  4146. * @param {Array|Object|string} collection The collection to iterate over.
  4147. * @param {Object} props The object of property values to filter by.
  4148. * @returns {Array} Returns a new array of elements that have the given properties.
  4149. * @example
  4150. *
  4151. * var characters = [
  4152. * { 'name': 'barney', 'age': 36, 'pets': ['hoppy'] },
  4153. * { 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }
  4154. * ];
  4155. *
  4156. * _.where(characters, { 'age': 36 });
  4157. * // => [{ 'name': 'barney', 'age': 36, 'pets': ['hoppy'] }]
  4158. *
  4159. * _.where(characters, { 'pets': ['dino'] });
  4160. * // => [{ 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }]
  4161. */
  4162. var where = filter;
  4163. /*--------------------------------------------------------------------------*/
  4164. /**
  4165. * Creates an array with all falsey values removed. The values `false`, `null`,
  4166. * `0`, `""`, `undefined`, and `NaN` are all falsey.
  4167. *
  4168. * @static
  4169. * @memberOf _
  4170. * @category Arrays
  4171. * @param {Array} array The array to compact.
  4172. * @returns {Array} Returns a new array of filtered values.
  4173. * @example
  4174. *
  4175. * _.compact([0, 1, false, 2, '', 3]);
  4176. * // => [1, 2, 3]
  4177. */
  4178. function compact(array) {
  4179. var index = -1,
  4180. length = array ? array.length : 0,
  4181. result = [];
  4182. while (++index < length) {
  4183. var value = array[index];
  4184. if (value) {
  4185. result.push(value);
  4186. }
  4187. }
  4188. return result;
  4189. }
  4190. /**
  4191. * Creates an array excluding all values of the provided arrays using strict
  4192. * equality for comparisons, i.e. `===`.
  4193. *
  4194. * @static
  4195. * @memberOf _
  4196. * @category Arrays
  4197. * @param {Array} array The array to process.
  4198. * @param {...Array} [values] The arrays of values to exclude.
  4199. * @returns {Array} Returns a new array of filtered values.
  4200. * @example
  4201. *
  4202. * _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
  4203. * // => [1, 3, 4]
  4204. */
  4205. function difference(array) {
  4206. return baseDifference(array, baseFlatten(arguments, true, true, 1));
  4207. }
  4208. /**
  4209. * This method is like `_.find` except that it returns the index of the first
  4210. * element that passes the callback check, instead of the element itself.
  4211. *
  4212. * If a property name is provided for `callback` the created "_.pluck" style
  4213. * callback will return the property value of the given element.
  4214. *
  4215. * If an object is provided for `callback` the created "_.where" style callback
  4216. * will return `true` for elements that have the properties of the given object,
  4217. * else `false`.
  4218. *
  4219. * @static
  4220. * @memberOf _
  4221. * @category Arrays
  4222. * @param {Array} array The array to search.
  4223. * @param {Function|Object|string} [callback=identity] The function called
  4224. * per iteration. If a property name or object is provided it will be used
  4225. * to create a "_.pluck" or "_.where" style callback, respectively.
  4226. * @param {*} [thisArg] The `this` binding of `callback`.
  4227. * @returns {number} Returns the index of the found element, else `-1`.
  4228. * @example
  4229. *
  4230. * var characters = [
  4231. * { 'name': 'barney', 'age': 36, 'blocked': false },
  4232. * { 'name': 'fred', 'age': 40, 'blocked': true },
  4233. * { 'name': 'pebbles', 'age': 1, 'blocked': false }
  4234. * ];
  4235. *
  4236. * _.findIndex(characters, function(chr) {
  4237. * return chr.age < 20;
  4238. * });
  4239. * // => 2
  4240. *
  4241. * // using "_.where" callback shorthand
  4242. * _.findIndex(characters, { 'age': 36 });
  4243. * // => 0
  4244. *
  4245. * // using "_.pluck" callback shorthand
  4246. * _.findIndex(characters, 'blocked');
  4247. * // => 1
  4248. */
  4249. function findIndex(array, callback, thisArg) {
  4250. var index = -1,
  4251. length = array ? array.length : 0;
  4252. callback = lodash.createCallback(callback, thisArg, 3);
  4253. while (++index < length) {
  4254. if (callback(array[index], index, array)) {
  4255. return index;
  4256. }
  4257. }
  4258. return -1;
  4259. }
  4260. /**
  4261. * This method is like `_.findIndex` except that it iterates over elements
  4262. * of a `collection` from right to left.
  4263. *
  4264. * If a property name is provided for `callback` the created "_.pluck" style
  4265. * callback will return the property value of the given element.
  4266. *
  4267. * If an object is provided for `callback` the created "_.where" style callback
  4268. * will return `true` for elements that have the properties of the given object,
  4269. * else `false`.
  4270. *
  4271. * @static
  4272. * @memberOf _
  4273. * @category Arrays
  4274. * @param {Array} array The array to search.
  4275. * @param {Function|Object|string} [callback=identity] The function called
  4276. * per iteration. If a property name or object is provided it will be used
  4277. * to create a "_.pluck" or "_.where" style callback, respectively.
  4278. * @param {*} [thisArg] The `this` binding of `callback`.
  4279. * @returns {number} Returns the index of the found element, else `-1`.
  4280. * @example
  4281. *
  4282. * var characters = [
  4283. * { 'name': 'barney', 'age': 36, 'blocked': true },
  4284. * { 'name': 'fred', 'age': 40, 'blocked': false },
  4285. * { 'name': 'pebbles', 'age': 1, 'blocked': true }
  4286. * ];
  4287. *
  4288. * _.findLastIndex(characters, function(chr) {
  4289. * return chr.age > 30;
  4290. * });
  4291. * // => 1
  4292. *
  4293. * // using "_.where" callback shorthand
  4294. * _.findLastIndex(characters, { 'age': 36 });
  4295. * // => 0
  4296. *
  4297. * // using "_.pluck" callback shorthand
  4298. * _.findLastIndex(characters, 'blocked');
  4299. * // => 2
  4300. */
  4301. function findLastIndex(array, callback, thisArg) {
  4302. var length = array ? array.length : 0;
  4303. callback = lodash.createCallback(callback, thisArg, 3);
  4304. while (length--) {
  4305. if (callback(array[length], length, array)) {
  4306. return length;
  4307. }
  4308. }
  4309. return -1;
  4310. }
  4311. /**
  4312. * Gets the first element or first `n` elements of an array. If a callback
  4313. * is provided elements at the beginning of the array are returned as long
  4314. * as the callback returns truey. The callback is bound to `thisArg` and
  4315. * invoked with three arguments; (value, index, array).
  4316. *
  4317. * If a property name is provided for `callback` the created "_.pluck" style
  4318. * callback will return the property value of the given element.
  4319. *
  4320. * If an object is provided for `callback` the created "_.where" style callback
  4321. * will return `true` for elements that have the properties of the given object,
  4322. * else `false`.
  4323. *
  4324. * @static
  4325. * @memberOf _
  4326. * @alias head, take
  4327. * @category Arrays
  4328. * @param {Array} array The array to query.
  4329. * @param {Function|Object|number|string} [callback] The function called
  4330. * per element or the number of elements to return. If a property name or
  4331. * object is provided it will be used to create a "_.pluck" or "_.where"
  4332. * style callback, respectively.
  4333. * @param {*} [thisArg] The `this` binding of `callback`.
  4334. * @returns {*} Returns the first element(s) of `array`.
  4335. * @example
  4336. *
  4337. * _.first([1, 2, 3]);
  4338. * // => 1
  4339. *
  4340. * _.first([1, 2, 3], 2);
  4341. * // => [1, 2]
  4342. *
  4343. * _.first([1, 2, 3], function(num) {
  4344. * return num < 3;
  4345. * });
  4346. * // => [1, 2]
  4347. *
  4348. * var characters = [
  4349. * { 'name': 'barney', 'blocked': true, 'employer': 'slate' },
  4350. * { 'name': 'fred', 'blocked': false, 'employer': 'slate' },
  4351. * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
  4352. * ];
  4353. *
  4354. * // using "_.pluck" callback shorthand
  4355. * _.first(characters, 'blocked');
  4356. * // => [{ 'name': 'barney', 'blocked': true, 'employer': 'slate' }]
  4357. *
  4358. * // using "_.where" callback shorthand
  4359. * _.pluck(_.first(characters, { 'employer': 'slate' }), 'name');
  4360. * // => ['barney', 'fred']
  4361. */
  4362. function first(array, callback, thisArg) {
  4363. var n = 0,
  4364. length = array ? array.length : 0;
  4365. if (typeof callback != 'number' && callback != null) {
  4366. var index = -1;
  4367. callback = lodash.createCallback(callback, thisArg, 3);
  4368. while (++index < length && callback(array[index], index, array)) {
  4369. n++;
  4370. }
  4371. } else {
  4372. n = callback;
  4373. if (n == null || thisArg) {
  4374. return array ? array[0] : undefined;
  4375. }
  4376. }
  4377. return slice(array, 0, nativeMin(nativeMax(0, n), length));
  4378. }
  4379. /**
  4380. * Flattens a nested array (the nesting can be to any depth). If `isShallow`
  4381. * is truey, the array will only be flattened a single level. If a callback
  4382. * is provided each element of the array is passed through the callback before
  4383. * flattening. The callback is bound to `thisArg` and invoked with three
  4384. * arguments; (value, index, array).
  4385. *
  4386. * If a property name is provided for `callback` the created "_.pluck" style
  4387. * callback will return the property value of the given element.
  4388. *
  4389. * If an object is provided for `callback` the created "_.where" style callback
  4390. * will return `true` for elements that have the properties of the given object,
  4391. * else `false`.
  4392. *
  4393. * @static
  4394. * @memberOf _
  4395. * @category Arrays
  4396. * @param {Array} array The array to flatten.
  4397. * @param {boolean} [isShallow=false] A flag to restrict flattening to a single level.
  4398. * @param {Function|Object|string} [callback=identity] The function called
  4399. * per iteration. If a property name or object is provided it will be used
  4400. * to create a "_.pluck" or "_.where" style callback, respectively.
  4401. * @param {*} [thisArg] The `this` binding of `callback`.
  4402. * @returns {Array} Returns a new flattened array.
  4403. * @example
  4404. *
  4405. * _.flatten([1, [2], [3, [[4]]]]);
  4406. * // => [1, 2, 3, 4];
  4407. *
  4408. * _.flatten([1, [2], [3, [[4]]]], true);
  4409. * // => [1, 2, 3, [[4]]];
  4410. *
  4411. * var characters = [
  4412. * { 'name': 'barney', 'age': 30, 'pets': ['hoppy'] },
  4413. * { 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }
  4414. * ];
  4415. *
  4416. * // using "_.pluck" callback shorthand
  4417. * _.flatten(characters, 'pets');
  4418. * // => ['hoppy', 'baby puss', 'dino']
  4419. */
  4420. function flatten(array, isShallow, callback, thisArg) {
  4421. // juggle arguments
  4422. if (typeof isShallow != 'boolean' && isShallow != null) {
  4423. thisArg = callback;
  4424. callback = (typeof isShallow != 'function' && thisArg && thisArg[isShallow] === array) ? null : isShallow;
  4425. isShallow = false;
  4426. }
  4427. if (callback != null) {
  4428. array = map(array, callback, thisArg);
  4429. }
  4430. return baseFlatten(array, isShallow);
  4431. }
  4432. /**
  4433. * Gets the index at which the first occurrence of `value` is found using
  4434. * strict equality for comparisons, i.e. `===`. If the array is already sorted
  4435. * providing `true` for `fromIndex` will run a faster binary search.
  4436. *
  4437. * @static
  4438. * @memberOf _
  4439. * @category Arrays
  4440. * @param {Array} array The array to search.
  4441. * @param {*} value The value to search for.
  4442. * @param {boolean|number} [fromIndex=0] The index to search from or `true`
  4443. * to perform a binary search on a sorted array.
  4444. * @returns {number} Returns the index of the matched value or `-1`.
  4445. * @example
  4446. *
  4447. * _.indexOf([1, 2, 3, 1, 2, 3], 2);
  4448. * // => 1
  4449. *
  4450. * _.indexOf([1, 2, 3, 1, 2, 3], 2, 3);
  4451. * // => 4
  4452. *
  4453. * _.indexOf([1, 1, 2, 2, 3, 3], 2, true);
  4454. * // => 2
  4455. */
  4456. function indexOf(array, value, fromIndex) {
  4457. if (typeof fromIndex == 'number') {
  4458. var length = array ? array.length : 0;
  4459. fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0);
  4460. } else if (fromIndex) {
  4461. var index = sortedIndex(array, value);
  4462. return array[index] === value ? index : -1;
  4463. }
  4464. return baseIndexOf(array, value, fromIndex);
  4465. }
  4466. /**
  4467. * Gets all but the last element or last `n` elements of an array. If a
  4468. * callback is provided elements at the end of the array are excluded from
  4469. * the result as long as the callback returns truey. The callback is bound
  4470. * to `thisArg` and invoked with three arguments; (value, index, array).
  4471. *
  4472. * If a property name is provided for `callback` the created "_.pluck" style
  4473. * callback will return the property value of the given element.
  4474. *
  4475. * If an object is provided for `callback` the created "_.where" style callback
  4476. * will return `true` for elements that have the properties of the given object,
  4477. * else `false`.
  4478. *
  4479. * @static
  4480. * @memberOf _
  4481. * @category Arrays
  4482. * @param {Array} array The array to query.
  4483. * @param {Function|Object|number|string} [callback=1] The function called
  4484. * per element or the number of elements to exclude. If a property name or
  4485. * object is provided it will be used to create a "_.pluck" or "_.where"
  4486. * style callback, respectively.
  4487. * @param {*} [thisArg] The `this` binding of `callback`.
  4488. * @returns {Array} Returns a slice of `array`.
  4489. * @example
  4490. *
  4491. * _.initial([1, 2, 3]);
  4492. * // => [1, 2]
  4493. *
  4494. * _.initial([1, 2, 3], 2);
  4495. * // => [1]
  4496. *
  4497. * _.initial([1, 2, 3], function(num) {
  4498. * return num > 1;
  4499. * });
  4500. * // => [1]
  4501. *
  4502. * var characters = [
  4503. * { 'name': 'barney', 'blocked': false, 'employer': 'slate' },
  4504. * { 'name': 'fred', 'blocked': true, 'employer': 'slate' },
  4505. * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
  4506. * ];
  4507. *
  4508. * // using "_.pluck" callback shorthand
  4509. * _.initial(characters, 'blocked');
  4510. * // => [{ 'name': 'barney', 'blocked': false, 'employer': 'slate' }]
  4511. *
  4512. * // using "_.where" callback shorthand
  4513. * _.pluck(_.initial(characters, { 'employer': 'na' }), 'name');
  4514. * // => ['barney', 'fred']
  4515. */
  4516. function initial(array, callback, thisArg) {
  4517. var n = 0,
  4518. length = array ? array.length : 0;
  4519. if (typeof callback != 'number' && callback != null) {
  4520. var index = length;
  4521. callback = lodash.createCallback(callback, thisArg, 3);
  4522. while (index-- && callback(array[index], index, array)) {
  4523. n++;
  4524. }
  4525. } else {
  4526. n = (callback == null || thisArg) ? 1 : callback || n;
  4527. }
  4528. return slice(array, 0, nativeMin(nativeMax(0, length - n), length));
  4529. }
  4530. /**
  4531. * Creates an array of unique values present in all provided arrays using
  4532. * strict equality for comparisons, i.e. `===`.
  4533. *
  4534. * @static
  4535. * @memberOf _
  4536. * @category Arrays
  4537. * @param {...Array} [array] The arrays to inspect.
  4538. * @returns {Array} Returns an array of shared values.
  4539. * @example
  4540. *
  4541. * _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);
  4542. * // => [1, 2]
  4543. */
  4544. function intersection() {
  4545. var args = [],
  4546. argsIndex = -1,
  4547. argsLength = arguments.length,
  4548. caches = getArray(),
  4549. indexOf = getIndexOf(),
  4550. trustIndexOf = indexOf === baseIndexOf,
  4551. seen = getArray();
  4552. while (++argsIndex < argsLength) {
  4553. var value = arguments[argsIndex];
  4554. if (isArray(value) || isArguments(value)) {
  4555. args.push(value);
  4556. caches.push(trustIndexOf && value.length >= largeArraySize &&
  4557. createCache(argsIndex ? args[argsIndex] : seen));
  4558. }
  4559. }
  4560. var array = args[0],
  4561. index = -1,
  4562. length = array ? array.length : 0,
  4563. result = [];
  4564. outer:
  4565. while (++index < length) {
  4566. var cache = caches[0];
  4567. value = array[index];
  4568. if ((cache ? cacheIndexOf(cache, value) : indexOf(seen, value)) < 0) {
  4569. argsIndex = argsLength;
  4570. (cache || seen).push(value);
  4571. while (--argsIndex) {
  4572. cache = caches[argsIndex];
  4573. if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value)) < 0) {
  4574. continue outer;
  4575. }
  4576. }
  4577. result.push(value);
  4578. }
  4579. }
  4580. while (argsLength--) {
  4581. cache = caches[argsLength];
  4582. if (cache) {
  4583. releaseObject(cache);
  4584. }
  4585. }
  4586. releaseArray(caches);
  4587. releaseArray(seen);
  4588. return result;
  4589. }
  4590. /**
  4591. * Gets the last element or last `n` elements of an array. If a callback is
  4592. * provided elements at the end of the array are returned as long as the
  4593. * callback returns truey. The callback is bound to `thisArg` and invoked
  4594. * with three arguments; (value, index, array).
  4595. *
  4596. * If a property name is provided for `callback` the created "_.pluck" style
  4597. * callback will return the property value of the given element.
  4598. *
  4599. * If an object is provided for `callback` the created "_.where" style callback
  4600. * will return `true` for elements that have the properties of the given object,
  4601. * else `false`.
  4602. *
  4603. * @static
  4604. * @memberOf _
  4605. * @category Arrays
  4606. * @param {Array} array The array to query.
  4607. * @param {Function|Object|number|string} [callback] The function called
  4608. * per element or the number of elements to return. If a property name or
  4609. * object is provided it will be used to create a "_.pluck" or "_.where"
  4610. * style callback, respectively.
  4611. * @param {*} [thisArg] The `this` binding of `callback`.
  4612. * @returns {*} Returns the last element(s) of `array`.
  4613. * @example
  4614. *
  4615. * _.last([1, 2, 3]);
  4616. * // => 3
  4617. *
  4618. * _.last([1, 2, 3], 2);
  4619. * // => [2, 3]
  4620. *
  4621. * _.last([1, 2, 3], function(num) {
  4622. * return num > 1;
  4623. * });
  4624. * // => [2, 3]
  4625. *
  4626. * var characters = [
  4627. * { 'name': 'barney', 'blocked': false, 'employer': 'slate' },
  4628. * { 'name': 'fred', 'blocked': true, 'employer': 'slate' },
  4629. * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
  4630. * ];
  4631. *
  4632. * // using "_.pluck" callback shorthand
  4633. * _.pluck(_.last(characters, 'blocked'), 'name');
  4634. * // => ['fred', 'pebbles']
  4635. *
  4636. * // using "_.where" callback shorthand
  4637. * _.last(characters, { 'employer': 'na' });
  4638. * // => [{ 'name': 'pebbles', 'blocked': true, 'employer': 'na' }]
  4639. */
  4640. function last(array, callback, thisArg) {
  4641. var n = 0,
  4642. length = array ? array.length : 0;
  4643. if (typeof callback != 'number' && callback != null) {
  4644. var index = length;
  4645. callback = lodash.createCallback(callback, thisArg, 3);
  4646. while (index-- && callback(array[index], index, array)) {
  4647. n++;
  4648. }
  4649. } else {
  4650. n = callback;
  4651. if (n == null || thisArg) {
  4652. return array ? array[length - 1] : undefined;
  4653. }
  4654. }
  4655. return slice(array, nativeMax(0, length - n));
  4656. }
  4657. /**
  4658. * Gets the index at which the last occurrence of `value` is found using strict
  4659. * equality for comparisons, i.e. `===`. If `fromIndex` is negative, it is used
  4660. * as the offset from the end of the collection.
  4661. *
  4662. * If a property name is provided for `callback` the created "_.pluck" style
  4663. * callback will return the property value of the given element.
  4664. *
  4665. * If an object is provided for `callback` the created "_.where" style callback
  4666. * will return `true` for elements that have the properties of the given object,
  4667. * else `false`.
  4668. *
  4669. * @static
  4670. * @memberOf _
  4671. * @category Arrays
  4672. * @param {Array} array The array to search.
  4673. * @param {*} value The value to search for.
  4674. * @param {number} [fromIndex=array.length-1] The index to search from.
  4675. * @returns {number} Returns the index of the matched value or `-1`.
  4676. * @example
  4677. *
  4678. * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  4679. * // => 4
  4680. *
  4681. * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);
  4682. * // => 1
  4683. */
  4684. function lastIndexOf(array, value, fromIndex) {
  4685. var index = array ? array.length : 0;
  4686. if (typeof fromIndex == 'number') {
  4687. index = (fromIndex < 0 ? nativeMax(0, index + fromIndex) : nativeMin(fromIndex, index - 1)) + 1;
  4688. }
  4689. while (index--) {
  4690. if (array[index] === value) {
  4691. return index;
  4692. }
  4693. }
  4694. return -1;
  4695. }
  4696. /**
  4697. * Removes all provided values from the given array using strict equality for
  4698. * comparisons, i.e. `===`.
  4699. *
  4700. * @static
  4701. * @memberOf _
  4702. * @category Arrays
  4703. * @param {Array} array The array to modify.
  4704. * @param {...*} [value] The values to remove.
  4705. * @returns {Array} Returns `array`.
  4706. * @example
  4707. *
  4708. * var array = [1, 2, 3, 1, 2, 3];
  4709. * _.pull(array, 2, 3);
  4710. * console.log(array);
  4711. * // => [1, 1]
  4712. */
  4713. function pull(array) {
  4714. var args = arguments,
  4715. argsIndex = 0,
  4716. argsLength = args.length,
  4717. length = array ? array.length : 0;
  4718. while (++argsIndex < argsLength) {
  4719. var index = -1,
  4720. value = args[argsIndex];
  4721. while (++index < length) {
  4722. if (array[index] === value) {
  4723. splice.call(array, index--, 1);
  4724. length--;
  4725. }
  4726. }
  4727. }
  4728. return array;
  4729. }
  4730. /**
  4731. * Creates an array of numbers (positive and/or negative) progressing from
  4732. * `start` up to but not including `end`. If `start` is less than `stop` a
  4733. * zero-length range is created unless a negative `step` is specified.
  4734. *
  4735. * @static
  4736. * @memberOf _
  4737. * @category Arrays
  4738. * @param {number} [start=0] The start of the range.
  4739. * @param {number} end The end of the range.
  4740. * @param {number} [step=1] The value to increment or decrement by.
  4741. * @returns {Array} Returns a new range array.
  4742. * @example
  4743. *
  4744. * _.range(4);
  4745. * // => [0, 1, 2, 3]
  4746. *
  4747. * _.range(1, 5);
  4748. * // => [1, 2, 3, 4]
  4749. *
  4750. * _.range(0, 20, 5);
  4751. * // => [0, 5, 10, 15]
  4752. *
  4753. * _.range(0, -4, -1);
  4754. * // => [0, -1, -2, -3]
  4755. *
  4756. * _.range(1, 4, 0);
  4757. * // => [1, 1, 1]
  4758. *
  4759. * _.range(0);
  4760. * // => []
  4761. */
  4762. function range(start, end, step) {
  4763. start = +start || 0;
  4764. step = typeof step == 'number' ? step : (+step || 1);
  4765. if (end == null) {
  4766. end = start;
  4767. start = 0;
  4768. }
  4769. // use `Array(length)` so engines like Chakra and V8 avoid slower modes
  4770. // http://youtu.be/XAqIpGU8ZZk#t=17m25s
  4771. var index = -1,
  4772. length = nativeMax(0, ceil((end - start) / (step || 1))),
  4773. result = Array(length);
  4774. while (++index < length) {
  4775. result[index] = start;
  4776. start += step;
  4777. }
  4778. return result;
  4779. }
  4780. /**
  4781. * Removes all elements from an array that the callback returns truey for
  4782. * and returns an array of removed elements. The callback is bound to `thisArg`
  4783. * and invoked with three arguments; (value, index, array).
  4784. *
  4785. * If a property name is provided for `callback` the created "_.pluck" style
  4786. * callback will return the property value of the given element.
  4787. *
  4788. * If an object is provided for `callback` the created "_.where" style callback
  4789. * will return `true` for elements that have the properties of the given object,
  4790. * else `false`.
  4791. *
  4792. * @static
  4793. * @memberOf _
  4794. * @category Arrays
  4795. * @param {Array} array The array to modify.
  4796. * @param {Function|Object|string} [callback=identity] The function called
  4797. * per iteration. If a property name or object is provided it will be used
  4798. * to create a "_.pluck" or "_.where" style callback, respectively.
  4799. * @param {*} [thisArg] The `this` binding of `callback`.
  4800. * @returns {Array} Returns a new array of removed elements.
  4801. * @example
  4802. *
  4803. * var array = [1, 2, 3, 4, 5, 6];
  4804. * var evens = _.remove(array, function(num) { return num % 2 == 0; });
  4805. *
  4806. * console.log(array);
  4807. * // => [1, 3, 5]
  4808. *
  4809. * console.log(evens);
  4810. * // => [2, 4, 6]
  4811. */
  4812. function remove(array, callback, thisArg) {
  4813. var index = -1,
  4814. length = array ? array.length : 0,
  4815. result = [];
  4816. callback = lodash.createCallback(callback, thisArg, 3);
  4817. while (++index < length) {
  4818. var value = array[index];
  4819. if (callback(value, index, array)) {
  4820. result.push(value);
  4821. splice.call(array, index--, 1);
  4822. length--;
  4823. }
  4824. }
  4825. return result;
  4826. }
  4827. /**
  4828. * The opposite of `_.initial` this method gets all but the first element or
  4829. * first `n` elements of an array. If a callback function is provided elements
  4830. * at the beginning of the array are excluded from the result as long as the
  4831. * callback returns truey. The callback is bound to `thisArg` and invoked
  4832. * with three arguments; (value, index, array).
  4833. *
  4834. * If a property name is provided for `callback` the created "_.pluck" style
  4835. * callback will return the property value of the given element.
  4836. *
  4837. * If an object is provided for `callback` the created "_.where" style callback
  4838. * will return `true` for elements that have the properties of the given object,
  4839. * else `false`.
  4840. *
  4841. * @static
  4842. * @memberOf _
  4843. * @alias drop, tail
  4844. * @category Arrays
  4845. * @param {Array} array The array to query.
  4846. * @param {Function|Object|number|string} [callback=1] The function called
  4847. * per element or the number of elements to exclude. If a property name or
  4848. * object is provided it will be used to create a "_.pluck" or "_.where"
  4849. * style callback, respectively.
  4850. * @param {*} [thisArg] The `this` binding of `callback`.
  4851. * @returns {Array} Returns a slice of `array`.
  4852. * @example
  4853. *
  4854. * _.rest([1, 2, 3]);
  4855. * // => [2, 3]
  4856. *
  4857. * _.rest([1, 2, 3], 2);
  4858. * // => [3]
  4859. *
  4860. * _.rest([1, 2, 3], function(num) {
  4861. * return num < 3;
  4862. * });
  4863. * // => [3]
  4864. *
  4865. * var characters = [
  4866. * { 'name': 'barney', 'blocked': true, 'employer': 'slate' },
  4867. * { 'name': 'fred', 'blocked': false, 'employer': 'slate' },
  4868. * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
  4869. * ];
  4870. *
  4871. * // using "_.pluck" callback shorthand
  4872. * _.pluck(_.rest(characters, 'blocked'), 'name');
  4873. * // => ['fred', 'pebbles']
  4874. *
  4875. * // using "_.where" callback shorthand
  4876. * _.rest(characters, { 'employer': 'slate' });
  4877. * // => [{ 'name': 'pebbles', 'blocked': true, 'employer': 'na' }]
  4878. */
  4879. function rest(array, callback, thisArg) {
  4880. if (typeof callback != 'number' && callback != null) {
  4881. var n = 0,
  4882. index = -1,
  4883. length = array ? array.length : 0;
  4884. callback = lodash.createCallback(callback, thisArg, 3);
  4885. while (++index < length && callback(array[index], index, array)) {
  4886. n++;
  4887. }
  4888. } else {
  4889. n = (callback == null || thisArg) ? 1 : nativeMax(0, callback);
  4890. }
  4891. return slice(array, n);
  4892. }
  4893. /**
  4894. * Uses a binary search to determine the smallest index at which a value
  4895. * should be inserted into a given sorted array in order to maintain the sort
  4896. * order of the array. If a callback is provided it will be executed for
  4897. * `value` and each element of `array` to compute their sort ranking. The
  4898. * callback is bound to `thisArg` and invoked with one argument; (value).
  4899. *
  4900. * If a property name is provided for `callback` the created "_.pluck" style
  4901. * callback will return the property value of the given element.
  4902. *
  4903. * If an object is provided for `callback` the created "_.where" style callback
  4904. * will return `true` for elements that have the properties of the given object,
  4905. * else `false`.
  4906. *
  4907. * @static
  4908. * @memberOf _
  4909. * @category Arrays
  4910. * @param {Array} array The array to inspect.
  4911. * @param {*} value The value to evaluate.
  4912. * @param {Function|Object|string} [callback=identity] The function called
  4913. * per iteration. If a property name or object is provided it will be used
  4914. * to create a "_.pluck" or "_.where" style callback, respectively.
  4915. * @param {*} [thisArg] The `this` binding of `callback`.
  4916. * @returns {number} Returns the index at which `value` should be inserted
  4917. * into `array`.
  4918. * @example
  4919. *
  4920. * _.sortedIndex([20, 30, 50], 40);
  4921. * // => 2
  4922. *
  4923. * // using "_.pluck" callback shorthand
  4924. * _.sortedIndex([{ 'x': 20 }, { 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
  4925. * // => 2
  4926. *
  4927. * var dict = {
  4928. * 'wordToNumber': { 'twenty': 20, 'thirty': 30, 'fourty': 40, 'fifty': 50 }
  4929. * };
  4930. *
  4931. * _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
  4932. * return dict.wordToNumber[word];
  4933. * });
  4934. * // => 2
  4935. *
  4936. * _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
  4937. * return this.wordToNumber[word];
  4938. * }, dict);
  4939. * // => 2
  4940. */
  4941. function sortedIndex(array, value, callback, thisArg) {
  4942. var low = 0,
  4943. high = array ? array.length : low;
  4944. // explicitly reference `identity` for better inlining in Firefox
  4945. callback = callback ? lodash.createCallback(callback, thisArg, 1) : identity;
  4946. value = callback(value);
  4947. while (low < high) {
  4948. var mid = (low + high) >>> 1;
  4949. (callback(array[mid]) < value)
  4950. ? low = mid + 1
  4951. : high = mid;
  4952. }
  4953. return low;
  4954. }
  4955. /**
  4956. * Creates an array of unique values, in order, of the provided arrays using
  4957. * strict equality for comparisons, i.e. `===`.
  4958. *
  4959. * @static
  4960. * @memberOf _
  4961. * @category Arrays
  4962. * @param {...Array} [array] The arrays to inspect.
  4963. * @returns {Array} Returns an array of combined values.
  4964. * @example
  4965. *
  4966. * _.union([1, 2, 3], [5, 2, 1, 4], [2, 1]);
  4967. * // => [1, 2, 3, 5, 4]
  4968. */
  4969. function union() {
  4970. return baseUniq(baseFlatten(arguments, true, true));
  4971. }
  4972. /**
  4973. * Creates a duplicate-value-free version of an array using strict equality
  4974. * for comparisons, i.e. `===`. If the array is sorted, providing
  4975. * `true` for `isSorted` will use a faster algorithm. If a callback is provided
  4976. * each element of `array` is passed through the callback before uniqueness
  4977. * is computed. The callback is bound to `thisArg` and invoked with three
  4978. * arguments; (value, index, array).
  4979. *
  4980. * If a property name is provided for `callback` the created "_.pluck" style
  4981. * callback will return the property value of the given element.
  4982. *
  4983. * If an object is provided for `callback` the created "_.where" style callback
  4984. * will return `true` for elements that have the properties of the given object,
  4985. * else `false`.
  4986. *
  4987. * @static
  4988. * @memberOf _
  4989. * @alias unique
  4990. * @category Arrays
  4991. * @param {Array} array The array to process.
  4992. * @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted.
  4993. * @param {Function|Object|string} [callback=identity] The function called
  4994. * per iteration. If a property name or object is provided it will be used
  4995. * to create a "_.pluck" or "_.where" style callback, respectively.
  4996. * @param {*} [thisArg] The `this` binding of `callback`.
  4997. * @returns {Array} Returns a duplicate-value-free array.
  4998. * @example
  4999. *
  5000. * _.uniq([1, 2, 1, 3, 1]);
  5001. * // => [1, 2, 3]
  5002. *
  5003. * _.uniq([1, 1, 2, 2, 3], true);
  5004. * // => [1, 2, 3]
  5005. *
  5006. * _.uniq(['A', 'b', 'C', 'a', 'B', 'c'], function(letter) { return letter.toLowerCase(); });
  5007. * // => ['A', 'b', 'C']
  5008. *
  5009. * _.uniq([1, 2.5, 3, 1.5, 2, 3.5], function(num) { return this.floor(num); }, Math);
  5010. * // => [1, 2.5, 3]
  5011. *
  5012. * // using "_.pluck" callback shorthand
  5013. * _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
  5014. * // => [{ 'x': 1 }, { 'x': 2 }]
  5015. */
  5016. function uniq(array, isSorted, callback, thisArg) {
  5017. // juggle arguments
  5018. if (typeof isSorted != 'boolean' && isSorted != null) {
  5019. thisArg = callback;
  5020. callback = (typeof isSorted != 'function' && thisArg && thisArg[isSorted] === array) ? null : isSorted;
  5021. isSorted = false;
  5022. }
  5023. if (callback != null) {
  5024. callback = lodash.createCallback(callback, thisArg, 3);
  5025. }
  5026. return baseUniq(array, isSorted, callback);
  5027. }
  5028. /**
  5029. * Creates an array excluding all provided values using strict equality for
  5030. * comparisons, i.e. `===`.
  5031. *
  5032. * @static
  5033. * @memberOf _
  5034. * @category Arrays
  5035. * @param {Array} array The array to filter.
  5036. * @param {...*} [value] The values to exclude.
  5037. * @returns {Array} Returns a new array of filtered values.
  5038. * @example
  5039. *
  5040. * _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  5041. * // => [2, 3, 4]
  5042. */
  5043. function without(array) {
  5044. return baseDifference(array, slice(arguments, 1));
  5045. }
  5046. /**
  5047. * Creates an array that is the symmetric difference of the provided arrays.
  5048. * See http://en.wikipedia.org/wiki/Symmetric_difference.
  5049. *
  5050. * @static
  5051. * @memberOf _
  5052. * @category Arrays
  5053. * @param {...Array} [array] The arrays to inspect.
  5054. * @returns {Array} Returns an array of values.
  5055. * @example
  5056. *
  5057. * _.xor([1, 2, 3], [5, 2, 1, 4]);
  5058. * // => [3, 5, 4]
  5059. *
  5060. * _.xor([1, 2, 5], [2, 3, 5], [3, 4, 5]);
  5061. * // => [1, 4, 5]
  5062. */
  5063. function xor() {
  5064. var index = -1,
  5065. length = arguments.length;
  5066. while (++index < length) {
  5067. var array = arguments[index];
  5068. if (isArray(array) || isArguments(array)) {
  5069. var result = result
  5070. ? baseUniq(baseDifference(result, array).concat(baseDifference(array, result)))
  5071. : array;
  5072. }
  5073. }
  5074. return result || [];
  5075. }
  5076. /**
  5077. * Creates an array of grouped elements, the first of which contains the first
  5078. * elements of the given arrays, the second of which contains the second
  5079. * elements of the given arrays, and so on.
  5080. *
  5081. * @static
  5082. * @memberOf _
  5083. * @alias unzip
  5084. * @category Arrays
  5085. * @param {...Array} [array] Arrays to process.
  5086. * @returns {Array} Returns a new array of grouped elements.
  5087. * @example
  5088. *
  5089. * _.zip(['fred', 'barney'], [30, 40], [true, false]);
  5090. * // => [['fred', 30, true], ['barney', 40, false]]
  5091. */
  5092. function zip() {
  5093. var array = arguments.length > 1 ? arguments : arguments[0],
  5094. index = -1,
  5095. length = array ? max(pluck(array, 'length')) : 0,
  5096. result = Array(length < 0 ? 0 : length);
  5097. while (++index < length) {
  5098. result[index] = pluck(array, index);
  5099. }
  5100. return result;
  5101. }
  5102. /**
  5103. * Creates an object composed from arrays of `keys` and `values`. Provide
  5104. * either a single two dimensional array, i.e. `[[key1, value1], [key2, value2]]`
  5105. * or two arrays, one of `keys` and one of corresponding `values`.
  5106. *
  5107. * @static
  5108. * @memberOf _
  5109. * @alias object
  5110. * @category Arrays
  5111. * @param {Array} keys The array of keys.
  5112. * @param {Array} [values=[]] The array of values.
  5113. * @returns {Object} Returns an object composed of the given keys and
  5114. * corresponding values.
  5115. * @example
  5116. *
  5117. * _.zipObject(['fred', 'barney'], [30, 40]);
  5118. * // => { 'fred': 30, 'barney': 40 }
  5119. */
  5120. function zipObject(keys, values) {
  5121. var index = -1,
  5122. length = keys ? keys.length : 0,
  5123. result = {};
  5124. if (!values && length && !isArray(keys[0])) {
  5125. values = [];
  5126. }
  5127. while (++index < length) {
  5128. var key = keys[index];
  5129. if (values) {
  5130. result[key] = values[index];
  5131. } else if (key) {
  5132. result[key[0]] = key[1];
  5133. }
  5134. }
  5135. return result;
  5136. }
  5137. /*--------------------------------------------------------------------------*/
  5138. /**
  5139. * Creates a function that executes `func`, with the `this` binding and
  5140. * arguments of the created function, only after being called `n` times.
  5141. *
  5142. * @static
  5143. * @memberOf _
  5144. * @category Functions
  5145. * @param {number} n The number of times the function must be called before
  5146. * `func` is executed.
  5147. * @param {Function} func The function to restrict.
  5148. * @returns {Function} Returns the new restricted function.
  5149. * @example
  5150. *
  5151. * var saves = ['profile', 'settings'];
  5152. *
  5153. * var done = _.after(saves.length, function() {
  5154. * console.log('Done saving!');
  5155. * });
  5156. *
  5157. * _.forEach(saves, function(type) {
  5158. * asyncSave({ 'type': type, 'complete': done });
  5159. * });
  5160. * // => logs 'Done saving!', after all saves have completed
  5161. */
  5162. function after(n, func) {
  5163. if (!isFunction(func)) {
  5164. throw new TypeError;
  5165. }
  5166. return function() {
  5167. if (--n < 1) {
  5168. return func.apply(this, arguments);
  5169. }
  5170. };
  5171. }
  5172. /**
  5173. * Creates a function that, when called, invokes `func` with the `this`
  5174. * binding of `thisArg` and prepends any additional `bind` arguments to those
  5175. * provided to the bound function.
  5176. *
  5177. * @static
  5178. * @memberOf _
  5179. * @category Functions
  5180. * @param {Function} func The function to bind.
  5181. * @param {*} [thisArg] The `this` binding of `func`.
  5182. * @param {...*} [arg] Arguments to be partially applied.
  5183. * @returns {Function} Returns the new bound function.
  5184. * @example
  5185. *
  5186. * var func = function(greeting) {
  5187. * return greeting + ' ' + this.name;
  5188. * };
  5189. *
  5190. * func = _.bind(func, { 'name': 'fred' }, 'hi');
  5191. * func();
  5192. * // => 'hi fred'
  5193. */
  5194. function bind(func, thisArg) {
  5195. return arguments.length > 2
  5196. ? createWrapper(func, 17, slice(arguments, 2), null, thisArg)
  5197. : createWrapper(func, 1, null, null, thisArg);
  5198. }
  5199. /**
  5200. * Binds methods of an object to the object itself, overwriting the existing
  5201. * method. Method names may be specified as individual arguments or as arrays
  5202. * of method names. If no method names are provided all the function properties
  5203. * of `object` will be bound.
  5204. *
  5205. * @static
  5206. * @memberOf _
  5207. * @category Functions
  5208. * @param {Object} object The object to bind and assign the bound methods to.
  5209. * @param {...string} [methodName] The object method names to
  5210. * bind, specified as individual method names or arrays of method names.
  5211. * @returns {Object} Returns `object`.
  5212. * @example
  5213. *
  5214. * var view = {
  5215. * 'label': 'docs',
  5216. * 'onClick': function() { console.log('clicked ' + this.label); }
  5217. * };
  5218. *
  5219. * _.bindAll(view);
  5220. * jQuery('#docs').on('click', view.onClick);
  5221. * // => logs 'clicked docs', when the button is clicked
  5222. */
  5223. function bindAll(object) {
  5224. var funcs = arguments.length > 1 ? baseFlatten(arguments, true, false, 1) : functions(object),
  5225. index = -1,
  5226. length = funcs.length;
  5227. while (++index < length) {
  5228. var key = funcs[index];
  5229. object[key] = createWrapper(object[key], 1, null, null, object);
  5230. }
  5231. return object;
  5232. }
  5233. /**
  5234. * Creates a function that, when called, invokes the method at `object[key]`
  5235. * and prepends any additional `bindKey` arguments to those provided to the bound
  5236. * function. This method differs from `_.bind` by allowing bound functions to
  5237. * reference methods that will be redefined or don't yet exist.
  5238. * See http://michaux.ca/articles/lazy-function-definition-pattern.
  5239. *
  5240. * @static
  5241. * @memberOf _
  5242. * @category Functions
  5243. * @param {Object} object The object the method belongs to.
  5244. * @param {string} key The key of the method.
  5245. * @param {...*} [arg] Arguments to be partially applied.
  5246. * @returns {Function} Returns the new bound function.
  5247. * @example
  5248. *
  5249. * var object = {
  5250. * 'name': 'fred',
  5251. * 'greet': function(greeting) {
  5252. * return greeting + ' ' + this.name;
  5253. * }
  5254. * };
  5255. *
  5256. * var func = _.bindKey(object, 'greet', 'hi');
  5257. * func();
  5258. * // => 'hi fred'
  5259. *
  5260. * object.greet = function(greeting) {
  5261. * return greeting + 'ya ' + this.name + '!';
  5262. * };
  5263. *
  5264. * func();
  5265. * // => 'hiya fred!'
  5266. */
  5267. function bindKey(object, key) {
  5268. return arguments.length > 2
  5269. ? createWrapper(key, 19, slice(arguments, 2), null, object)
  5270. : createWrapper(key, 3, null, null, object);
  5271. }
  5272. /**
  5273. * Creates a function that is the composition of the provided functions,
  5274. * where each function consumes the return value of the function that follows.
  5275. * For example, composing the functions `f()`, `g()`, and `h()` produces `f(g(h()))`.
  5276. * Each function is executed with the `this` binding of the composed function.
  5277. *
  5278. * @static
  5279. * @memberOf _
  5280. * @category Functions
  5281. * @param {...Function} [func] Functions to compose.
  5282. * @returns {Function} Returns the new composed function.
  5283. * @example
  5284. *
  5285. * var realNameMap = {
  5286. * 'pebbles': 'penelope'
  5287. * };
  5288. *
  5289. * var format = function(name) {
  5290. * name = realNameMap[name.toLowerCase()] || name;
  5291. * return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();
  5292. * };
  5293. *
  5294. * var greet = function(formatted) {
  5295. * return 'Hiya ' + formatted + '!';
  5296. * };
  5297. *
  5298. * var welcome = _.compose(greet, format);
  5299. * welcome('pebbles');
  5300. * // => 'Hiya Penelope!'
  5301. */
  5302. function compose() {
  5303. var funcs = arguments,
  5304. length = funcs.length;
  5305. while (length--) {
  5306. if (!isFunction(funcs[length])) {
  5307. throw new TypeError;
  5308. }
  5309. }
  5310. return function() {
  5311. var args = arguments,
  5312. length = funcs.length;
  5313. while (length--) {
  5314. args = [funcs[length].apply(this, args)];
  5315. }
  5316. return args[0];
  5317. };
  5318. }
  5319. /**
  5320. * Creates a function which accepts one or more arguments of `func` that when
  5321. * invoked either executes `func` returning its result, if all `func` arguments
  5322. * have been provided, or returns a function that accepts one or more of the
  5323. * remaining `func` arguments, and so on. The arity of `func` can be specified
  5324. * if `func.length` is not sufficient.
  5325. *
  5326. * @static
  5327. * @memberOf _
  5328. * @category Functions
  5329. * @param {Function} func The function to curry.
  5330. * @param {number} [arity=func.length] The arity of `func`.
  5331. * @returns {Function} Returns the new curried function.
  5332. * @example
  5333. *
  5334. * var curried = _.curry(function(a, b, c) {
  5335. * console.log(a + b + c);
  5336. * });
  5337. *
  5338. * curried(1)(2)(3);
  5339. * // => 6
  5340. *
  5341. * curried(1, 2)(3);
  5342. * // => 6
  5343. *
  5344. * curried(1, 2, 3);
  5345. * // => 6
  5346. */
  5347. function curry(func, arity) {
  5348. arity = typeof arity == 'number' ? arity : (+arity || func.length);
  5349. return createWrapper(func, 4, null, null, null, arity);
  5350. }
  5351. /**
  5352. * Creates a function that will delay the execution of `func` until after
  5353. * `wait` milliseconds have elapsed since the last time it was invoked.
  5354. * Provide an options object to indicate that `func` should be invoked on
  5355. * the leading and/or trailing edge of the `wait` timeout. Subsequent calls
  5356. * to the debounced function will return the result of the last `func` call.
  5357. *
  5358. * Note: If `leading` and `trailing` options are `true` `func` will be called
  5359. * on the trailing edge of the timeout only if the the debounced function is
  5360. * invoked more than once during the `wait` timeout.
  5361. *
  5362. * @static
  5363. * @memberOf _
  5364. * @category Functions
  5365. * @param {Function} func The function to debounce.
  5366. * @param {number} wait The number of milliseconds to delay.
  5367. * @param {Object} [options] The options object.
  5368. * @param {boolean} [options.leading=false] Specify execution on the leading edge of the timeout.
  5369. * @param {number} [options.maxWait] The maximum time `func` is allowed to be delayed before it's called.
  5370. * @param {boolean} [options.trailing=true] Specify execution on the trailing edge of the timeout.
  5371. * @returns {Function} Returns the new debounced function.
  5372. * @example
  5373. *
  5374. * // avoid costly calculations while the window size is in flux
  5375. * var lazyLayout = _.debounce(calculateLayout, 150);
  5376. * jQuery(window).on('resize', lazyLayout);
  5377. *
  5378. * // execute `sendMail` when the click event is fired, debouncing subsequent calls
  5379. * jQuery('#postbox').on('click', _.debounce(sendMail, 300, {
  5380. * 'leading': true,
  5381. * 'trailing': false
  5382. * });
  5383. *
  5384. * // ensure `batchLog` is executed once after 1 second of debounced calls
  5385. * var source = new EventSource('/stream');
  5386. * source.addEventListener('message', _.debounce(batchLog, 250, {
  5387. * 'maxWait': 1000
  5388. * }, false);
  5389. */
  5390. function debounce(func, wait, options) {
  5391. var args,
  5392. maxTimeoutId,
  5393. result,
  5394. stamp,
  5395. thisArg,
  5396. timeoutId,
  5397. trailingCall,
  5398. lastCalled = 0,
  5399. maxWait = false,
  5400. trailing = true;
  5401. if (!isFunction(func)) {
  5402. throw new TypeError;
  5403. }
  5404. wait = nativeMax(0, wait) || 0;
  5405. if (options === true) {
  5406. var leading = true;
  5407. trailing = false;
  5408. } else if (isObject(options)) {
  5409. leading = options.leading;
  5410. maxWait = 'maxWait' in options && (nativeMax(wait, options.maxWait) || 0);
  5411. trailing = 'trailing' in options ? options.trailing : trailing;
  5412. }
  5413. var delayed = function() {
  5414. var remaining = wait - (now() - stamp);
  5415. if (remaining <= 0) {
  5416. if (maxTimeoutId) {
  5417. clearTimeout(maxTimeoutId);
  5418. }
  5419. var isCalled = trailingCall;
  5420. maxTimeoutId = timeoutId = trailingCall = undefined;
  5421. if (isCalled) {
  5422. lastCalled = now();
  5423. result = func.apply(thisArg, args);
  5424. if (!timeoutId && !maxTimeoutId) {
  5425. args = thisArg = null;
  5426. }
  5427. }
  5428. } else {
  5429. timeoutId = setTimeout(delayed, remaining);
  5430. }
  5431. };
  5432. var maxDelayed = function() {
  5433. if (timeoutId) {
  5434. clearTimeout(timeoutId);
  5435. }
  5436. maxTimeoutId = timeoutId = trailingCall = undefined;
  5437. if (trailing || (maxWait !== wait)) {
  5438. lastCalled = now();
  5439. result = func.apply(thisArg, args);
  5440. if (!timeoutId && !maxTimeoutId) {
  5441. args = thisArg = null;
  5442. }
  5443. }
  5444. };
  5445. return function() {
  5446. args = arguments;
  5447. stamp = now();
  5448. thisArg = this;
  5449. trailingCall = trailing && (timeoutId || !leading);
  5450. if (maxWait === false) {
  5451. var leadingCall = leading && !timeoutId;
  5452. } else {
  5453. if (!maxTimeoutId && !leading) {
  5454. lastCalled = stamp;
  5455. }
  5456. var remaining = maxWait - (stamp - lastCalled),
  5457. isCalled = remaining <= 0;
  5458. if (isCalled) {
  5459. if (maxTimeoutId) {
  5460. maxTimeoutId = clearTimeout(maxTimeoutId);
  5461. }
  5462. lastCalled = stamp;
  5463. result = func.apply(thisArg, args);
  5464. }
  5465. else if (!maxTimeoutId) {
  5466. maxTimeoutId = setTimeout(maxDelayed, remaining);
  5467. }
  5468. }
  5469. if (isCalled && timeoutId) {
  5470. timeoutId = clearTimeout(timeoutId);
  5471. }
  5472. else if (!timeoutId && wait !== maxWait) {
  5473. timeoutId = setTimeout(delayed, wait);
  5474. }
  5475. if (leadingCall) {
  5476. isCalled = true;
  5477. result = func.apply(thisArg, args);
  5478. }
  5479. if (isCalled && !timeoutId && !maxTimeoutId) {
  5480. args = thisArg = null;
  5481. }
  5482. return result;
  5483. };
  5484. }
  5485. /**
  5486. * Defers executing the `func` function until the current call stack has cleared.
  5487. * Additional arguments will be provided to `func` when it is invoked.
  5488. *
  5489. * @static
  5490. * @memberOf _
  5491. * @category Functions
  5492. * @param {Function} func The function to defer.
  5493. * @param {...*} [arg] Arguments to invoke the function with.
  5494. * @returns {number} Returns the timer id.
  5495. * @example
  5496. *
  5497. * _.defer(function(text) { console.log(text); }, 'deferred');
  5498. * // logs 'deferred' after one or more milliseconds
  5499. */
  5500. function defer(func) {
  5501. if (!isFunction(func)) {
  5502. throw new TypeError;
  5503. }
  5504. var args = slice(arguments, 1);
  5505. return setTimeout(function() { func.apply(undefined, args); }, 1);
  5506. }
  5507. /**
  5508. * Executes the `func` function after `wait` milliseconds. Additional arguments
  5509. * will be provided to `func` when it is invoked.
  5510. *
  5511. * @static
  5512. * @memberOf _
  5513. * @category Functions
  5514. * @param {Function} func The function to delay.
  5515. * @param {number} wait The number of milliseconds to delay execution.
  5516. * @param {...*} [arg] Arguments to invoke the function with.
  5517. * @returns {number} Returns the timer id.
  5518. * @example
  5519. *
  5520. * _.delay(function(text) { console.log(text); }, 1000, 'later');
  5521. * // => logs 'later' after one second
  5522. */
  5523. function delay(func, wait) {
  5524. if (!isFunction(func)) {
  5525. throw new TypeError;
  5526. }
  5527. var args = slice(arguments, 2);
  5528. return setTimeout(function() { func.apply(undefined, args); }, wait);
  5529. }
  5530. /**
  5531. * Creates a function that memoizes the result of `func`. If `resolver` is
  5532. * provided it will be used to determine the cache key for storing the result
  5533. * based on the arguments provided to the memoized function. By default, the
  5534. * first argument provided to the memoized function is used as the cache key.
  5535. * The `func` is executed with the `this` binding of the memoized function.
  5536. * The result cache is exposed as the `cache` property on the memoized function.
  5537. *
  5538. * @static
  5539. * @memberOf _
  5540. * @category Functions
  5541. * @param {Function} func The function to have its output memoized.
  5542. * @param {Function} [resolver] A function used to resolve the cache key.
  5543. * @returns {Function} Returns the new memoizing function.
  5544. * @example
  5545. *
  5546. * var fibonacci = _.memoize(function(n) {
  5547. * return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
  5548. * });
  5549. *
  5550. * fibonacci(9)
  5551. * // => 34
  5552. *
  5553. * var data = {
  5554. * 'fred': { 'name': 'fred', 'age': 40 },
  5555. * 'pebbles': { 'name': 'pebbles', 'age': 1 }
  5556. * };
  5557. *
  5558. * // modifying the result cache
  5559. * var get = _.memoize(function(name) { return data[name]; }, _.identity);
  5560. * get('pebbles');
  5561. * // => { 'name': 'pebbles', 'age': 1 }
  5562. *
  5563. * get.cache.pebbles.name = 'penelope';
  5564. * get('pebbles');
  5565. * // => { 'name': 'penelope', 'age': 1 }
  5566. */
  5567. function memoize(func, resolver) {
  5568. if (!isFunction(func)) {
  5569. throw new TypeError;
  5570. }
  5571. var memoized = function() {
  5572. var cache = memoized.cache,
  5573. key = resolver ? resolver.apply(this, arguments) : keyPrefix + arguments[0];
  5574. return hasOwnProperty.call(cache, key)
  5575. ? cache[key]
  5576. : (cache[key] = func.apply(this, arguments));
  5577. }
  5578. memoized.cache = {};
  5579. return memoized;
  5580. }
  5581. /**
  5582. * Creates a function that is restricted to execute `func` once. Repeat calls to
  5583. * the function will return the value of the first call. The `func` is executed
  5584. * with the `this` binding of the created function.
  5585. *
  5586. * @static
  5587. * @memberOf _
  5588. * @category Functions
  5589. * @param {Function} func The function to restrict.
  5590. * @returns {Function} Returns the new restricted function.
  5591. * @example
  5592. *
  5593. * var initialize = _.once(createApplication);
  5594. * initialize();
  5595. * initialize();
  5596. * // `initialize` executes `createApplication` once
  5597. */
  5598. function once(func) {
  5599. var ran,
  5600. result;
  5601. if (!isFunction(func)) {
  5602. throw new TypeError;
  5603. }
  5604. return function() {
  5605. if (ran) {
  5606. return result;
  5607. }
  5608. ran = true;
  5609. result = func.apply(this, arguments);
  5610. // clear the `func` variable so the function may be garbage collected
  5611. func = null;
  5612. return result;
  5613. };
  5614. }
  5615. /**
  5616. * Creates a function that, when called, invokes `func` with any additional
  5617. * `partial` arguments prepended to those provided to the new function. This
  5618. * method is similar to `_.bind` except it does **not** alter the `this` binding.
  5619. *
  5620. * @static
  5621. * @memberOf _
  5622. * @category Functions
  5623. * @param {Function} func The function to partially apply arguments to.
  5624. * @param {...*} [arg] Arguments to be partially applied.
  5625. * @returns {Function} Returns the new partially applied function.
  5626. * @example
  5627. *
  5628. * var greet = function(greeting, name) { return greeting + ' ' + name; };
  5629. * var hi = _.partial(greet, 'hi');
  5630. * hi('fred');
  5631. * // => 'hi fred'
  5632. */
  5633. function partial(func) {
  5634. return createWrapper(func, 16, slice(arguments, 1));
  5635. }
  5636. /**
  5637. * This method is like `_.partial` except that `partial` arguments are
  5638. * appended to those provided to the new function.
  5639. *
  5640. * @static
  5641. * @memberOf _
  5642. * @category Functions
  5643. * @param {Function} func The function to partially apply arguments to.
  5644. * @param {...*} [arg] Arguments to be partially applied.
  5645. * @returns {Function} Returns the new partially applied function.
  5646. * @example
  5647. *
  5648. * var defaultsDeep = _.partialRight(_.merge, _.defaults);
  5649. *
  5650. * var options = {
  5651. * 'variable': 'data',
  5652. * 'imports': { 'jq': $ }
  5653. * };
  5654. *
  5655. * defaultsDeep(options, _.templateSettings);
  5656. *
  5657. * options.variable
  5658. * // => 'data'
  5659. *
  5660. * options.imports
  5661. * // => { '_': _, 'jq': $ }
  5662. */
  5663. function partialRight(func) {
  5664. return createWrapper(func, 32, null, slice(arguments, 1));
  5665. }
  5666. /**
  5667. * Creates a function that, when executed, will only call the `func` function
  5668. * at most once per every `wait` milliseconds. Provide an options object to
  5669. * indicate that `func` should be invoked on the leading and/or trailing edge
  5670. * of the `wait` timeout. Subsequent calls to the throttled function will
  5671. * return the result of the last `func` call.
  5672. *
  5673. * Note: If `leading` and `trailing` options are `true` `func` will be called
  5674. * on the trailing edge of the timeout only if the the throttled function is
  5675. * invoked more than once during the `wait` timeout.
  5676. *
  5677. * @static
  5678. * @memberOf _
  5679. * @category Functions
  5680. * @param {Function} func The function to throttle.
  5681. * @param {number} wait The number of milliseconds to throttle executions to.
  5682. * @param {Object} [options] The options object.
  5683. * @param {boolean} [options.leading=true] Specify execution on the leading edge of the timeout.
  5684. * @param {boolean} [options.trailing=true] Specify execution on the trailing edge of the timeout.
  5685. * @returns {Function} Returns the new throttled function.
  5686. * @example
  5687. *
  5688. * // avoid excessively updating the position while scrolling
  5689. * var throttled = _.throttle(updatePosition, 100);
  5690. * jQuery(window).on('scroll', throttled);
  5691. *
  5692. * // execute `renewToken` when the click event is fired, but not more than once every 5 minutes
  5693. * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {
  5694. * 'trailing': false
  5695. * }));
  5696. */
  5697. function throttle(func, wait, options) {
  5698. var leading = true,
  5699. trailing = true;
  5700. if (!isFunction(func)) {
  5701. throw new TypeError;
  5702. }
  5703. if (options === false) {
  5704. leading = false;
  5705. } else if (isObject(options)) {
  5706. leading = 'leading' in options ? options.leading : leading;
  5707. trailing = 'trailing' in options ? options.trailing : trailing;
  5708. }
  5709. debounceOptions.leading = leading;
  5710. debounceOptions.maxWait = wait;
  5711. debounceOptions.trailing = trailing;
  5712. return debounce(func, wait, debounceOptions);
  5713. }
  5714. /**
  5715. * Creates a function that provides `value` to the wrapper function as its
  5716. * first argument. Additional arguments provided to the function are appended
  5717. * to those provided to the wrapper function. The wrapper is executed with
  5718. * the `this` binding of the created function.
  5719. *
  5720. * @static
  5721. * @memberOf _
  5722. * @category Functions
  5723. * @param {*} value The value to wrap.
  5724. * @param {Function} wrapper The wrapper function.
  5725. * @returns {Function} Returns the new function.
  5726. * @example
  5727. *
  5728. * var p = _.wrap(_.escape, function(func, text) {
  5729. * return '<p>' + func(text) + '</p>';
  5730. * });
  5731. *
  5732. * p('Fred, Wilma, & Pebbles');
  5733. * // => '<p>Fred, Wilma, &amp; Pebbles</p>'
  5734. */
  5735. function wrap(value, wrapper) {
  5736. return createWrapper(wrapper, 16, [value]);
  5737. }
  5738. /*--------------------------------------------------------------------------*/
  5739. /**
  5740. * Creates a function that returns `value`.
  5741. *
  5742. * @static
  5743. * @memberOf _
  5744. * @category Utilities
  5745. * @param {*} value The value to return from the new function.
  5746. * @returns {Function} Returns the new function.
  5747. * @example
  5748. *
  5749. * var object = { 'name': 'fred' };
  5750. * var getter = _.constant(object);
  5751. * getter() === object;
  5752. * // => true
  5753. */
  5754. function constant(value) {
  5755. return function() {
  5756. return value;
  5757. };
  5758. }
  5759. /**
  5760. * Produces a callback bound to an optional `thisArg`. If `func` is a property
  5761. * name the created callback will return the property value for a given element.
  5762. * If `func` is an object the created callback will return `true` for elements
  5763. * that contain the equivalent object properties, otherwise it will return `false`.
  5764. *
  5765. * @static
  5766. * @memberOf _
  5767. * @category Utilities
  5768. * @param {*} [func=identity] The value to convert to a callback.
  5769. * @param {*} [thisArg] The `this` binding of the created callback.
  5770. * @param {number} [argCount] The number of arguments the callback accepts.
  5771. * @returns {Function} Returns a callback function.
  5772. * @example
  5773. *
  5774. * var characters = [
  5775. * { 'name': 'barney', 'age': 36 },
  5776. * { 'name': 'fred', 'age': 40 }
  5777. * ];
  5778. *
  5779. * // wrap to create custom callback shorthands
  5780. * _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) {
  5781. * var match = /^(.+?)__([gl]t)(.+)$/.exec(callback);
  5782. * return !match ? func(callback, thisArg) : function(object) {
  5783. * return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3];
  5784. * };
  5785. * });
  5786. *
  5787. * _.filter(characters, 'age__gt38');
  5788. * // => [{ 'name': 'fred', 'age': 40 }]
  5789. */
  5790. function createCallback(func, thisArg, argCount) {
  5791. var type = typeof func;
  5792. if (func == null || type == 'function') {
  5793. return baseCreateCallback(func, thisArg, argCount);
  5794. }
  5795. // handle "_.pluck" style callback shorthands
  5796. if (type != 'object') {
  5797. return property(func);
  5798. }
  5799. var props = keys(func),
  5800. key = props[0],
  5801. a = func[key];
  5802. // handle "_.where" style callback shorthands
  5803. if (props.length == 1 && a === a && !isObject(a)) {
  5804. // fast path the common case of providing an object with a single
  5805. // property containing a primitive value
  5806. return function(object) {
  5807. var b = object[key];
  5808. return a === b && (a !== 0 || (1 / a == 1 / b));
  5809. };
  5810. }
  5811. return function(object) {
  5812. var length = props.length,
  5813. result = false;
  5814. while (length--) {
  5815. if (!(result = baseIsEqual(object[props[length]], func[props[length]], null, true))) {
  5816. break;
  5817. }
  5818. }
  5819. return result;
  5820. };
  5821. }
  5822. /**
  5823. * Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their
  5824. * corresponding HTML entities.
  5825. *
  5826. * @static
  5827. * @memberOf _
  5828. * @category Utilities
  5829. * @param {string} string The string to escape.
  5830. * @returns {string} Returns the escaped string.
  5831. * @example
  5832. *
  5833. * _.escape('Fred, Wilma, & Pebbles');
  5834. * // => 'Fred, Wilma, &amp; Pebbles'
  5835. */
  5836. function escape(string) {
  5837. return string == null ? '' : String(string).replace(reUnescapedHtml, escapeHtmlChar);
  5838. }
  5839. /**
  5840. * This method returns the first argument provided to it.
  5841. *
  5842. * @static
  5843. * @memberOf _
  5844. * @category Utilities
  5845. * @param {*} value Any value.
  5846. * @returns {*} Returns `value`.
  5847. * @example
  5848. *
  5849. * var object = { 'name': 'fred' };
  5850. * _.identity(object) === object;
  5851. * // => true
  5852. */
  5853. function identity(value) {
  5854. return value;
  5855. }
  5856. /**
  5857. * Adds function properties of a source object to the destination object.
  5858. * If `object` is a function methods will be added to its prototype as well.
  5859. *
  5860. * @static
  5861. * @memberOf _
  5862. * @category Utilities
  5863. * @param {Function|Object} [object=lodash] object The destination object.
  5864. * @param {Object} source The object of functions to add.
  5865. * @param {Object} [options] The options object.
  5866. * @param {boolean} [options.chain=true] Specify whether the functions added are chainable.
  5867. * @example
  5868. *
  5869. * function capitalize(string) {
  5870. * return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
  5871. * }
  5872. *
  5873. * _.mixin({ 'capitalize': capitalize });
  5874. * _.capitalize('fred');
  5875. * // => 'Fred'
  5876. *
  5877. * _('fred').capitalize().value();
  5878. * // => 'Fred'
  5879. *
  5880. * _.mixin({ 'capitalize': capitalize }, { 'chain': false });
  5881. * _('fred').capitalize();
  5882. * // => 'Fred'
  5883. */
  5884. function mixin(object, source, options) {
  5885. var chain = true,
  5886. methodNames = source && functions(source);
  5887. if (!source || (!options && !methodNames.length)) {
  5888. if (options == null) {
  5889. options = source;
  5890. }
  5891. ctor = lodashWrapper;
  5892. source = object;
  5893. object = lodash;
  5894. methodNames = functions(source);
  5895. }
  5896. if (options === false) {
  5897. chain = false;
  5898. } else if (isObject(options) && 'chain' in options) {
  5899. chain = options.chain;
  5900. }
  5901. var ctor = object,
  5902. isFunc = isFunction(ctor);
  5903. forEach(methodNames, function(methodName) {
  5904. var func = object[methodName] = source[methodName];
  5905. if (isFunc) {
  5906. ctor.prototype[methodName] = function() {
  5907. var chainAll = this.__chain__,
  5908. value = this.__wrapped__,
  5909. args = [value];
  5910. push.apply(args, arguments);
  5911. var result = func.apply(object, args);
  5912. if (chain || chainAll) {
  5913. if (value === result && isObject(result)) {
  5914. return this;
  5915. }
  5916. result = new ctor(result);
  5917. result.__chain__ = chainAll;
  5918. }
  5919. return result;
  5920. };
  5921. }
  5922. });
  5923. }
  5924. /**
  5925. * Reverts the '_' variable to its previous value and returns a reference to
  5926. * the `lodash` function.
  5927. *
  5928. * @static
  5929. * @memberOf _
  5930. * @category Utilities
  5931. * @returns {Function} Returns the `lodash` function.
  5932. * @example
  5933. *
  5934. * var lodash = _.noConflict();
  5935. */
  5936. function noConflict() {
  5937. context._ = oldDash;
  5938. return this;
  5939. }
  5940. /**
  5941. * A no-operation function.
  5942. *
  5943. * @static
  5944. * @memberOf _
  5945. * @category Utilities
  5946. * @example
  5947. *
  5948. * var object = { 'name': 'fred' };
  5949. * _.noop(object) === undefined;
  5950. * // => true
  5951. */
  5952. function noop() {
  5953. // no operation performed
  5954. }
  5955. /**
  5956. * Gets the number of milliseconds that have elapsed since the Unix epoch
  5957. * (1 January 1970 00:00:00 UTC).
  5958. *
  5959. * @static
  5960. * @memberOf _
  5961. * @category Utilities
  5962. * @example
  5963. *
  5964. * var stamp = _.now();
  5965. * _.defer(function() { console.log(_.now() - stamp); });
  5966. * // => logs the number of milliseconds it took for the deferred function to be called
  5967. */
  5968. var now = isNative(now = Date.now) && now || function() {
  5969. return new Date().getTime();
  5970. };
  5971. /**
  5972. * Converts the given value into an integer of the specified radix.
  5973. * If `radix` is `undefined` or `0` a `radix` of `10` is used unless the
  5974. * `value` is a hexadecimal, in which case a `radix` of `16` is used.
  5975. *
  5976. * Note: This method avoids differences in native ES3 and ES5 `parseInt`
  5977. * implementations. See http://es5.github.io/#E.
  5978. *
  5979. * @static
  5980. * @memberOf _
  5981. * @category Utilities
  5982. * @param {string} value The value to parse.
  5983. * @param {number} [radix] The radix used to interpret the value to parse.
  5984. * @returns {number} Returns the new integer value.
  5985. * @example
  5986. *
  5987. * _.parseInt('08');
  5988. * // => 8
  5989. */
  5990. var parseInt = nativeParseInt(whitespace + '08') == 8 ? nativeParseInt : function(value, radix) {
  5991. // Firefox < 21 and Opera < 15 follow the ES3 specified implementation of `parseInt`
  5992. return nativeParseInt(isString(value) ? value.replace(reLeadingSpacesAndZeros, '') : value, radix || 0);
  5993. };
  5994. /**
  5995. * Creates a "_.pluck" style function, which returns the `key` value of a
  5996. * given object.
  5997. *
  5998. * @static
  5999. * @memberOf _
  6000. * @category Utilities
  6001. * @param {string} key The name of the property to retrieve.
  6002. * @returns {Function} Returns the new function.
  6003. * @example
  6004. *
  6005. * var characters = [
  6006. * { 'name': 'fred', 'age': 40 },
  6007. * { 'name': 'barney', 'age': 36 }
  6008. * ];
  6009. *
  6010. * var getName = _.property('name');
  6011. *
  6012. * _.map(characters, getName);
  6013. * // => ['barney', 'fred']
  6014. *
  6015. * _.sortBy(characters, getName);
  6016. * // => [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }]
  6017. */
  6018. function property(key) {
  6019. return function(object) {
  6020. return object[key];
  6021. };
  6022. }
  6023. /**
  6024. * Produces a random number between `min` and `max` (inclusive). If only one
  6025. * argument is provided a number between `0` and the given number will be
  6026. * returned. If `floating` is truey or either `min` or `max` are floats a
  6027. * floating-point number will be returned instead of an integer.
  6028. *
  6029. * @static
  6030. * @memberOf _
  6031. * @category Utilities
  6032. * @param {number} [min=0] The minimum possible value.
  6033. * @param {number} [max=1] The maximum possible value.
  6034. * @param {boolean} [floating=false] Specify returning a floating-point number.
  6035. * @returns {number} Returns a random number.
  6036. * @example
  6037. *
  6038. * _.random(0, 5);
  6039. * // => an integer between 0 and 5
  6040. *
  6041. * _.random(5);
  6042. * // => also an integer between 0 and 5
  6043. *
  6044. * _.random(5, true);
  6045. * // => a floating-point number between 0 and 5
  6046. *
  6047. * _.random(1.2, 5.2);
  6048. * // => a floating-point number between 1.2 and 5.2
  6049. */
  6050. function random(min, max, floating) {
  6051. var noMin = min == null,
  6052. noMax = max == null;
  6053. if (floating == null) {
  6054. if (typeof min == 'boolean' && noMax) {
  6055. floating = min;
  6056. min = 1;
  6057. }
  6058. else if (!noMax && typeof max == 'boolean') {
  6059. floating = max;
  6060. noMax = true;
  6061. }
  6062. }
  6063. if (noMin && noMax) {
  6064. max = 1;
  6065. }
  6066. min = +min || 0;
  6067. if (noMax) {
  6068. max = min;
  6069. min = 0;
  6070. } else {
  6071. max = +max || 0;
  6072. }
  6073. if (floating || min % 1 || max % 1) {
  6074. var rand = nativeRandom();
  6075. return nativeMin(min + (rand * (max - min + parseFloat('1e-' + ((rand +'').length - 1)))), max);
  6076. }
  6077. return baseRandom(min, max);
  6078. }
  6079. /**
  6080. * Resolves the value of property `key` on `object`. If `key` is a function
  6081. * it will be invoked with the `this` binding of `object` and its result returned,
  6082. * else the property value is returned. If `object` is falsey then `undefined`
  6083. * is returned.
  6084. *
  6085. * @static
  6086. * @memberOf _
  6087. * @category Utilities
  6088. * @param {Object} object The object to inspect.
  6089. * @param {string} key The name of the property to resolve.
  6090. * @returns {*} Returns the resolved value.
  6091. * @example
  6092. *
  6093. * var object = {
  6094. * 'cheese': 'crumpets',
  6095. * 'stuff': function() {
  6096. * return 'nonsense';
  6097. * }
  6098. * };
  6099. *
  6100. * _.result(object, 'cheese');
  6101. * // => 'crumpets'
  6102. *
  6103. * _.result(object, 'stuff');
  6104. * // => 'nonsense'
  6105. */
  6106. function result(object, key) {
  6107. if (object) {
  6108. var value = object[key];
  6109. return isFunction(value) ? object[key]() : value;
  6110. }
  6111. }
  6112. /**
  6113. * A micro-templating method that handles arbitrary delimiters, preserves
  6114. * whitespace, and correctly escapes quotes within interpolated code.
  6115. *
  6116. * Note: In the development build, `_.template` utilizes sourceURLs for easier
  6117. * debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
  6118. *
  6119. * For more information on precompiling templates see:
  6120. * https://lodash.com/custom-builds
  6121. *
  6122. * For more information on Chrome extension sandboxes see:
  6123. * http://developer.chrome.com/stable/extensions/sandboxingEval.html
  6124. *
  6125. * @static
  6126. * @memberOf _
  6127. * @category Utilities
  6128. * @param {string} text The template text.
  6129. * @param {Object} data The data object used to populate the text.
  6130. * @param {Object} [options] The options object.
  6131. * @param {RegExp} [options.escape] The "escape" delimiter.
  6132. * @param {RegExp} [options.evaluate] The "evaluate" delimiter.
  6133. * @param {Object} [options.imports] An object to import into the template as local variables.
  6134. * @param {RegExp} [options.interpolate] The "interpolate" delimiter.
  6135. * @param {string} [sourceURL] The sourceURL of the template's compiled source.
  6136. * @param {string} [variable] The data object variable name.
  6137. * @returns {Function|string} Returns a compiled function when no `data` object
  6138. * is given, else it returns the interpolated text.
  6139. * @example
  6140. *
  6141. * // using the "interpolate" delimiter to create a compiled template
  6142. * var compiled = _.template('hello <%= name %>');
  6143. * compiled({ 'name': 'fred' });
  6144. * // => 'hello fred'
  6145. *
  6146. * // using the "escape" delimiter to escape HTML in data property values
  6147. * _.template('<b><%- value %></b>', { 'value': '<script>' });
  6148. * // => '<b>&lt;script&gt;</b>'
  6149. *
  6150. * // using the "evaluate" delimiter to generate HTML
  6151. * var list = '<% _.forEach(people, function(name) { %><li><%- name %></li><% }); %>';
  6152. * _.template(list, { 'people': ['fred', 'barney'] });
  6153. * // => '<li>fred</li><li>barney</li>'
  6154. *
  6155. * // using the ES6 delimiter as an alternative to the default "interpolate" delimiter
  6156. * _.template('hello ${ name }', { 'name': 'pebbles' });
  6157. * // => 'hello pebbles'
  6158. *
  6159. * // using the internal `print` function in "evaluate" delimiters
  6160. * _.template('<% print("hello " + name); %>!', { 'name': 'barney' });
  6161. * // => 'hello barney!'
  6162. *
  6163. * // using a custom template delimiters
  6164. * _.templateSettings = {
  6165. * 'interpolate': /{{([\s\S]+?)}}/g
  6166. * };
  6167. *
  6168. * _.template('hello {{ name }}!', { 'name': 'mustache' });
  6169. * // => 'hello mustache!'
  6170. *
  6171. * // using the `imports` option to import jQuery
  6172. * var list = '<% jq.each(people, function(name) { %><li><%- name %></li><% }); %>';
  6173. * _.template(list, { 'people': ['fred', 'barney'] }, { 'imports': { 'jq': jQuery } });
  6174. * // => '<li>fred</li><li>barney</li>'
  6175. *
  6176. * // using the `sourceURL` option to specify a custom sourceURL for the template
  6177. * var compiled = _.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' });
  6178. * compiled(data);
  6179. * // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector
  6180. *
  6181. * // using the `variable` option to ensure a with-statement isn't used in the compiled template
  6182. * var compiled = _.template('hi <%= data.name %>!', null, { 'variable': 'data' });
  6183. * compiled.source;
  6184. * // => function(data) {
  6185. * var __t, __p = '', __e = _.escape;
  6186. * __p += 'hi ' + ((__t = ( data.name )) == null ? '' : __t) + '!';
  6187. * return __p;
  6188. * }
  6189. *
  6190. * // using the `source` property to inline compiled templates for meaningful
  6191. * // line numbers in error messages and a stack trace
  6192. * fs.writeFileSync(path.join(cwd, 'jst.js'), '\
  6193. * var JST = {\
  6194. * "main": ' + _.template(mainText).source + '\
  6195. * };\
  6196. * ');
  6197. */
  6198. function template(text, data, options) {
  6199. // based on John Resig's `tmpl` implementation
  6200. // http://ejohn.org/blog/javascript-micro-templating/
  6201. // and Laura Doktorova's doT.js
  6202. // https://github.com/olado/doT
  6203. var settings = lodash.templateSettings;
  6204. text = String(text || '');
  6205. // avoid missing dependencies when `iteratorTemplate` is not defined
  6206. options = defaults({}, options, settings);
  6207. var imports = defaults({}, options.imports, settings.imports),
  6208. importsKeys = keys(imports),
  6209. importsValues = values(imports);
  6210. var isEvaluating,
  6211. index = 0,
  6212. interpolate = options.interpolate || reNoMatch,
  6213. source = "__p += '";
  6214. // compile the regexp to match each delimiter
  6215. var reDelimiters = RegExp(
  6216. (options.escape || reNoMatch).source + '|' +
  6217. interpolate.source + '|' +
  6218. (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
  6219. (options.evaluate || reNoMatch).source + '|$'
  6220. , 'g');
  6221. text.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
  6222. interpolateValue || (interpolateValue = esTemplateValue);
  6223. // escape characters that cannot be included in string literals
  6224. source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar);
  6225. // replace delimiters with snippets
  6226. if (escapeValue) {
  6227. source += "' +\n__e(" + escapeValue + ") +\n'";
  6228. }
  6229. if (evaluateValue) {
  6230. isEvaluating = true;
  6231. source += "';\n" + evaluateValue + ";\n__p += '";
  6232. }
  6233. if (interpolateValue) {
  6234. source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
  6235. }
  6236. index = offset + match.length;
  6237. // the JS engine embedded in Adobe products requires returning the `match`
  6238. // string in order to produce the correct `offset` value
  6239. return match;
  6240. });
  6241. source += "';\n";
  6242. // if `variable` is not specified, wrap a with-statement around the generated
  6243. // code to add the data object to the top of the scope chain
  6244. var variable = options.variable,
  6245. hasVariable = variable;
  6246. if (!hasVariable) {
  6247. variable = 'obj';
  6248. source = 'with (' + variable + ') {\n' + source + '\n}\n';
  6249. }
  6250. // cleanup code by stripping empty strings
  6251. source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
  6252. .replace(reEmptyStringMiddle, '$1')
  6253. .replace(reEmptyStringTrailing, '$1;');
  6254. // frame code as the function body
  6255. source = 'function(' + variable + ') {\n' +
  6256. (hasVariable ? '' : variable + ' || (' + variable + ' = {});\n') +
  6257. "var __t, __p = '', __e = _.escape" +
  6258. (isEvaluating
  6259. ? ', __j = Array.prototype.join;\n' +
  6260. "function print() { __p += __j.call(arguments, '') }\n"
  6261. : ';\n'
  6262. ) +
  6263. source +
  6264. 'return __p\n}';
  6265. // Use a sourceURL for easier debugging.
  6266. // http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
  6267. var sourceURL = '\n/*\n//# sourceURL=' + (options.sourceURL || '/lodash/template/source[' + (templateCounter++) + ']') + '\n*/';
  6268. try {
  6269. var result = Function(importsKeys, 'return ' + source + sourceURL).apply(undefined, importsValues);
  6270. } catch(e) {
  6271. e.source = source;
  6272. throw e;
  6273. }
  6274. if (data) {
  6275. return result(data);
  6276. }
  6277. // provide the compiled function's source by its `toString` method, in
  6278. // supported environments, or the `source` property as a convenience for
  6279. // inlining compiled templates during the build process
  6280. result.source = source;
  6281. return result;
  6282. }
  6283. /**
  6284. * Executes the callback `n` times, returning an array of the results
  6285. * of each callback execution. The callback is bound to `thisArg` and invoked
  6286. * with one argument; (index).
  6287. *
  6288. * @static
  6289. * @memberOf _
  6290. * @category Utilities
  6291. * @param {number} n The number of times to execute the callback.
  6292. * @param {Function} callback The function called per iteration.
  6293. * @param {*} [thisArg] The `this` binding of `callback`.
  6294. * @returns {Array} Returns an array of the results of each `callback` execution.
  6295. * @example
  6296. *
  6297. * var diceRolls = _.times(3, _.partial(_.random, 1, 6));
  6298. * // => [3, 6, 4]
  6299. *
  6300. * _.times(3, function(n) { mage.castSpell(n); });
  6301. * // => calls `mage.castSpell(n)` three times, passing `n` of `0`, `1`, and `2` respectively
  6302. *
  6303. * _.times(3, function(n) { this.cast(n); }, mage);
  6304. * // => also calls `mage.castSpell(n)` three times
  6305. */
  6306. function times(n, callback, thisArg) {
  6307. n = (n = +n) > -1 ? n : 0;
  6308. var index = -1,
  6309. result = Array(n);
  6310. callback = baseCreateCallback(callback, thisArg, 1);
  6311. while (++index < n) {
  6312. result[index] = callback(index);
  6313. }
  6314. return result;
  6315. }
  6316. /**
  6317. * The inverse of `_.escape` this method converts the HTML entities
  6318. * `&amp;`, `&lt;`, `&gt;`, `&quot;`, and `&#39;` in `string` to their
  6319. * corresponding characters.
  6320. *
  6321. * @static
  6322. * @memberOf _
  6323. * @category Utilities
  6324. * @param {string} string The string to unescape.
  6325. * @returns {string} Returns the unescaped string.
  6326. * @example
  6327. *
  6328. * _.unescape('Fred, Barney &amp; Pebbles');
  6329. * // => 'Fred, Barney & Pebbles'
  6330. */
  6331. function unescape(string) {
  6332. return string == null ? '' : String(string).replace(reEscapedHtml, unescapeHtmlChar);
  6333. }
  6334. /**
  6335. * Generates a unique ID. If `prefix` is provided the ID will be appended to it.
  6336. *
  6337. * @static
  6338. * @memberOf _
  6339. * @category Utilities
  6340. * @param {string} [prefix] The value to prefix the ID with.
  6341. * @returns {string} Returns the unique ID.
  6342. * @example
  6343. *
  6344. * _.uniqueId('contact_');
  6345. * // => 'contact_104'
  6346. *
  6347. * _.uniqueId();
  6348. * // => '105'
  6349. */
  6350. function uniqueId(prefix) {
  6351. var id = ++idCounter;
  6352. return String(prefix == null ? '' : prefix) + id;
  6353. }
  6354. /*--------------------------------------------------------------------------*/
  6355. /**
  6356. * Creates a `lodash` object that wraps the given value with explicit
  6357. * method chaining enabled.
  6358. *
  6359. * @static
  6360. * @memberOf _
  6361. * @category Chaining
  6362. * @param {*} value The value to wrap.
  6363. * @returns {Object} Returns the wrapper object.
  6364. * @example
  6365. *
  6366. * var characters = [
  6367. * { 'name': 'barney', 'age': 36 },
  6368. * { 'name': 'fred', 'age': 40 },
  6369. * { 'name': 'pebbles', 'age': 1 }
  6370. * ];
  6371. *
  6372. * var youngest = _.chain(characters)
  6373. * .sortBy('age')
  6374. * .map(function(chr) { return chr.name + ' is ' + chr.age; })
  6375. * .first()
  6376. * .value();
  6377. * // => 'pebbles is 1'
  6378. */
  6379. function chain(value) {
  6380. value = new lodashWrapper(value);
  6381. value.__chain__ = true;
  6382. return value;
  6383. }
  6384. /**
  6385. * Invokes `interceptor` with the `value` as the first argument and then
  6386. * returns `value`. The purpose of this method is to "tap into" a method
  6387. * chain in order to perform operations on intermediate results within
  6388. * the chain.
  6389. *
  6390. * @static
  6391. * @memberOf _
  6392. * @category Chaining
  6393. * @param {*} value The value to provide to `interceptor`.
  6394. * @param {Function} interceptor The function to invoke.
  6395. * @returns {*} Returns `value`.
  6396. * @example
  6397. *
  6398. * _([1, 2, 3, 4])
  6399. * .tap(function(array) { array.pop(); })
  6400. * .reverse()
  6401. * .value();
  6402. * // => [3, 2, 1]
  6403. */
  6404. function tap(value, interceptor) {
  6405. interceptor(value);
  6406. return value;
  6407. }
  6408. /**
  6409. * Enables explicit method chaining on the wrapper object.
  6410. *
  6411. * @name chain
  6412. * @memberOf _
  6413. * @category Chaining
  6414. * @returns {*} Returns the wrapper object.
  6415. * @example
  6416. *
  6417. * var characters = [
  6418. * { 'name': 'barney', 'age': 36 },
  6419. * { 'name': 'fred', 'age': 40 }
  6420. * ];
  6421. *
  6422. * // without explicit chaining
  6423. * _(characters).first();
  6424. * // => { 'name': 'barney', 'age': 36 }
  6425. *
  6426. * // with explicit chaining
  6427. * _(characters).chain()
  6428. * .first()
  6429. * .pick('age')
  6430. * .value();
  6431. * // => { 'age': 36 }
  6432. */
  6433. function wrapperChain() {
  6434. this.__chain__ = true;
  6435. return this;
  6436. }
  6437. /**
  6438. * Produces the `toString` result of the wrapped value.
  6439. *
  6440. * @name toString
  6441. * @memberOf _
  6442. * @category Chaining
  6443. * @returns {string} Returns the string result.
  6444. * @example
  6445. *
  6446. * _([1, 2, 3]).toString();
  6447. * // => '1,2,3'
  6448. */
  6449. function wrapperToString() {
  6450. return String(this.__wrapped__);
  6451. }
  6452. /**
  6453. * Extracts the wrapped value.
  6454. *
  6455. * @name valueOf
  6456. * @memberOf _
  6457. * @alias value
  6458. * @category Chaining
  6459. * @returns {*} Returns the wrapped value.
  6460. * @example
  6461. *
  6462. * _([1, 2, 3]).valueOf();
  6463. * // => [1, 2, 3]
  6464. */
  6465. function wrapperValueOf() {
  6466. return this.__wrapped__;
  6467. }
  6468. /*--------------------------------------------------------------------------*/
  6469. // add functions that return wrapped values when chaining
  6470. lodash.after = after;
  6471. lodash.assign = assign;
  6472. lodash.at = at;
  6473. lodash.bind = bind;
  6474. lodash.bindAll = bindAll;
  6475. lodash.bindKey = bindKey;
  6476. lodash.chain = chain;
  6477. lodash.compact = compact;
  6478. lodash.compose = compose;
  6479. lodash.constant = constant;
  6480. lodash.countBy = countBy;
  6481. lodash.create = create;
  6482. lodash.createCallback = createCallback;
  6483. lodash.curry = curry;
  6484. lodash.debounce = debounce;
  6485. lodash.defaults = defaults;
  6486. lodash.defer = defer;
  6487. lodash.delay = delay;
  6488. lodash.difference = difference;
  6489. lodash.filter = filter;
  6490. lodash.flatten = flatten;
  6491. lodash.forEach = forEach;
  6492. lodash.forEachRight = forEachRight;
  6493. lodash.forIn = forIn;
  6494. lodash.forInRight = forInRight;
  6495. lodash.forOwn = forOwn;
  6496. lodash.forOwnRight = forOwnRight;
  6497. lodash.functions = functions;
  6498. lodash.groupBy = groupBy;
  6499. lodash.indexBy = indexBy;
  6500. lodash.initial = initial;
  6501. lodash.intersection = intersection;
  6502. lodash.invert = invert;
  6503. lodash.invoke = invoke;
  6504. lodash.keys = keys;
  6505. lodash.map = map;
  6506. lodash.mapValues = mapValues;
  6507. lodash.max = max;
  6508. lodash.memoize = memoize;
  6509. lodash.merge = merge;
  6510. lodash.min = min;
  6511. lodash.omit = omit;
  6512. lodash.once = once;
  6513. lodash.pairs = pairs;
  6514. lodash.partial = partial;
  6515. lodash.partialRight = partialRight;
  6516. lodash.pick = pick;
  6517. lodash.pluck = pluck;
  6518. lodash.property = property;
  6519. lodash.pull = pull;
  6520. lodash.range = range;
  6521. lodash.reject = reject;
  6522. lodash.remove = remove;
  6523. lodash.rest = rest;
  6524. lodash.shuffle = shuffle;
  6525. lodash.sortBy = sortBy;
  6526. lodash.tap = tap;
  6527. lodash.throttle = throttle;
  6528. lodash.times = times;
  6529. lodash.toArray = toArray;
  6530. lodash.transform = transform;
  6531. lodash.union = union;
  6532. lodash.uniq = uniq;
  6533. lodash.values = values;
  6534. lodash.where = where;
  6535. lodash.without = without;
  6536. lodash.wrap = wrap;
  6537. lodash.xor = xor;
  6538. lodash.zip = zip;
  6539. lodash.zipObject = zipObject;
  6540. // add aliases
  6541. lodash.collect = map;
  6542. lodash.drop = rest;
  6543. lodash.each = forEach;
  6544. lodash.eachRight = forEachRight;
  6545. lodash.extend = assign;
  6546. lodash.methods = functions;
  6547. lodash.object = zipObject;
  6548. lodash.select = filter;
  6549. lodash.tail = rest;
  6550. lodash.unique = uniq;
  6551. lodash.unzip = zip;
  6552. // add functions to `lodash.prototype`
  6553. mixin(lodash);
  6554. /*--------------------------------------------------------------------------*/
  6555. // add functions that return unwrapped values when chaining
  6556. lodash.clone = clone;
  6557. lodash.cloneDeep = cloneDeep;
  6558. lodash.contains = contains;
  6559. lodash.escape = escape;
  6560. lodash.every = every;
  6561. lodash.find = find;
  6562. lodash.findIndex = findIndex;
  6563. lodash.findKey = findKey;
  6564. lodash.findLast = findLast;
  6565. lodash.findLastIndex = findLastIndex;
  6566. lodash.findLastKey = findLastKey;
  6567. lodash.has = has;
  6568. lodash.identity = identity;
  6569. lodash.indexOf = indexOf;
  6570. lodash.isArguments = isArguments;
  6571. lodash.isArray = isArray;
  6572. lodash.isBoolean = isBoolean;
  6573. lodash.isDate = isDate;
  6574. lodash.isElement = isElement;
  6575. lodash.isEmpty = isEmpty;
  6576. lodash.isEqual = isEqual;
  6577. lodash.isFinite = isFinite;
  6578. lodash.isFunction = isFunction;
  6579. lodash.isNaN = isNaN;
  6580. lodash.isNull = isNull;
  6581. lodash.isNumber = isNumber;
  6582. lodash.isObject = isObject;
  6583. lodash.isPlainObject = isPlainObject;
  6584. lodash.isRegExp = isRegExp;
  6585. lodash.isString = isString;
  6586. lodash.isUndefined = isUndefined;
  6587. lodash.lastIndexOf = lastIndexOf;
  6588. lodash.mixin = mixin;
  6589. lodash.noConflict = noConflict;
  6590. lodash.noop = noop;
  6591. lodash.now = now;
  6592. lodash.parseInt = parseInt;
  6593. lodash.random = random;
  6594. lodash.reduce = reduce;
  6595. lodash.reduceRight = reduceRight;
  6596. lodash.result = result;
  6597. lodash.runInContext = runInContext;
  6598. lodash.size = size;
  6599. lodash.some = some;
  6600. lodash.sortedIndex = sortedIndex;
  6601. lodash.template = template;
  6602. lodash.unescape = unescape;
  6603. lodash.uniqueId = uniqueId;
  6604. // add aliases
  6605. lodash.all = every;
  6606. lodash.any = some;
  6607. lodash.detect = find;
  6608. lodash.findWhere = find;
  6609. lodash.foldl = reduce;
  6610. lodash.foldr = reduceRight;
  6611. lodash.include = contains;
  6612. lodash.inject = reduce;
  6613. mixin(function() {
  6614. var source = {}
  6615. forOwn(lodash, function(func, methodName) {
  6616. if (!lodash.prototype[methodName]) {
  6617. source[methodName] = func;
  6618. }
  6619. });
  6620. return source;
  6621. }(), false);
  6622. /*--------------------------------------------------------------------------*/
  6623. // add functions capable of returning wrapped and unwrapped values when chaining
  6624. lodash.first = first;
  6625. lodash.last = last;
  6626. lodash.sample = sample;
  6627. // add aliases
  6628. lodash.take = first;
  6629. lodash.head = first;
  6630. forOwn(lodash, function(func, methodName) {
  6631. var callbackable = methodName !== 'sample';
  6632. if (!lodash.prototype[methodName]) {
  6633. lodash.prototype[methodName]= function(n, guard) {
  6634. var chainAll = this.__chain__,
  6635. result = func(this.__wrapped__, n, guard);
  6636. return !chainAll && (n == null || (guard && !(callbackable && typeof n == 'function')))
  6637. ? result
  6638. : new lodashWrapper(result, chainAll);
  6639. };
  6640. }
  6641. });
  6642. /*--------------------------------------------------------------------------*/
  6643. /**
  6644. * The semantic version number.
  6645. *
  6646. * @static
  6647. * @memberOf _
  6648. * @type string
  6649. */
  6650. lodash.VERSION = '2.4.2';
  6651. // add "Chaining" functions to the wrapper
  6652. lodash.prototype.chain = wrapperChain;
  6653. lodash.prototype.toString = wrapperToString;
  6654. lodash.prototype.value = wrapperValueOf;
  6655. lodash.prototype.valueOf = wrapperValueOf;
  6656. // add `Array` functions that return unwrapped values
  6657. baseEach(['join', 'pop', 'shift'], function(methodName) {
  6658. var func = arrayRef[methodName];
  6659. lodash.prototype[methodName] = function() {
  6660. var chainAll = this.__chain__,
  6661. result = func.apply(this.__wrapped__, arguments);
  6662. return chainAll
  6663. ? new lodashWrapper(result, chainAll)
  6664. : result;
  6665. };
  6666. });
  6667. // add `Array` functions that return the existing wrapped value
  6668. baseEach(['push', 'reverse', 'sort', 'unshift'], function(methodName) {
  6669. var func = arrayRef[methodName];
  6670. lodash.prototype[methodName] = function() {
  6671. func.apply(this.__wrapped__, arguments);
  6672. return this;
  6673. };
  6674. });
  6675. // add `Array` functions that return new wrapped values
  6676. baseEach(['concat', 'slice', 'splice'], function(methodName) {
  6677. var func = arrayRef[methodName];
  6678. lodash.prototype[methodName] = function() {
  6679. return new lodashWrapper(func.apply(this.__wrapped__, arguments), this.__chain__);
  6680. };
  6681. });
  6682. // avoid array-like object bugs with `Array#shift` and `Array#splice`
  6683. // in IE < 9, Firefox < 10, Narwhal, and RingoJS
  6684. if (!support.spliceObjects) {
  6685. baseEach(['pop', 'shift', 'splice'], function(methodName) {
  6686. var func = arrayRef[methodName],
  6687. isSplice = methodName == 'splice';
  6688. lodash.prototype[methodName] = function() {
  6689. var chainAll = this.__chain__,
  6690. value = this.__wrapped__,
  6691. result = func.apply(value, arguments);
  6692. if (value.length === 0) {
  6693. delete value[0];
  6694. }
  6695. return (chainAll || isSplice)
  6696. ? new lodashWrapper(result, chainAll)
  6697. : result;
  6698. };
  6699. });
  6700. }
  6701. return lodash;
  6702. }
  6703. /*--------------------------------------------------------------------------*/
  6704. // expose Lo-Dash
  6705. var _ = runInContext();
  6706. // some AMD build optimizers like r.js check for condition patterns like the following:
  6707. if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  6708. // Expose Lo-Dash to the global object even when an AMD loader is present in
  6709. // case Lo-Dash is loaded with a RequireJS shim config.
  6710. // See http://requirejs.org/docs/api.html#config-shim
  6711. root._ = _;
  6712. // define as an anonymous module so, through path mapping, it can be
  6713. // referenced as the "underscore" module
  6714. define(function() {
  6715. return _;
  6716. });
  6717. }
  6718. // check for `exports` after `define` in case a build optimizer adds an `exports` object
  6719. else if (freeExports && freeModule) {
  6720. // in Node.js or RingoJS
  6721. if (moduleExports) {
  6722. (freeModule.exports = _)._ = _;
  6723. }
  6724. // in Narwhal or Rhino -require
  6725. else {
  6726. freeExports._ = _;
  6727. }
  6728. }
  6729. else {
  6730. // in a browser or Rhino
  6731. root._ = _;
  6732. }
  6733. }.call(this));