lodash.js 224 KB

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