lodash.compat.js 238 KB

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