lodash.js 225 KB

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