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

lodash.js 240KB

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