lodash.js 399 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236
  1. /**
  2. * @license
  3. * lodash 3.9.3 (Custom Build) <https://lodash.com/>
  4. * Build: `lodash modern -o ./lodash.js`
  5. * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
  6. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  7. * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. * Available under MIT license <https://lodash.com/license>
  9. */
  10. ;(function() {
  11. /** Used as a safe reference for `undefined` in pre-ES5 environments. */
  12. var undefined;
  13. /** Used as the semantic version number. */
  14. var VERSION = '3.9.3';
  15. /** Used to compose bitmasks for wrapper metadata. */
  16. var BIND_FLAG = 1,
  17. BIND_KEY_FLAG = 2,
  18. CURRY_BOUND_FLAG = 4,
  19. CURRY_FLAG = 8,
  20. CURRY_RIGHT_FLAG = 16,
  21. PARTIAL_FLAG = 32,
  22. PARTIAL_RIGHT_FLAG = 64,
  23. ARY_FLAG = 128,
  24. REARG_FLAG = 256;
  25. /** Used as default options for `_.trunc`. */
  26. var DEFAULT_TRUNC_LENGTH = 30,
  27. DEFAULT_TRUNC_OMISSION = '...';
  28. /** Used to detect when a function becomes hot. */
  29. var HOT_COUNT = 150,
  30. HOT_SPAN = 16;
  31. /** Used to indicate the type of lazy iteratees. */
  32. var LAZY_DROP_WHILE_FLAG = 0,
  33. LAZY_FILTER_FLAG = 1,
  34. LAZY_MAP_FLAG = 2;
  35. /** Used as the `TypeError` message for "Functions" methods. */
  36. var FUNC_ERROR_TEXT = 'Expected a function';
  37. /** Used as the internal argument placeholder. */
  38. var PLACEHOLDER = '__lodash_placeholder__';
  39. /** `Object#toString` result references. */
  40. var argsTag = '[object Arguments]',
  41. arrayTag = '[object Array]',
  42. boolTag = '[object Boolean]',
  43. dateTag = '[object Date]',
  44. errorTag = '[object Error]',
  45. funcTag = '[object Function]',
  46. mapTag = '[object Map]',
  47. numberTag = '[object Number]',
  48. objectTag = '[object Object]',
  49. regexpTag = '[object RegExp]',
  50. setTag = '[object Set]',
  51. stringTag = '[object String]',
  52. weakMapTag = '[object WeakMap]';
  53. var arrayBufferTag = '[object ArrayBuffer]',
  54. float32Tag = '[object Float32Array]',
  55. float64Tag = '[object Float64Array]',
  56. int8Tag = '[object Int8Array]',
  57. int16Tag = '[object Int16Array]',
  58. int32Tag = '[object Int32Array]',
  59. uint8Tag = '[object Uint8Array]',
  60. uint8ClampedTag = '[object Uint8ClampedArray]',
  61. uint16Tag = '[object Uint16Array]',
  62. uint32Tag = '[object Uint32Array]';
  63. /** Used to match empty string literals in compiled template source. */
  64. var reEmptyStringLeading = /\b__p \+= '';/g,
  65. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  66. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  67. /** Used to match HTML entities and HTML characters. */
  68. var reEscapedHtml = /&(?:amp|lt|gt|quot|#39|#96);/g,
  69. reUnescapedHtml = /[&<>"'`]/g,
  70. reHasEscapedHtml = RegExp(reEscapedHtml.source),
  71. reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
  72. /** Used to match template delimiters. */
  73. var reEscape = /<%-([\s\S]+?)%>/g,
  74. reEvaluate = /<%([\s\S]+?)%>/g,
  75. reInterpolate = /<%=([\s\S]+?)%>/g;
  76. /** Used to match property names within property paths. */
  77. var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\n\\]|\\.)*?\1)\]/,
  78. reIsPlainProp = /^\w*$/,
  79. rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g;
  80. /**
  81. * Used to match `RegExp` [special characters](http://www.regular-expressions.info/characters.html#special).
  82. * In addition to special characters the forward slash is escaped to allow for
  83. * easier `eval` use and `Function` compilation.
  84. */
  85. var reRegExpChars = /[.*+?^${}()|[\]\/\\]/g,
  86. reHasRegExpChars = RegExp(reRegExpChars.source);
  87. /** Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). */
  88. var reComboMark = /[\u0300-\u036f\ufe20-\ufe23]/g;
  89. /** Used to match backslashes in property paths. */
  90. var reEscapeChar = /\\(\\)?/g;
  91. /** Used to match [ES template delimiters](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-template-literal-lexical-components). */
  92. var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
  93. /** Used to match `RegExp` flags from their coerced string values. */
  94. var reFlags = /\w*$/;
  95. /** Used to detect hexadecimal string values. */
  96. var reHasHexPrefix = /^0[xX]/;
  97. /** Used to detect host constructors (Safari > 5). */
  98. var reIsHostCtor = /^\[object .+?Constructor\]$/;
  99. /** Used to detect unsigned integer values. */
  100. var reIsUint = /^\d+$/;
  101. /** Used to match latin-1 supplementary letters (excluding mathematical operators). */
  102. var reLatin1 = /[\xc0-\xd6\xd8-\xde\xdf-\xf6\xf8-\xff]/g;
  103. /** Used to ensure capturing order of template delimiters. */
  104. var reNoMatch = /($^)/;
  105. /** Used to match unescaped characters in compiled string literals. */
  106. var reUnescapedString = /['\n\r\u2028\u2029\\]/g;
  107. /** Used to match words to create compound words. */
  108. var reWords = (function() {
  109. var upper = '[A-Z\\xc0-\\xd6\\xd8-\\xde]',
  110. lower = '[a-z\\xdf-\\xf6\\xf8-\\xff]+';
  111. return RegExp(upper + '+(?=' + upper + lower + ')|' + upper + '?' + lower + '|' + upper + '+|[0-9]+', 'g');
  112. }());
  113. /** Used to detect and test for whitespace. */
  114. var whitespace = (
  115. // Basic whitespace characters.
  116. ' \t\x0b\f\xa0\ufeff' +
  117. // Line terminators.
  118. '\n\r\u2028\u2029' +
  119. // Unicode category "Zs" space separators.
  120. '\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'
  121. );
  122. /** Used to assign default `context` object properties. */
  123. var contextProps = [
  124. 'Array', 'ArrayBuffer', 'Date', 'Error', 'Float32Array', 'Float64Array',
  125. 'Function', 'Int8Array', 'Int16Array', 'Int32Array', 'Math', 'Number',
  126. 'Object', 'RegExp', 'Set', 'String', '_', 'clearTimeout', 'document',
  127. 'isFinite', 'parseFloat', 'parseInt', 'setTimeout', 'TypeError', 'Uint8Array',
  128. 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array', 'WeakMap', 'window'
  129. ];
  130. /** Used to make template sourceURLs easier to identify. */
  131. var templateCounter = -1;
  132. /** Used to identify `toStringTag` values of typed arrays. */
  133. var typedArrayTags = {};
  134. typedArrayTags[float32Tag] = typedArrayTags[float64Tag] =
  135. typedArrayTags[int8Tag] = typedArrayTags[int16Tag] =
  136. typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] =
  137. typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] =
  138. typedArrayTags[uint32Tag] = true;
  139. typedArrayTags[argsTag] = typedArrayTags[arrayTag] =
  140. typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] =
  141. typedArrayTags[dateTag] = typedArrayTags[errorTag] =
  142. typedArrayTags[funcTag] = typedArrayTags[mapTag] =
  143. typedArrayTags[numberTag] = typedArrayTags[objectTag] =
  144. typedArrayTags[regexpTag] = typedArrayTags[setTag] =
  145. typedArrayTags[stringTag] = typedArrayTags[weakMapTag] = false;
  146. /** Used to identify `toStringTag` values supported by `_.clone`. */
  147. var cloneableTags = {};
  148. cloneableTags[argsTag] = cloneableTags[arrayTag] =
  149. cloneableTags[arrayBufferTag] = cloneableTags[boolTag] =
  150. cloneableTags[dateTag] = cloneableTags[float32Tag] =
  151. cloneableTags[float64Tag] = cloneableTags[int8Tag] =
  152. cloneableTags[int16Tag] = cloneableTags[int32Tag] =
  153. cloneableTags[numberTag] = cloneableTags[objectTag] =
  154. cloneableTags[regexpTag] = cloneableTags[stringTag] =
  155. cloneableTags[uint8Tag] = cloneableTags[uint8ClampedTag] =
  156. cloneableTags[uint16Tag] = cloneableTags[uint32Tag] = true;
  157. cloneableTags[errorTag] = cloneableTags[funcTag] =
  158. cloneableTags[mapTag] = cloneableTags[setTag] =
  159. cloneableTags[weakMapTag] = false;
  160. /** Used as an internal `_.debounce` options object by `_.throttle`. */
  161. var debounceOptions = {
  162. 'leading': false,
  163. 'maxWait': 0,
  164. 'trailing': false
  165. };
  166. /** Used to map latin-1 supplementary letters to basic latin letters. */
  167. var deburredLetters = {
  168. '\xc0': 'A', '\xc1': 'A', '\xc2': 'A', '\xc3': 'A', '\xc4': 'A', '\xc5': 'A',
  169. '\xe0': 'a', '\xe1': 'a', '\xe2': 'a', '\xe3': 'a', '\xe4': 'a', '\xe5': 'a',
  170. '\xc7': 'C', '\xe7': 'c',
  171. '\xd0': 'D', '\xf0': 'd',
  172. '\xc8': 'E', '\xc9': 'E', '\xca': 'E', '\xcb': 'E',
  173. '\xe8': 'e', '\xe9': 'e', '\xea': 'e', '\xeb': 'e',
  174. '\xcC': 'I', '\xcd': 'I', '\xce': 'I', '\xcf': 'I',
  175. '\xeC': 'i', '\xed': 'i', '\xee': 'i', '\xef': 'i',
  176. '\xd1': 'N', '\xf1': 'n',
  177. '\xd2': 'O', '\xd3': 'O', '\xd4': 'O', '\xd5': 'O', '\xd6': 'O', '\xd8': 'O',
  178. '\xf2': 'o', '\xf3': 'o', '\xf4': 'o', '\xf5': 'o', '\xf6': 'o', '\xf8': 'o',
  179. '\xd9': 'U', '\xda': 'U', '\xdb': 'U', '\xdc': 'U',
  180. '\xf9': 'u', '\xfa': 'u', '\xfb': 'u', '\xfc': 'u',
  181. '\xdd': 'Y', '\xfd': 'y', '\xff': 'y',
  182. '\xc6': 'Ae', '\xe6': 'ae',
  183. '\xde': 'Th', '\xfe': 'th',
  184. '\xdf': 'ss'
  185. };
  186. /** Used to map characters to HTML entities. */
  187. var htmlEscapes = {
  188. '&': '&amp;',
  189. '<': '&lt;',
  190. '>': '&gt;',
  191. '"': '&quot;',
  192. "'": '&#39;',
  193. '`': '&#96;'
  194. };
  195. /** Used to map HTML entities to characters. */
  196. var htmlUnescapes = {
  197. '&amp;': '&',
  198. '&lt;': '<',
  199. '&gt;': '>',
  200. '&quot;': '"',
  201. '&#39;': "'",
  202. '&#96;': '`'
  203. };
  204. /** Used to determine if values are of the language type `Object`. */
  205. var objectTypes = {
  206. 'function': true,
  207. 'object': true
  208. };
  209. /** Used to escape characters for inclusion in compiled string literals. */
  210. var stringEscapes = {
  211. '\\': '\\',
  212. "'": "'",
  213. '\n': 'n',
  214. '\r': 'r',
  215. '\u2028': 'u2028',
  216. '\u2029': 'u2029'
  217. };
  218. /** Detect free variable `exports`. */
  219. var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
  220. /** Detect free variable `module`. */
  221. var freeModule = objectTypes[typeof module] && module && !module.nodeType && module;
  222. /** Detect free variable `global` from Node.js. */
  223. var freeGlobal = freeExports && freeModule && typeof global == 'object' && global && global.Object && global;
  224. /** Detect free variable `self`. */
  225. var freeSelf = objectTypes[typeof self] && self && self.Object && self;
  226. /** Detect free variable `window`. */
  227. var freeWindow = objectTypes[typeof window] && window && window.Object && window;
  228. /** Detect the popular CommonJS extension `module.exports`. */
  229. var moduleExports = freeModule && freeModule.exports === freeExports && freeExports;
  230. /**
  231. * Used as a reference to the global object.
  232. *
  233. * The `this` value is used if it's the global object to avoid Greasemonkey's
  234. * restricted `window` object, otherwise the `window` object is used.
  235. */
  236. var root = freeGlobal || ((freeWindow !== (this && this.window)) && freeWindow) || freeSelf || this;
  237. /*--------------------------------------------------------------------------*/
  238. /**
  239. * The base implementation of `compareAscending` which compares values and
  240. * sorts them in ascending order without guaranteeing a stable sort.
  241. *
  242. * @private
  243. * @param {*} value The value to compare.
  244. * @param {*} other The other value to compare.
  245. * @returns {number} Returns the sort order indicator for `value`.
  246. */
  247. function baseCompareAscending(value, other) {
  248. if (value !== other) {
  249. var valIsNull = value === null,
  250. valIsUndef = value === undefined,
  251. valIsReflexive = value === value;
  252. var othIsNull = other === null,
  253. othIsUndef = other === undefined,
  254. othIsReflexive = other === other;
  255. if ((value > other && !othIsNull) || !valIsReflexive ||
  256. (valIsNull && !othIsUndef && othIsReflexive) ||
  257. (valIsUndef && othIsReflexive)) {
  258. return 1;
  259. }
  260. if ((value < other && !valIsNull) || !othIsReflexive ||
  261. (othIsNull && !valIsUndef && valIsReflexive) ||
  262. (othIsUndef && valIsReflexive)) {
  263. return -1;
  264. }
  265. }
  266. return 0;
  267. }
  268. /**
  269. * The base implementation of `_.findIndex` and `_.findLastIndex` without
  270. * support for callback shorthands and `this` binding.
  271. *
  272. * @private
  273. * @param {Array} array The array to search.
  274. * @param {Function} predicate The function invoked per iteration.
  275. * @param {boolean} [fromRight] Specify iterating from right to left.
  276. * @returns {number} Returns the index of the matched value, else `-1`.
  277. */
  278. function baseFindIndex(array, predicate, fromRight) {
  279. var length = array.length,
  280. index = fromRight ? length : -1;
  281. while ((fromRight ? index-- : ++index < length)) {
  282. if (predicate(array[index], index, array)) {
  283. return index;
  284. }
  285. }
  286. return -1;
  287. }
  288. /**
  289. * The base implementation of `_.indexOf` without support for binary searches.
  290. *
  291. * @private
  292. * @param {Array} array The array to search.
  293. * @param {*} value The value to search for.
  294. * @param {number} fromIndex The index to search from.
  295. * @returns {number} Returns the index of the matched value, else `-1`.
  296. */
  297. function baseIndexOf(array, value, fromIndex) {
  298. if (value !== value) {
  299. return indexOfNaN(array, fromIndex);
  300. }
  301. var index = fromIndex - 1,
  302. length = array.length;
  303. while (++index < length) {
  304. if (array[index] === value) {
  305. return index;
  306. }
  307. }
  308. return -1;
  309. }
  310. /**
  311. * The base implementation of `_.isFunction` without support for environments
  312. * with incorrect `typeof` results.
  313. *
  314. * @private
  315. * @param {*} value The value to check.
  316. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  317. */
  318. function baseIsFunction(value) {
  319. // Avoid a Chakra JIT bug in compatibility modes of IE 11.
  320. // See https://github.com/jashkenas/underscore/issues/1621 for more details.
  321. return typeof value == 'function' || false;
  322. }
  323. /**
  324. * Converts `value` to a string if it's not one. An empty string is returned
  325. * for `null` or `undefined` values.
  326. *
  327. * @private
  328. * @param {*} value The value to process.
  329. * @returns {string} Returns the string.
  330. */
  331. function baseToString(value) {
  332. if (typeof value == 'string') {
  333. return value;
  334. }
  335. return value == null ? '' : (value + '');
  336. }
  337. /**
  338. * Used by `_.trim` and `_.trimLeft` to get the index of the first character
  339. * of `string` that is not found in `chars`.
  340. *
  341. * @private
  342. * @param {string} string The string to inspect.
  343. * @param {string} chars The characters to find.
  344. * @returns {number} Returns the index of the first character not found in `chars`.
  345. */
  346. function charsLeftIndex(string, chars) {
  347. var index = -1,
  348. length = string.length;
  349. while (++index < length && chars.indexOf(string.charAt(index)) > -1) {}
  350. return index;
  351. }
  352. /**
  353. * Used by `_.trim` and `_.trimRight` to get the index of the last character
  354. * of `string` that is not found in `chars`.
  355. *
  356. * @private
  357. * @param {string} string The string to inspect.
  358. * @param {string} chars The characters to find.
  359. * @returns {number} Returns the index of the last character not found in `chars`.
  360. */
  361. function charsRightIndex(string, chars) {
  362. var index = string.length;
  363. while (index-- && chars.indexOf(string.charAt(index)) > -1) {}
  364. return index;
  365. }
  366. /**
  367. * Used by `_.sortBy` to compare transformed elements of a collection and stable
  368. * sort them in ascending order.
  369. *
  370. * @private
  371. * @param {Object} object The object to compare to `other`.
  372. * @param {Object} other The object to compare to `object`.
  373. * @returns {number} Returns the sort order indicator for `object`.
  374. */
  375. function compareAscending(object, other) {
  376. return baseCompareAscending(object.criteria, other.criteria) || (object.index - other.index);
  377. }
  378. /**
  379. * Used by `_.sortByOrder` to compare multiple properties of each element
  380. * in a collection and stable sort them in the following order:
  381. *
  382. * If `orders` is unspecified, sort in ascending order for all properties.
  383. * Otherwise, for each property, sort in ascending order if its corresponding value in
  384. * orders is true, and descending order if false.
  385. *
  386. * @private
  387. * @param {Object} object The object to compare to `other`.
  388. * @param {Object} other The object to compare to `object`.
  389. * @param {boolean[]} orders The order to sort by for each property.
  390. * @returns {number} Returns the sort order indicator for `object`.
  391. */
  392. function compareMultiple(object, other, orders) {
  393. var index = -1,
  394. objCriteria = object.criteria,
  395. othCriteria = other.criteria,
  396. length = objCriteria.length,
  397. ordersLength = orders.length;
  398. while (++index < length) {
  399. var result = baseCompareAscending(objCriteria[index], othCriteria[index]);
  400. if (result) {
  401. if (index >= ordersLength) {
  402. return result;
  403. }
  404. return result * (orders[index] ? 1 : -1);
  405. }
  406. }
  407. // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
  408. // that causes it, under certain circumstances, to provide the same value for
  409. // `object` and `other`. See https://github.com/jashkenas/underscore/pull/1247
  410. // for more details.
  411. //
  412. // This also ensures a stable sort in V8 and other engines.
  413. // See https://code.google.com/p/v8/issues/detail?id=90 for more details.
  414. return object.index - other.index;
  415. }
  416. /**
  417. * Used by `_.deburr` to convert latin-1 supplementary letters to basic latin letters.
  418. *
  419. * @private
  420. * @param {string} letter The matched letter to deburr.
  421. * @returns {string} Returns the deburred letter.
  422. */
  423. function deburrLetter(letter) {
  424. return deburredLetters[letter];
  425. }
  426. /**
  427. * Used by `_.escape` to convert characters to HTML entities.
  428. *
  429. * @private
  430. * @param {string} chr The matched character to escape.
  431. * @returns {string} Returns the escaped character.
  432. */
  433. function escapeHtmlChar(chr) {
  434. return htmlEscapes[chr];
  435. }
  436. /**
  437. * Used by `_.template` to escape characters for inclusion in compiled
  438. * string literals.
  439. *
  440. * @private
  441. * @param {string} chr The matched character to escape.
  442. * @returns {string} Returns the escaped character.
  443. */
  444. function escapeStringChar(chr) {
  445. return '\\' + stringEscapes[chr];
  446. }
  447. /**
  448. * Gets the index at which the first occurrence of `NaN` is found in `array`.
  449. *
  450. * @private
  451. * @param {Array} array The array to search.
  452. * @param {number} fromIndex The index to search from.
  453. * @param {boolean} [fromRight] Specify iterating from right to left.
  454. * @returns {number} Returns the index of the matched `NaN`, else `-1`.
  455. */
  456. function indexOfNaN(array, fromIndex, fromRight) {
  457. var length = array.length,
  458. index = fromIndex + (fromRight ? 0 : -1);
  459. while ((fromRight ? index-- : ++index < length)) {
  460. var other = array[index];
  461. if (other !== other) {
  462. return index;
  463. }
  464. }
  465. return -1;
  466. }
  467. /**
  468. * Checks if `value` is object-like.
  469. *
  470. * @private
  471. * @param {*} value The value to check.
  472. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  473. */
  474. function isObjectLike(value) {
  475. return !!value && typeof value == 'object';
  476. }
  477. /**
  478. * Used by `trimmedLeftIndex` and `trimmedRightIndex` to determine if a
  479. * character code is whitespace.
  480. *
  481. * @private
  482. * @param {number} charCode The character code to inspect.
  483. * @returns {boolean} Returns `true` if `charCode` is whitespace, else `false`.
  484. */
  485. function isSpace(charCode) {
  486. return ((charCode <= 160 && (charCode >= 9 && charCode <= 13) || charCode == 32 || charCode == 160) || charCode == 5760 || charCode == 6158 ||
  487. (charCode >= 8192 && (charCode <= 8202 || charCode == 8232 || charCode == 8233 || charCode == 8239 || charCode == 8287 || charCode == 12288 || charCode == 65279)));
  488. }
  489. /**
  490. * Replaces all `placeholder` elements in `array` with an internal placeholder
  491. * and returns an array of their indexes.
  492. *
  493. * @private
  494. * @param {Array} array The array to modify.
  495. * @param {*} placeholder The placeholder to replace.
  496. * @returns {Array} Returns the new array of placeholder indexes.
  497. */
  498. function replaceHolders(array, placeholder) {
  499. var index = -1,
  500. length = array.length,
  501. resIndex = -1,
  502. result = [];
  503. while (++index < length) {
  504. if (array[index] === placeholder) {
  505. array[index] = PLACEHOLDER;
  506. result[++resIndex] = index;
  507. }
  508. }
  509. return result;
  510. }
  511. /**
  512. * An implementation of `_.uniq` optimized for sorted arrays without support
  513. * for callback shorthands and `this` binding.
  514. *
  515. * @private
  516. * @param {Array} array The array to inspect.
  517. * @param {Function} [iteratee] The function invoked per iteration.
  518. * @returns {Array} Returns the new duplicate-value-free array.
  519. */
  520. function sortedUniq(array, iteratee) {
  521. var seen,
  522. index = -1,
  523. length = array.length,
  524. resIndex = -1,
  525. result = [];
  526. while (++index < length) {
  527. var value = array[index],
  528. computed = iteratee ? iteratee(value, index, array) : value;
  529. if (!index || seen !== computed) {
  530. seen = computed;
  531. result[++resIndex] = value;
  532. }
  533. }
  534. return result;
  535. }
  536. /**
  537. * Used by `_.trim` and `_.trimLeft` to get the index of the first non-whitespace
  538. * character of `string`.
  539. *
  540. * @private
  541. * @param {string} string The string to inspect.
  542. * @returns {number} Returns the index of the first non-whitespace character.
  543. */
  544. function trimmedLeftIndex(string) {
  545. var index = -1,
  546. length = string.length;
  547. while (++index < length && isSpace(string.charCodeAt(index))) {}
  548. return index;
  549. }
  550. /**
  551. * Used by `_.trim` and `_.trimRight` to get the index of the last non-whitespace
  552. * character of `string`.
  553. *
  554. * @private
  555. * @param {string} string The string to inspect.
  556. * @returns {number} Returns the index of the last non-whitespace character.
  557. */
  558. function trimmedRightIndex(string) {
  559. var index = string.length;
  560. while (index-- && isSpace(string.charCodeAt(index))) {}
  561. return index;
  562. }
  563. /**
  564. * Used by `_.unescape` to convert HTML entities to characters.
  565. *
  566. * @private
  567. * @param {string} chr The matched character to unescape.
  568. * @returns {string} Returns the unescaped character.
  569. */
  570. function unescapeHtmlChar(chr) {
  571. return htmlUnescapes[chr];
  572. }
  573. /*--------------------------------------------------------------------------*/
  574. /**
  575. * Create a new pristine `lodash` function using the given `context` object.
  576. *
  577. * @static
  578. * @memberOf _
  579. * @category Utility
  580. * @param {Object} [context=root] The context object.
  581. * @returns {Function} Returns a new `lodash` function.
  582. * @example
  583. *
  584. * _.mixin({ 'foo': _.constant('foo') });
  585. *
  586. * var lodash = _.runInContext();
  587. * lodash.mixin({ 'bar': lodash.constant('bar') });
  588. *
  589. * _.isFunction(_.foo);
  590. * // => true
  591. * _.isFunction(_.bar);
  592. * // => false
  593. *
  594. * lodash.isFunction(lodash.foo);
  595. * // => false
  596. * lodash.isFunction(lodash.bar);
  597. * // => true
  598. *
  599. * // using `context` to mock `Date#getTime` use in `_.now`
  600. * var mock = _.runInContext({
  601. * 'Date': function() {
  602. * return { 'getTime': getTimeMock };
  603. * }
  604. * });
  605. *
  606. * // or creating a suped-up `defer` in Node.js
  607. * var defer = _.runInContext({ 'setTimeout': setImmediate }).defer;
  608. */
  609. function runInContext(context) {
  610. // Avoid issues with some ES3 environments that attempt to use values, named
  611. // after built-in constructors like `Object`, for the creation of literals.
  612. // ES5 clears this up by stating that literals must use built-in constructors.
  613. // See https://es5.github.io/#x11.1.5 for more details.
  614. context = context ? _.defaults(root.Object(), context, _.pick(root, contextProps)) : root;
  615. /** Native constructor references. */
  616. var Array = context.Array,
  617. Date = context.Date,
  618. Error = context.Error,
  619. Function = context.Function,
  620. Math = context.Math,
  621. Number = context.Number,
  622. Object = context.Object,
  623. RegExp = context.RegExp,
  624. String = context.String,
  625. TypeError = context.TypeError;
  626. /** Used for native method references. */
  627. var arrayProto = Array.prototype,
  628. objectProto = Object.prototype,
  629. stringProto = String.prototype;
  630. /** Used to detect DOM support. */
  631. var document = (document = context.window) ? document.document : null;
  632. /** Used to resolve the decompiled source of functions. */
  633. var fnToString = Function.prototype.toString;
  634. /** Used to check objects for own properties. */
  635. var hasOwnProperty = objectProto.hasOwnProperty;
  636. /** Used to generate unique IDs. */
  637. var idCounter = 0;
  638. /**
  639. * Used to resolve the [`toStringTag`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring)
  640. * of values.
  641. */
  642. var objToString = objectProto.toString;
  643. /** Used to restore the original `_` reference in `_.noConflict`. */
  644. var oldDash = context._;
  645. /** Used to detect if a method is native. */
  646. var reIsNative = RegExp('^' +
  647. escapeRegExp(fnToString.call(hasOwnProperty))
  648. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
  649. );
  650. /** Native method references. */
  651. var ArrayBuffer = getNative(context, 'ArrayBuffer'),
  652. bufferSlice = getNative(ArrayBuffer && new ArrayBuffer(0), 'slice'),
  653. ceil = Math.ceil,
  654. clearTimeout = context.clearTimeout,
  655. floor = Math.floor,
  656. getPrototypeOf = getNative(Object, 'getPrototypeOf'),
  657. parseFloat = context.parseFloat,
  658. push = arrayProto.push,
  659. Set = getNative(context, 'Set'),
  660. setTimeout = context.setTimeout,
  661. splice = arrayProto.splice,
  662. Uint8Array = getNative(context, 'Uint8Array'),
  663. WeakMap = getNative(context, 'WeakMap');
  664. /** Used to clone array buffers. */
  665. var Float64Array = (function() {
  666. // Safari 5 errors when using an array buffer to initialize a typed array
  667. // where the array buffer's `byteLength` is not a multiple of the typed
  668. // array's `BYTES_PER_ELEMENT`.
  669. try {
  670. var func = getNative(context, 'Float64Array'),
  671. result = new func(new ArrayBuffer(10), 0, 1) && func;
  672. } catch(e) {}
  673. return result || null;
  674. }());
  675. /* Native method references for those with the same name as other `lodash` methods. */
  676. var nativeCreate = getNative(Object, 'create'),
  677. nativeIsArray = getNative(Array, 'isArray'),
  678. nativeIsFinite = context.isFinite,
  679. nativeKeys = getNative(Object, 'keys'),
  680. nativeMax = Math.max,
  681. nativeMin = Math.min,
  682. nativeNow = getNative(Date, 'now'),
  683. nativeNumIsFinite = getNative(Number, 'isFinite'),
  684. nativeParseInt = context.parseInt,
  685. nativeRandom = Math.random;
  686. /** Used as references for `-Infinity` and `Infinity`. */
  687. var NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY,
  688. POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
  689. /** Used as references for the maximum length and index of an array. */
  690. var MAX_ARRAY_LENGTH = 4294967295,
  691. MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1,
  692. HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1;
  693. /** Used as the size, in bytes, of each `Float64Array` element. */
  694. var FLOAT64_BYTES_PER_ELEMENT = Float64Array ? Float64Array.BYTES_PER_ELEMENT : 0;
  695. /**
  696. * Used as the [maximum length](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.max_safe_integer)
  697. * of an array-like value.
  698. */
  699. var MAX_SAFE_INTEGER = 9007199254740991;
  700. /** Used to store function metadata. */
  701. var metaMap = WeakMap && new WeakMap;
  702. /** Used to lookup unminified function names. */
  703. var realNames = {};
  704. /*------------------------------------------------------------------------*/
  705. /**
  706. * Creates a `lodash` object which wraps `value` to enable implicit chaining.
  707. * Methods that operate on and return arrays, collections, and functions can
  708. * be chained together. Methods that return a boolean or single value will
  709. * automatically end the chain returning the unwrapped value. Explicit chaining
  710. * may be enabled using `_.chain`. The execution of chained methods is lazy,
  711. * that is, execution is deferred until `_#value` is implicitly or explicitly
  712. * called.
  713. *
  714. * Lazy evaluation allows several methods to support shortcut fusion. Shortcut
  715. * fusion is an optimization that merges iteratees to avoid creating intermediate
  716. * arrays and reduce the number of iteratee executions.
  717. *
  718. * Chaining is supported in custom builds as long as the `_#value` method is
  719. * directly or indirectly included in the build.
  720. *
  721. * In addition to lodash methods, wrappers have `Array` and `String` methods.
  722. *
  723. * The wrapper `Array` methods are:
  724. * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`,
  725. * `splice`, and `unshift`
  726. *
  727. * The wrapper `String` methods are:
  728. * `replace` and `split`
  729. *
  730. * The wrapper methods that support shortcut fusion are:
  731. * `compact`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`,
  732. * `first`, `initial`, `last`, `map`, `pluck`, `reject`, `rest`, `reverse`,
  733. * `slice`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `toArray`,
  734. * and `where`
  735. *
  736. * The chainable wrapper methods are:
  737. * `after`, `ary`, `assign`, `at`, `before`, `bind`, `bindAll`, `bindKey`,
  738. * `callback`, `chain`, `chunk`, `commit`, `compact`, `concat`, `constant`,
  739. * `countBy`, `create`, `curry`, `debounce`, `defaults`, `defer`, `delay`,
  740. * `difference`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `fill`,
  741. * `filter`, `flatten`, `flattenDeep`, `flow`, `flowRight`, `forEach`,
  742. * `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`, `functions`,
  743. * `groupBy`, `indexBy`, `initial`, `intersection`, `invert`, `invoke`, `keys`,
  744. * `keysIn`, `map`, `mapKeys`, `mapValues`, `matches`, `matchesProperty`,
  745. * `memoize`, `merge`, `method`, `methodOf`, `mixin`, `negate`, `omit`, `once`,
  746. * `pairs`, `partial`, `partialRight`, `partition`, `pick`, `plant`, `pluck`,
  747. * `property`, `propertyOf`, `pull`, `pullAt`, `push`, `range`, `rearg`,
  748. * `reject`, `remove`, `rest`, `restParam`, `reverse`, `set`, `shuffle`,
  749. * `slice`, `sort`, `sortBy`, `sortByAll`, `sortByOrder`, `splice`, `spread`,
  750. * `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`,
  751. * `thru`, `times`, `toArray`, `toPlainObject`, `transform`, `union`, `uniq`,
  752. * `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`, `where`, `without`,
  753. * `wrap`, `xor`, `zip`, `zipObject`, `zipWith`
  754. *
  755. * The wrapper methods that are **not** chainable by default are:
  756. * `add`, `attempt`, `camelCase`, `capitalize`, `clone`, `cloneDeep`, `deburr`,
  757. * `endsWith`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`,
  758. * `findLast`, `findLastIndex`, `findLastKey`, `findWhere`, `first`, `get`,
  759. * `gt`, `gte`, `has`, `identity`, `includes`, `indexOf`, `inRange`, `isArguments`,
  760. * `isArray`, `isBoolean`, `isDate`, `isElement`, `isEmpty`, `isEqual`, `isError`,
  761. * `isFinite` `isFunction`, `isMatch`, `isNative`, `isNaN`, `isNull`, `isNumber`,
  762. * `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`,
  763. * `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lt`, `lte`,
  764. * `max`, `min`, `noConflict`, `noop`, `now`, `pad`, `padLeft`, `padRight`,
  765. * `parseInt`, `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`,
  766. * `runInContext`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`,
  767. * `sortedLastIndex`, `startCase`, `startsWith`, `sum`, `template`, `trim`,
  768. * `trimLeft`, `trimRight`, `trunc`, `unescape`, `uniqueId`, `value`, and `words`
  769. *
  770. * The wrapper method `sample` will return a wrapped value when `n` is provided,
  771. * otherwise an unwrapped value is returned.
  772. *
  773. * @name _
  774. * @constructor
  775. * @category Chain
  776. * @param {*} value The value to wrap in a `lodash` instance.
  777. * @returns {Object} Returns the new `lodash` wrapper instance.
  778. * @example
  779. *
  780. * var wrapped = _([1, 2, 3]);
  781. *
  782. * // returns an unwrapped value
  783. * wrapped.reduce(function(total, n) {
  784. * return total + n;
  785. * });
  786. * // => 6
  787. *
  788. * // returns a wrapped value
  789. * var squares = wrapped.map(function(n) {
  790. * return n * n;
  791. * });
  792. *
  793. * _.isArray(squares);
  794. * // => false
  795. *
  796. * _.isArray(squares.value());
  797. * // => true
  798. */
  799. function lodash(value) {
  800. if (isObjectLike(value) && !isArray(value) && !(value instanceof LazyWrapper)) {
  801. if (value instanceof LodashWrapper) {
  802. return value;
  803. }
  804. if (hasOwnProperty.call(value, '__chain__') && hasOwnProperty.call(value, '__wrapped__')) {
  805. return wrapperClone(value);
  806. }
  807. }
  808. return new LodashWrapper(value);
  809. }
  810. /**
  811. * The function whose prototype all chaining wrappers inherit from.
  812. *
  813. * @private
  814. */
  815. function baseLodash() {
  816. // No operation performed.
  817. }
  818. /**
  819. * The base constructor for creating `lodash` wrapper objects.
  820. *
  821. * @private
  822. * @param {*} value The value to wrap.
  823. * @param {boolean} [chainAll] Enable chaining for all wrapper methods.
  824. * @param {Array} [actions=[]] Actions to peform to resolve the unwrapped value.
  825. */
  826. function LodashWrapper(value, chainAll, actions) {
  827. this.__wrapped__ = value;
  828. this.__actions__ = actions || [];
  829. this.__chain__ = !!chainAll;
  830. }
  831. /**
  832. * An object environment feature flags.
  833. *
  834. * @static
  835. * @memberOf _
  836. * @type Object
  837. */
  838. var support = lodash.support = {};
  839. (function(x) {
  840. var Ctor = function() { this.x = x; },
  841. object = { '0': x, 'length': x },
  842. props = [];
  843. Ctor.prototype = { 'valueOf': x, 'y': x };
  844. for (var key in new Ctor) { props.push(key); }
  845. /**
  846. * Detect if the DOM is supported.
  847. *
  848. * @memberOf _.support
  849. * @type boolean
  850. */
  851. try {
  852. support.dom = document.createDocumentFragment().nodeType === 11;
  853. } catch(e) {
  854. support.dom = false;
  855. }
  856. }(1, 0));
  857. /**
  858. * By default, the template delimiters used by lodash are like those in
  859. * embedded Ruby (ERB). Change the following template settings to use
  860. * alternative delimiters.
  861. *
  862. * @static
  863. * @memberOf _
  864. * @type Object
  865. */
  866. lodash.templateSettings = {
  867. /**
  868. * Used to detect `data` property values to be HTML-escaped.
  869. *
  870. * @memberOf _.templateSettings
  871. * @type RegExp
  872. */
  873. 'escape': reEscape,
  874. /**
  875. * Used to detect code to be evaluated.
  876. *
  877. * @memberOf _.templateSettings
  878. * @type RegExp
  879. */
  880. 'evaluate': reEvaluate,
  881. /**
  882. * Used to detect `data` property values to inject.
  883. *
  884. * @memberOf _.templateSettings
  885. * @type RegExp
  886. */
  887. 'interpolate': reInterpolate,
  888. /**
  889. * Used to reference the data object in the template text.
  890. *
  891. * @memberOf _.templateSettings
  892. * @type string
  893. */
  894. 'variable': '',
  895. /**
  896. * Used to import variables into the compiled template.
  897. *
  898. * @memberOf _.templateSettings
  899. * @type Object
  900. */
  901. 'imports': {
  902. /**
  903. * A reference to the `lodash` function.
  904. *
  905. * @memberOf _.templateSettings.imports
  906. * @type Function
  907. */
  908. '_': lodash
  909. }
  910. };
  911. /*------------------------------------------------------------------------*/
  912. /**
  913. * Creates a lazy wrapper object which wraps `value` to enable lazy evaluation.
  914. *
  915. * @private
  916. * @param {*} value The value to wrap.
  917. */
  918. function LazyWrapper(value) {
  919. this.__wrapped__ = value;
  920. this.__actions__ = null;
  921. this.__dir__ = 1;
  922. this.__dropCount__ = 0;
  923. this.__filtered__ = false;
  924. this.__iteratees__ = null;
  925. this.__takeCount__ = POSITIVE_INFINITY;
  926. this.__views__ = null;
  927. }
  928. /**
  929. * Creates a clone of the lazy wrapper object.
  930. *
  931. * @private
  932. * @name clone
  933. * @memberOf LazyWrapper
  934. * @returns {Object} Returns the cloned `LazyWrapper` object.
  935. */
  936. function lazyClone() {
  937. var actions = this.__actions__,
  938. iteratees = this.__iteratees__,
  939. views = this.__views__,
  940. result = new LazyWrapper(this.__wrapped__);
  941. result.__actions__ = actions ? arrayCopy(actions) : null;
  942. result.__dir__ = this.__dir__;
  943. result.__filtered__ = this.__filtered__;
  944. result.__iteratees__ = iteratees ? arrayCopy(iteratees) : null;
  945. result.__takeCount__ = this.__takeCount__;
  946. result.__views__ = views ? arrayCopy(views) : null;
  947. return result;
  948. }
  949. /**
  950. * Reverses the direction of lazy iteration.
  951. *
  952. * @private
  953. * @name reverse
  954. * @memberOf LazyWrapper
  955. * @returns {Object} Returns the new reversed `LazyWrapper` object.
  956. */
  957. function lazyReverse() {
  958. if (this.__filtered__) {
  959. var result = new LazyWrapper(this);
  960. result.__dir__ = -1;
  961. result.__filtered__ = true;
  962. } else {
  963. result = this.clone();
  964. result.__dir__ *= -1;
  965. }
  966. return result;
  967. }
  968. /**
  969. * Extracts the unwrapped value from its lazy wrapper.
  970. *
  971. * @private
  972. * @name value
  973. * @memberOf LazyWrapper
  974. * @returns {*} Returns the unwrapped value.
  975. */
  976. function lazyValue() {
  977. var array = this.__wrapped__.value();
  978. if (!isArray(array)) {
  979. return baseWrapperValue(array, this.__actions__);
  980. }
  981. var dir = this.__dir__,
  982. isRight = dir < 0,
  983. view = getView(0, array.length, this.__views__),
  984. start = view.start,
  985. end = view.end,
  986. length = end - start,
  987. index = isRight ? end : (start - 1),
  988. takeCount = nativeMin(length, this.__takeCount__),
  989. iteratees = this.__iteratees__,
  990. iterLength = iteratees ? iteratees.length : 0,
  991. resIndex = 0,
  992. result = [];
  993. outer:
  994. while (length-- && resIndex < takeCount) {
  995. index += dir;
  996. var iterIndex = -1,
  997. value = array[index];
  998. while (++iterIndex < iterLength) {
  999. var data = iteratees[iterIndex],
  1000. iteratee = data.iteratee,
  1001. type = data.type;
  1002. if (type == LAZY_DROP_WHILE_FLAG) {
  1003. if (data.done && (isRight ? (index > data.index) : (index < data.index))) {
  1004. data.count = 0;
  1005. data.done = false;
  1006. }
  1007. data.index = index;
  1008. if (!data.done) {
  1009. var limit = data.limit;
  1010. if (!(data.done = limit > -1 ? (data.count++ >= limit) : !iteratee(value))) {
  1011. continue outer;
  1012. }
  1013. }
  1014. } else {
  1015. var computed = iteratee(value);
  1016. if (type == LAZY_MAP_FLAG) {
  1017. value = computed;
  1018. } else if (!computed) {
  1019. if (type == LAZY_FILTER_FLAG) {
  1020. continue outer;
  1021. } else {
  1022. break outer;
  1023. }
  1024. }
  1025. }
  1026. }
  1027. result[resIndex++] = value;
  1028. }
  1029. return result;
  1030. }
  1031. /*------------------------------------------------------------------------*/
  1032. /**
  1033. * Creates a cache object to store key/value pairs.
  1034. *
  1035. * @private
  1036. * @static
  1037. * @name Cache
  1038. * @memberOf _.memoize
  1039. */
  1040. function MapCache() {
  1041. this.__data__ = {};
  1042. }
  1043. /**
  1044. * Removes `key` and its value from the cache.
  1045. *
  1046. * @private
  1047. * @name delete
  1048. * @memberOf _.memoize.Cache
  1049. * @param {string} key The key of the value to remove.
  1050. * @returns {boolean} Returns `true` if the entry was removed successfully, else `false`.
  1051. */
  1052. function mapDelete(key) {
  1053. return this.has(key) && delete this.__data__[key];
  1054. }
  1055. /**
  1056. * Gets the cached value for `key`.
  1057. *
  1058. * @private
  1059. * @name get
  1060. * @memberOf _.memoize.Cache
  1061. * @param {string} key The key of the value to get.
  1062. * @returns {*} Returns the cached value.
  1063. */
  1064. function mapGet(key) {
  1065. return key == '__proto__' ? undefined : this.__data__[key];
  1066. }
  1067. /**
  1068. * Checks if a cached value for `key` exists.
  1069. *
  1070. * @private
  1071. * @name has
  1072. * @memberOf _.memoize.Cache
  1073. * @param {string} key The key of the entry to check.
  1074. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  1075. */
  1076. function mapHas(key) {
  1077. return key != '__proto__' && hasOwnProperty.call(this.__data__, key);
  1078. }
  1079. /**
  1080. * Sets `value` to `key` of the cache.
  1081. *
  1082. * @private
  1083. * @name set
  1084. * @memberOf _.memoize.Cache
  1085. * @param {string} key The key of the value to cache.
  1086. * @param {*} value The value to cache.
  1087. * @returns {Object} Returns the cache object.
  1088. */
  1089. function mapSet(key, value) {
  1090. if (key != '__proto__') {
  1091. this.__data__[key] = value;
  1092. }
  1093. return this;
  1094. }
  1095. /*------------------------------------------------------------------------*/
  1096. /**
  1097. *
  1098. * Creates a cache object to store unique values.
  1099. *
  1100. * @private
  1101. * @param {Array} [values] The values to cache.
  1102. */
  1103. function SetCache(values) {
  1104. var length = values ? values.length : 0;
  1105. this.data = { 'hash': nativeCreate(null), 'set': new Set };
  1106. while (length--) {
  1107. this.push(values[length]);
  1108. }
  1109. }
  1110. /**
  1111. * Checks if `value` is in `cache` mimicking the return signature of
  1112. * `_.indexOf` by returning `0` if the value is found, else `-1`.
  1113. *
  1114. * @private
  1115. * @param {Object} cache The cache to search.
  1116. * @param {*} value The value to search for.
  1117. * @returns {number} Returns `0` if `value` is found, else `-1`.
  1118. */
  1119. function cacheIndexOf(cache, value) {
  1120. var data = cache.data,
  1121. result = (typeof value == 'string' || isObject(value)) ? data.set.has(value) : data.hash[value];
  1122. return result ? 0 : -1;
  1123. }
  1124. /**
  1125. * Adds `value` to the cache.
  1126. *
  1127. * @private
  1128. * @name push
  1129. * @memberOf SetCache
  1130. * @param {*} value The value to cache.
  1131. */
  1132. function cachePush(value) {
  1133. var data = this.data;
  1134. if (typeof value == 'string' || isObject(value)) {
  1135. data.set.add(value);
  1136. } else {
  1137. data.hash[value] = true;
  1138. }
  1139. }
  1140. /*------------------------------------------------------------------------*/
  1141. /**
  1142. * Copies the values of `source` to `array`.
  1143. *
  1144. * @private
  1145. * @param {Array} source The array to copy values from.
  1146. * @param {Array} [array=[]] The array to copy values to.
  1147. * @returns {Array} Returns `array`.
  1148. */
  1149. function arrayCopy(source, array) {
  1150. var index = -1,
  1151. length = source.length;
  1152. array || (array = Array(length));
  1153. while (++index < length) {
  1154. array[index] = source[index];
  1155. }
  1156. return array;
  1157. }
  1158. /**
  1159. * A specialized version of `_.forEach` for arrays without support for callback
  1160. * shorthands and `this` binding.
  1161. *
  1162. * @private
  1163. * @param {Array} array The array to iterate over.
  1164. * @param {Function} iteratee The function invoked per iteration.
  1165. * @returns {Array} Returns `array`.
  1166. */
  1167. function arrayEach(array, iteratee) {
  1168. var index = -1,
  1169. length = array.length;
  1170. while (++index < length) {
  1171. if (iteratee(array[index], index, array) === false) {
  1172. break;
  1173. }
  1174. }
  1175. return array;
  1176. }
  1177. /**
  1178. * A specialized version of `_.forEachRight` for arrays without support for
  1179. * callback shorthands and `this` binding.
  1180. *
  1181. * @private
  1182. * @param {Array} array The array to iterate over.
  1183. * @param {Function} iteratee The function invoked per iteration.
  1184. * @returns {Array} Returns `array`.
  1185. */
  1186. function arrayEachRight(array, iteratee) {
  1187. var length = array.length;
  1188. while (length--) {
  1189. if (iteratee(array[length], length, array) === false) {
  1190. break;
  1191. }
  1192. }
  1193. return array;
  1194. }
  1195. /**
  1196. * A specialized version of `_.every` for arrays without support for callback
  1197. * shorthands and `this` binding.
  1198. *
  1199. * @private
  1200. * @param {Array} array The array to iterate over.
  1201. * @param {Function} predicate The function invoked per iteration.
  1202. * @returns {boolean} Returns `true` if all elements pass the predicate check,
  1203. * else `false`.
  1204. */
  1205. function arrayEvery(array, predicate) {
  1206. var index = -1,
  1207. length = array.length;
  1208. while (++index < length) {
  1209. if (!predicate(array[index], index, array)) {
  1210. return false;
  1211. }
  1212. }
  1213. return true;
  1214. }
  1215. /**
  1216. * A specialized version of `baseExtremum` for arrays which invokes `iteratee`
  1217. * with one argument: (value).
  1218. *
  1219. * @private
  1220. * @param {Array} array The array to iterate over.
  1221. * @param {Function} iteratee The function invoked per iteration.
  1222. * @param {Function} comparator The function used to compare values.
  1223. * @param {*} exValue The initial extremum value.
  1224. * @returns {*} Returns the extremum value.
  1225. */
  1226. function arrayExtremum(array, iteratee, comparator, exValue) {
  1227. var index = -1,
  1228. length = array.length,
  1229. computed = exValue,
  1230. result = computed;
  1231. while (++index < length) {
  1232. var value = array[index],
  1233. current = +iteratee(value);
  1234. if (comparator(current, computed)) {
  1235. computed = current;
  1236. result = value;
  1237. }
  1238. }
  1239. return result;
  1240. }
  1241. /**
  1242. * A specialized version of `_.filter` for arrays without support for callback
  1243. * shorthands and `this` binding.
  1244. *
  1245. * @private
  1246. * @param {Array} array The array to iterate over.
  1247. * @param {Function} predicate The function invoked per iteration.
  1248. * @returns {Array} Returns the new filtered array.
  1249. */
  1250. function arrayFilter(array, predicate) {
  1251. var index = -1,
  1252. length = array.length,
  1253. resIndex = -1,
  1254. result = [];
  1255. while (++index < length) {
  1256. var value = array[index];
  1257. if (predicate(value, index, array)) {
  1258. result[++resIndex] = value;
  1259. }
  1260. }
  1261. return result;
  1262. }
  1263. /**
  1264. * A specialized version of `_.map` for arrays without support for callback
  1265. * shorthands and `this` binding.
  1266. *
  1267. * @private
  1268. * @param {Array} array The array to iterate over.
  1269. * @param {Function} iteratee The function invoked per iteration.
  1270. * @returns {Array} Returns the new mapped array.
  1271. */
  1272. function arrayMap(array, iteratee) {
  1273. var index = -1,
  1274. length = array.length,
  1275. result = Array(length);
  1276. while (++index < length) {
  1277. result[index] = iteratee(array[index], index, array);
  1278. }
  1279. return result;
  1280. }
  1281. /**
  1282. * A specialized version of `_.reduce` for arrays without support for callback
  1283. * shorthands and `this` binding.
  1284. *
  1285. * @private
  1286. * @param {Array} array The array to iterate over.
  1287. * @param {Function} iteratee The function invoked per iteration.
  1288. * @param {*} [accumulator] The initial value.
  1289. * @param {boolean} [initFromArray] Specify using the first element of `array`
  1290. * as the initial value.
  1291. * @returns {*} Returns the accumulated value.
  1292. */
  1293. function arrayReduce(array, iteratee, accumulator, initFromArray) {
  1294. var index = -1,
  1295. length = array.length;
  1296. if (initFromArray && length) {
  1297. accumulator = array[++index];
  1298. }
  1299. while (++index < length) {
  1300. accumulator = iteratee(accumulator, array[index], index, array);
  1301. }
  1302. return accumulator;
  1303. }
  1304. /**
  1305. * A specialized version of `_.reduceRight` for arrays without support for
  1306. * callback shorthands and `this` binding.
  1307. *
  1308. * @private
  1309. * @param {Array} array The array to iterate over.
  1310. * @param {Function} iteratee The function invoked per iteration.
  1311. * @param {*} [accumulator] The initial value.
  1312. * @param {boolean} [initFromArray] Specify using the last element of `array`
  1313. * as the initial value.
  1314. * @returns {*} Returns the accumulated value.
  1315. */
  1316. function arrayReduceRight(array, iteratee, accumulator, initFromArray) {
  1317. var length = array.length;
  1318. if (initFromArray && length) {
  1319. accumulator = array[--length];
  1320. }
  1321. while (length--) {
  1322. accumulator = iteratee(accumulator, array[length], length, array);
  1323. }
  1324. return accumulator;
  1325. }
  1326. /**
  1327. * A specialized version of `_.some` for arrays without support for callback
  1328. * shorthands and `this` binding.
  1329. *
  1330. * @private
  1331. * @param {Array} array The array to iterate over.
  1332. * @param {Function} predicate The function invoked per iteration.
  1333. * @returns {boolean} Returns `true` if any element passes the predicate check,
  1334. * else `false`.
  1335. */
  1336. function arraySome(array, predicate) {
  1337. var index = -1,
  1338. length = array.length;
  1339. while (++index < length) {
  1340. if (predicate(array[index], index, array)) {
  1341. return true;
  1342. }
  1343. }
  1344. return false;
  1345. }
  1346. /**
  1347. * A specialized version of `_.sum` for arrays without support for iteratees.
  1348. *
  1349. * @private
  1350. * @param {Array} array The array to iterate over.
  1351. * @returns {number} Returns the sum.
  1352. */
  1353. function arraySum(array) {
  1354. var length = array.length,
  1355. result = 0;
  1356. while (length--) {
  1357. result += +array[length] || 0;
  1358. }
  1359. return result;
  1360. }
  1361. /**
  1362. * Used by `_.defaults` to customize its `_.assign` use.
  1363. *
  1364. * @private
  1365. * @param {*} objectValue The destination object property value.
  1366. * @param {*} sourceValue The source object property value.
  1367. * @returns {*} Returns the value to assign to the destination object.
  1368. */
  1369. function assignDefaults(objectValue, sourceValue) {
  1370. return objectValue === undefined ? sourceValue : objectValue;
  1371. }
  1372. /**
  1373. * Used by `_.template` to customize its `_.assign` use.
  1374. *
  1375. * **Note:** This function is like `assignDefaults` except that it ignores
  1376. * inherited property values when checking if a property is `undefined`.
  1377. *
  1378. * @private
  1379. * @param {*} objectValue The destination object property value.
  1380. * @param {*} sourceValue The source object property value.
  1381. * @param {string} key The key associated with the object and source values.
  1382. * @param {Object} object The destination object.
  1383. * @returns {*} Returns the value to assign to the destination object.
  1384. */
  1385. function assignOwnDefaults(objectValue, sourceValue, key, object) {
  1386. return (objectValue === undefined || !hasOwnProperty.call(object, key))
  1387. ? sourceValue
  1388. : objectValue;
  1389. }
  1390. /**
  1391. * A specialized version of `_.assign` for customizing assigned values without
  1392. * support for argument juggling, multiple sources, and `this` binding `customizer`
  1393. * functions.
  1394. *
  1395. * @private
  1396. * @param {Object} object The destination object.
  1397. * @param {Object} source The source object.
  1398. * @param {Function} customizer The function to customize assigned values.
  1399. * @returns {Object} Returns `object`.
  1400. */
  1401. function assignWith(object, source, customizer) {
  1402. var index = -1,
  1403. props = keys(source),
  1404. length = props.length;
  1405. while (++index < length) {
  1406. var key = props[index],
  1407. value = object[key],
  1408. result = customizer(value, source[key], key, object, source);
  1409. if ((result === result ? (result !== value) : (value === value)) ||
  1410. (value === undefined && !(key in object))) {
  1411. object[key] = result;
  1412. }
  1413. }
  1414. return object;
  1415. }
  1416. /**
  1417. * The base implementation of `_.assign` without support for argument juggling,
  1418. * multiple sources, and `customizer` functions.
  1419. *
  1420. * @private
  1421. * @param {Object} object The destination object.
  1422. * @param {Object} source The source object.
  1423. * @returns {Object} Returns `object`.
  1424. */
  1425. function baseAssign(object, source) {
  1426. return source == null
  1427. ? object
  1428. : baseCopy(source, keys(source), object);
  1429. }
  1430. /**
  1431. * The base implementation of `_.at` without support for string collections
  1432. * and individual key arguments.
  1433. *
  1434. * @private
  1435. * @param {Array|Object} collection The collection to iterate over.
  1436. * @param {number[]|string[]} props The property names or indexes of elements to pick.
  1437. * @returns {Array} Returns the new array of picked elements.
  1438. */
  1439. function baseAt(collection, props) {
  1440. var index = -1,
  1441. isNil = collection == null,
  1442. isArr = !isNil && isArrayLike(collection),
  1443. length = isArr ? collection.length : 0,
  1444. propsLength = props.length,
  1445. result = Array(propsLength);
  1446. while(++index < propsLength) {
  1447. var key = props[index];
  1448. if (isArr) {
  1449. result[index] = isIndex(key, length) ? collection[key] : undefined;
  1450. } else {
  1451. result[index] = isNil ? undefined : collection[key];
  1452. }
  1453. }
  1454. return result;
  1455. }
  1456. /**
  1457. * Copies properties of `source` to `object`.
  1458. *
  1459. * @private
  1460. * @param {Object} source The object to copy properties from.
  1461. * @param {Array} props The property names to copy.
  1462. * @param {Object} [object={}] The object to copy properties to.
  1463. * @returns {Object} Returns `object`.
  1464. */
  1465. function baseCopy(source, props, object) {
  1466. object || (object = {});
  1467. var index = -1,
  1468. length = props.length;
  1469. while (++index < length) {
  1470. var key = props[index];
  1471. object[key] = source[key];
  1472. }
  1473. return object;
  1474. }
  1475. /**
  1476. * The base implementation of `_.callback` which supports specifying the
  1477. * number of arguments to provide to `func`.
  1478. *
  1479. * @private
  1480. * @param {*} [func=_.identity] The value to convert to a callback.
  1481. * @param {*} [thisArg] The `this` binding of `func`.
  1482. * @param {number} [argCount] The number of arguments to provide to `func`.
  1483. * @returns {Function} Returns the callback.
  1484. */
  1485. function baseCallback(func, thisArg, argCount) {
  1486. var type = typeof func;
  1487. if (type == 'function') {
  1488. return thisArg === undefined
  1489. ? func
  1490. : bindCallback(func, thisArg, argCount);
  1491. }
  1492. if (func == null) {
  1493. return identity;
  1494. }
  1495. if (type == 'object') {
  1496. return baseMatches(func);
  1497. }
  1498. return thisArg === undefined
  1499. ? property(func)
  1500. : baseMatchesProperty(func, thisArg);
  1501. }
  1502. /**
  1503. * The base implementation of `_.clone` without support for argument juggling
  1504. * and `this` binding `customizer` functions.
  1505. *
  1506. * @private
  1507. * @param {*} value The value to clone.
  1508. * @param {boolean} [isDeep] Specify a deep clone.
  1509. * @param {Function} [customizer] The function to customize cloning values.
  1510. * @param {string} [key] The key of `value`.
  1511. * @param {Object} [object] The object `value` belongs to.
  1512. * @param {Array} [stackA=[]] Tracks traversed source objects.
  1513. * @param {Array} [stackB=[]] Associates clones with source counterparts.
  1514. * @returns {*} Returns the cloned value.
  1515. */
  1516. function baseClone(value, isDeep, customizer, key, object, stackA, stackB) {
  1517. var result;
  1518. if (customizer) {
  1519. result = object ? customizer(value, key, object) : customizer(value);
  1520. }
  1521. if (result !== undefined) {
  1522. return result;
  1523. }
  1524. if (!isObject(value)) {
  1525. return value;
  1526. }
  1527. var isArr = isArray(value);
  1528. if (isArr) {
  1529. result = initCloneArray(value);
  1530. if (!isDeep) {
  1531. return arrayCopy(value, result);
  1532. }
  1533. } else {
  1534. var tag = objToString.call(value),
  1535. isFunc = tag == funcTag;
  1536. if (tag == objectTag || tag == argsTag || (isFunc && !object)) {
  1537. result = initCloneObject(isFunc ? {} : value);
  1538. if (!isDeep) {
  1539. return baseAssign(result, value);
  1540. }
  1541. } else {
  1542. return cloneableTags[tag]
  1543. ? initCloneByTag(value, tag, isDeep)
  1544. : (object ? value : {});
  1545. }
  1546. }
  1547. // Check for circular references and return corresponding clone.
  1548. stackA || (stackA = []);
  1549. stackB || (stackB = []);
  1550. var length = stackA.length;
  1551. while (length--) {
  1552. if (stackA[length] == value) {
  1553. return stackB[length];
  1554. }
  1555. }
  1556. // Add the source value to the stack of traversed objects and associate it with its clone.
  1557. stackA.push(value);
  1558. stackB.push(result);
  1559. // Recursively populate clone (susceptible to call stack limits).
  1560. (isArr ? arrayEach : baseForOwn)(value, function(subValue, key) {
  1561. result[key] = baseClone(subValue, isDeep, customizer, key, value, stackA, stackB);
  1562. });
  1563. return result;
  1564. }
  1565. /**
  1566. * The base implementation of `_.create` without support for assigning
  1567. * properties to the created object.
  1568. *
  1569. * @private
  1570. * @param {Object} prototype The object to inherit from.
  1571. * @returns {Object} Returns the new object.
  1572. */
  1573. var baseCreate = (function() {
  1574. function object() {}
  1575. return function(prototype) {
  1576. if (isObject(prototype)) {
  1577. object.prototype = prototype;
  1578. var result = new object;
  1579. object.prototype = null;
  1580. }
  1581. return result || {};
  1582. };
  1583. }());
  1584. /**
  1585. * The base implementation of `_.delay` and `_.defer` which accepts an index
  1586. * of where to slice the arguments to provide to `func`.
  1587. *
  1588. * @private
  1589. * @param {Function} func The function to delay.
  1590. * @param {number} wait The number of milliseconds to delay invocation.
  1591. * @param {Object} args The arguments provide to `func`.
  1592. * @returns {number} Returns the timer id.
  1593. */
  1594. function baseDelay(func, wait, args) {
  1595. if (typeof func != 'function') {
  1596. throw new TypeError(FUNC_ERROR_TEXT);
  1597. }
  1598. return setTimeout(function() { func.apply(undefined, args); }, wait);
  1599. }
  1600. /**
  1601. * The base implementation of `_.difference` which accepts a single array
  1602. * of values to exclude.
  1603. *
  1604. * @private
  1605. * @param {Array} array The array to inspect.
  1606. * @param {Array} values The values to exclude.
  1607. * @returns {Array} Returns the new array of filtered values.
  1608. */
  1609. function baseDifference(array, values) {
  1610. var length = array ? array.length : 0,
  1611. result = [];
  1612. if (!length) {
  1613. return result;
  1614. }
  1615. var index = -1,
  1616. indexOf = getIndexOf(),
  1617. isCommon = indexOf == baseIndexOf,
  1618. cache = (isCommon && values.length >= 200) ? createCache(values) : null,
  1619. valuesLength = values.length;
  1620. if (cache) {
  1621. indexOf = cacheIndexOf;
  1622. isCommon = false;
  1623. values = cache;
  1624. }
  1625. outer:
  1626. while (++index < length) {
  1627. var value = array[index];
  1628. if (isCommon && value === value) {
  1629. var valuesIndex = valuesLength;
  1630. while (valuesIndex--) {
  1631. if (values[valuesIndex] === value) {
  1632. continue outer;
  1633. }
  1634. }
  1635. result.push(value);
  1636. }
  1637. else if (indexOf(values, value, 0) < 0) {
  1638. result.push(value);
  1639. }
  1640. }
  1641. return result;
  1642. }
  1643. /**
  1644. * The base implementation of `_.forEach` without support for callback
  1645. * shorthands and `this` binding.
  1646. *
  1647. * @private
  1648. * @param {Array|Object|string} collection The collection to iterate over.
  1649. * @param {Function} iteratee The function invoked per iteration.
  1650. * @returns {Array|Object|string} Returns `collection`.
  1651. */
  1652. var baseEach = createBaseEach(baseForOwn);
  1653. /**
  1654. * The base implementation of `_.forEachRight` without support for callback
  1655. * shorthands and `this` binding.
  1656. *
  1657. * @private
  1658. * @param {Array|Object|string} collection The collection to iterate over.
  1659. * @param {Function} iteratee The function invoked per iteration.
  1660. * @returns {Array|Object|string} Returns `collection`.
  1661. */
  1662. var baseEachRight = createBaseEach(baseForOwnRight, true);
  1663. /**
  1664. * The base implementation of `_.every` without support for callback
  1665. * shorthands and `this` binding.
  1666. *
  1667. * @private
  1668. * @param {Array|Object|string} collection The collection to iterate over.
  1669. * @param {Function} predicate The function invoked per iteration.
  1670. * @returns {boolean} Returns `true` if all elements pass the predicate check,
  1671. * else `false`
  1672. */
  1673. function baseEvery(collection, predicate) {
  1674. var result = true;
  1675. baseEach(collection, function(value, index, collection) {
  1676. result = !!predicate(value, index, collection);
  1677. return result;
  1678. });
  1679. return result;
  1680. }
  1681. /**
  1682. * Gets the extremum value of `collection` invoking `iteratee` for each value
  1683. * in `collection` to generate the criterion by which the value is ranked.
  1684. * The `iteratee` is invoked with three arguments: (value, index|key, collection).
  1685. *
  1686. * @private
  1687. * @param {Array|Object|string} collection The collection to iterate over.
  1688. * @param {Function} iteratee The function invoked per iteration.
  1689. * @param {Function} comparator The function used to compare values.
  1690. * @param {*} exValue The initial extremum value.
  1691. * @returns {*} Returns the extremum value.
  1692. */
  1693. function baseExtremum(collection, iteratee, comparator, exValue) {
  1694. var computed = exValue,
  1695. result = computed;
  1696. baseEach(collection, function(value, index, collection) {
  1697. var current = +iteratee(value, index, collection);
  1698. if (comparator(current, computed) || (current === exValue && current === result)) {
  1699. computed = current;
  1700. result = value;
  1701. }
  1702. });
  1703. return result;
  1704. }
  1705. /**
  1706. * The base implementation of `_.fill` without an iteratee call guard.
  1707. *
  1708. * @private
  1709. * @param {Array} array The array to fill.
  1710. * @param {*} value The value to fill `array` with.
  1711. * @param {number} [start=0] The start position.
  1712. * @param {number} [end=array.length] The end position.
  1713. * @returns {Array} Returns `array`.
  1714. */
  1715. function baseFill(array, value, start, end) {
  1716. var length = array.length;
  1717. start = start == null ? 0 : (+start || 0);
  1718. if (start < 0) {
  1719. start = -start > length ? 0 : (length + start);
  1720. }
  1721. end = (end === undefined || end > length) ? length : (+end || 0);
  1722. if (end < 0) {
  1723. end += length;
  1724. }
  1725. length = start > end ? 0 : (end >>> 0);
  1726. start >>>= 0;
  1727. while (start < length) {
  1728. array[start++] = value;
  1729. }
  1730. return array;
  1731. }
  1732. /**
  1733. * The base implementation of `_.filter` without support for callback
  1734. * shorthands and `this` binding.
  1735. *
  1736. * @private
  1737. * @param {Array|Object|string} collection The collection to iterate over.
  1738. * @param {Function} predicate The function invoked per iteration.
  1739. * @returns {Array} Returns the new filtered array.
  1740. */
  1741. function baseFilter(collection, predicate) {
  1742. var result = [];
  1743. baseEach(collection, function(value, index, collection) {
  1744. if (predicate(value, index, collection)) {
  1745. result.push(value);
  1746. }
  1747. });
  1748. return result;
  1749. }
  1750. /**
  1751. * The base implementation of `_.find`, `_.findLast`, `_.findKey`, and `_.findLastKey`,
  1752. * without support for callback shorthands and `this` binding, which iterates
  1753. * over `collection` using the provided `eachFunc`.
  1754. *
  1755. * @private
  1756. * @param {Array|Object|string} collection The collection to search.
  1757. * @param {Function} predicate The function invoked per iteration.
  1758. * @param {Function} eachFunc The function to iterate over `collection`.
  1759. * @param {boolean} [retKey] Specify returning the key of the found element
  1760. * instead of the element itself.
  1761. * @returns {*} Returns the found element or its key, else `undefined`.
  1762. */
  1763. function baseFind(collection, predicate, eachFunc, retKey) {
  1764. var result;
  1765. eachFunc(collection, function(value, key, collection) {
  1766. if (predicate(value, key, collection)) {
  1767. result = retKey ? key : value;
  1768. return false;
  1769. }
  1770. });
  1771. return result;
  1772. }
  1773. /**
  1774. * The base implementation of `_.flatten` with added support for restricting
  1775. * flattening and specifying the start index.
  1776. *
  1777. * @private
  1778. * @param {Array} array The array to flatten.
  1779. * @param {boolean} [isDeep] Specify a deep flatten.
  1780. * @param {boolean} [isStrict] Restrict flattening to arrays-like objects.
  1781. * @returns {Array} Returns the new flattened array.
  1782. */
  1783. function baseFlatten(array, isDeep, isStrict) {
  1784. var index = -1,
  1785. length = array.length,
  1786. resIndex = -1,
  1787. result = [];
  1788. while (++index < length) {
  1789. var value = array[index];
  1790. if (isObjectLike(value) && isArrayLike(value) &&
  1791. (isStrict || isArray(value) || isArguments(value))) {
  1792. if (isDeep) {
  1793. // Recursively flatten arrays (susceptible to call stack limits).
  1794. value = baseFlatten(value, isDeep, isStrict);
  1795. }
  1796. var valIndex = -1,
  1797. valLength = value.length;
  1798. while (++valIndex < valLength) {
  1799. result[++resIndex] = value[valIndex];
  1800. }
  1801. } else if (!isStrict) {
  1802. result[++resIndex] = value;
  1803. }
  1804. }
  1805. return result;
  1806. }
  1807. /**
  1808. * The base implementation of `baseForIn` and `baseForOwn` which iterates
  1809. * over `object` properties returned by `keysFunc` invoking `iteratee` for
  1810. * each property. Iteratee functions may exit iteration early by explicitly
  1811. * returning `false`.
  1812. *
  1813. * @private
  1814. * @param {Object} object The object to iterate over.
  1815. * @param {Function} iteratee The function invoked per iteration.
  1816. * @param {Function} keysFunc The function to get the keys of `object`.
  1817. * @returns {Object} Returns `object`.
  1818. */
  1819. var baseFor = createBaseFor();
  1820. /**
  1821. * This function is like `baseFor` except that it iterates over properties
  1822. * in the opposite order.
  1823. *
  1824. * @private
  1825. * @param {Object} object The object to iterate over.
  1826. * @param {Function} iteratee The function invoked per iteration.
  1827. * @param {Function} keysFunc The function to get the keys of `object`.
  1828. * @returns {Object} Returns `object`.
  1829. */
  1830. var baseForRight = createBaseFor(true);
  1831. /**
  1832. * The base implementation of `_.forIn` without support for callback
  1833. * shorthands and `this` binding.
  1834. *
  1835. * @private
  1836. * @param {Object} object The object to iterate over.
  1837. * @param {Function} iteratee The function invoked per iteration.
  1838. * @returns {Object} Returns `object`.
  1839. */
  1840. function baseForIn(object, iteratee) {
  1841. return baseFor(object, iteratee, keysIn);
  1842. }
  1843. /**
  1844. * The base implementation of `_.forOwn` without support for callback
  1845. * shorthands and `this` binding.
  1846. *
  1847. * @private
  1848. * @param {Object} object The object to iterate over.
  1849. * @param {Function} iteratee The function invoked per iteration.
  1850. * @returns {Object} Returns `object`.
  1851. */
  1852. function baseForOwn(object, iteratee) {
  1853. return baseFor(object, iteratee, keys);
  1854. }
  1855. /**
  1856. * The base implementation of `_.forOwnRight` without support for callback
  1857. * shorthands and `this` binding.
  1858. *
  1859. * @private
  1860. * @param {Object} object The object to iterate over.
  1861. * @param {Function} iteratee The function invoked per iteration.
  1862. * @returns {Object} Returns `object`.
  1863. */
  1864. function baseForOwnRight(object, iteratee) {
  1865. return baseForRight(object, iteratee, keys);
  1866. }
  1867. /**
  1868. * The base implementation of `_.functions` which creates an array of
  1869. * `object` function property names filtered from those provided.
  1870. *
  1871. * @private
  1872. * @param {Object} object The object to inspect.
  1873. * @param {Array} props The property names to filter.
  1874. * @returns {Array} Returns the new array of filtered property names.
  1875. */
  1876. function baseFunctions(object, props) {
  1877. var index = -1,
  1878. length = props.length,
  1879. resIndex = -1,
  1880. result = [];
  1881. while (++index < length) {
  1882. var key = props[index];
  1883. if (isFunction(object[key])) {
  1884. result[++resIndex] = key;
  1885. }
  1886. }
  1887. return result;
  1888. }
  1889. /**
  1890. * The base implementation of `get` without support for string paths
  1891. * and default values.
  1892. *
  1893. * @private
  1894. * @param {Object} object The object to query.
  1895. * @param {Array} path The path of the property to get.
  1896. * @param {string} [pathKey] The key representation of path.
  1897. * @returns {*} Returns the resolved value.
  1898. */
  1899. function baseGet(object, path, pathKey) {
  1900. if (object == null) {
  1901. return;
  1902. }
  1903. if (pathKey !== undefined && pathKey in toObject(object)) {
  1904. path = [pathKey];
  1905. }
  1906. var index = 0,
  1907. length = path.length;
  1908. while (object != null && index < length) {
  1909. object = object[path[index++]];
  1910. }
  1911. return (index && index == length) ? object : undefined;
  1912. }
  1913. /**
  1914. * The base implementation of `_.isEqual` without support for `this` binding
  1915. * `customizer` functions.
  1916. *
  1917. * @private
  1918. * @param {*} value The value to compare.
  1919. * @param {*} other The other value to compare.
  1920. * @param {Function} [customizer] The function to customize comparing values.
  1921. * @param {boolean} [isLoose] Specify performing partial comparisons.
  1922. * @param {Array} [stackA] Tracks traversed `value` objects.
  1923. * @param {Array} [stackB] Tracks traversed `other` objects.
  1924. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  1925. */
  1926. function baseIsEqual(value, other, customizer, isLoose, stackA, stackB) {
  1927. if (value === other) {
  1928. return true;
  1929. }
  1930. if (value == null || other == null || (!isObject(value) && !isObjectLike(other))) {
  1931. return value !== value && other !== other;
  1932. }
  1933. return baseIsEqualDeep(value, other, baseIsEqual, customizer, isLoose, stackA, stackB);
  1934. }
  1935. /**
  1936. * A specialized version of `baseIsEqual` for arrays and objects which performs
  1937. * deep comparisons and tracks traversed objects enabling objects with circular
  1938. * references to be compared.
  1939. *
  1940. * @private
  1941. * @param {Object} object The object to compare.
  1942. * @param {Object} other The other object to compare.
  1943. * @param {Function} equalFunc The function to determine equivalents of values.
  1944. * @param {Function} [customizer] The function to customize comparing objects.
  1945. * @param {boolean} [isLoose] Specify performing partial comparisons.
  1946. * @param {Array} [stackA=[]] Tracks traversed `value` objects.
  1947. * @param {Array} [stackB=[]] Tracks traversed `other` objects.
  1948. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  1949. */
  1950. function baseIsEqualDeep(object, other, equalFunc, customizer, isLoose, stackA, stackB) {
  1951. var objIsArr = isArray(object),
  1952. othIsArr = isArray(other),
  1953. objTag = arrayTag,
  1954. othTag = arrayTag;
  1955. if (!objIsArr) {
  1956. objTag = objToString.call(object);
  1957. if (objTag == argsTag) {
  1958. objTag = objectTag;
  1959. } else if (objTag != objectTag) {
  1960. objIsArr = isTypedArray(object);
  1961. }
  1962. }
  1963. if (!othIsArr) {
  1964. othTag = objToString.call(other);
  1965. if (othTag == argsTag) {
  1966. othTag = objectTag;
  1967. } else if (othTag != objectTag) {
  1968. othIsArr = isTypedArray(other);
  1969. }
  1970. }
  1971. var objIsObj = objTag == objectTag,
  1972. othIsObj = othTag == objectTag,
  1973. isSameTag = objTag == othTag;
  1974. if (isSameTag && !(objIsArr || objIsObj)) {
  1975. return equalByTag(object, other, objTag);
  1976. }
  1977. if (!isLoose) {
  1978. var objIsWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'),
  1979. othIsWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__');
  1980. if (objIsWrapped || othIsWrapped) {
  1981. return equalFunc(objIsWrapped ? object.value() : object, othIsWrapped ? other.value() : other, customizer, isLoose, stackA, stackB);
  1982. }
  1983. }
  1984. if (!isSameTag) {
  1985. return false;
  1986. }
  1987. // Assume cyclic values are equal.
  1988. // For more information on detecting circular references see https://es5.github.io/#JO.
  1989. stackA || (stackA = []);
  1990. stackB || (stackB = []);
  1991. var length = stackA.length;
  1992. while (length--) {
  1993. if (stackA[length] == object) {
  1994. return stackB[length] == other;
  1995. }
  1996. }
  1997. // Add `object` and `other` to the stack of traversed objects.
  1998. stackA.push(object);
  1999. stackB.push(other);
  2000. var result = (objIsArr ? equalArrays : equalObjects)(object, other, equalFunc, customizer, isLoose, stackA, stackB);
  2001. stackA.pop();
  2002. stackB.pop();
  2003. return result;
  2004. }
  2005. /**
  2006. * The base implementation of `_.isMatch` without support for callback
  2007. * shorthands and `this` binding.
  2008. *
  2009. * @private
  2010. * @param {Object} object The object to inspect.
  2011. * @param {Array} matchData The propery names, values, and compare flags to match.
  2012. * @param {Function} [customizer] The function to customize comparing objects.
  2013. * @returns {boolean} Returns `true` if `object` is a match, else `false`.
  2014. */
  2015. function baseIsMatch(object, matchData, customizer) {
  2016. var index = matchData.length,
  2017. length = index,
  2018. noCustomizer = !customizer;
  2019. if (object == null) {
  2020. return !length;
  2021. }
  2022. object = toObject(object);
  2023. while (index--) {
  2024. var data = matchData[index];
  2025. if ((noCustomizer && data[2])
  2026. ? data[1] !== object[data[0]]
  2027. : !(data[0] in object)
  2028. ) {
  2029. return false;
  2030. }
  2031. }
  2032. while (++index < length) {
  2033. data = matchData[index];
  2034. var key = data[0],
  2035. objValue = object[key],
  2036. srcValue = data[1];
  2037. if (noCustomizer && data[2]) {
  2038. if (objValue === undefined && !(key in object)) {
  2039. return false;
  2040. }
  2041. } else {
  2042. var result = customizer ? customizer(objValue, srcValue, key) : undefined;
  2043. if (!(result === undefined ? baseIsEqual(srcValue, objValue, customizer, true) : result)) {
  2044. return false;
  2045. }
  2046. }
  2047. }
  2048. return true;
  2049. }
  2050. /**
  2051. * The base implementation of `_.map` without support for callback shorthands
  2052. * and `this` binding.
  2053. *
  2054. * @private
  2055. * @param {Array|Object|string} collection The collection to iterate over.
  2056. * @param {Function} iteratee The function invoked per iteration.
  2057. * @returns {Array} Returns the new mapped array.
  2058. */
  2059. function baseMap(collection, iteratee) {
  2060. var index = -1,
  2061. result = isArrayLike(collection) ? Array(collection.length) : [];
  2062. baseEach(collection, function(value, key, collection) {
  2063. result[++index] = iteratee(value, key, collection);
  2064. });
  2065. return result;
  2066. }
  2067. /**
  2068. * The base implementation of `_.matches` which does not clone `source`.
  2069. *
  2070. * @private
  2071. * @param {Object} source The object of property values to match.
  2072. * @returns {Function} Returns the new function.
  2073. */
  2074. function baseMatches(source) {
  2075. var matchData = getMatchData(source);
  2076. if (matchData.length == 1 && matchData[0][2]) {
  2077. var key = matchData[0][0],
  2078. value = matchData[0][1];
  2079. return function(object) {
  2080. if (object == null) {
  2081. return false;
  2082. }
  2083. return object[key] === value && (value !== undefined || (key in toObject(object)));
  2084. };
  2085. }
  2086. return function(object) {
  2087. return baseIsMatch(object, matchData);
  2088. };
  2089. }
  2090. /**
  2091. * The base implementation of `_.matchesProperty` which does not clone `srcValue`.
  2092. *
  2093. * @private
  2094. * @param {string} path The path of the property to get.
  2095. * @param {*} srcValue The value to compare.
  2096. * @returns {Function} Returns the new function.
  2097. */
  2098. function baseMatchesProperty(path, srcValue) {
  2099. var isArr = isArray(path),
  2100. isCommon = isKey(path) && isStrictComparable(srcValue),
  2101. pathKey = (path + '');
  2102. path = toPath(path);
  2103. return function(object) {
  2104. if (object == null) {
  2105. return false;
  2106. }
  2107. var key = pathKey;
  2108. object = toObject(object);
  2109. if ((isArr || !isCommon) && !(key in object)) {
  2110. object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  2111. if (object == null) {
  2112. return false;
  2113. }
  2114. key = last(path);
  2115. object = toObject(object);
  2116. }
  2117. return object[key] === srcValue
  2118. ? (srcValue !== undefined || (key in object))
  2119. : baseIsEqual(srcValue, object[key], undefined, true);
  2120. };
  2121. }
  2122. /**
  2123. * The base implementation of `_.merge` without support for argument juggling,
  2124. * multiple sources, and `this` binding `customizer` functions.
  2125. *
  2126. * @private
  2127. * @param {Object} object The destination object.
  2128. * @param {Object} source The source object.
  2129. * @param {Function} [customizer] The function to customize merging properties.
  2130. * @param {Array} [stackA=[]] Tracks traversed source objects.
  2131. * @param {Array} [stackB=[]] Associates values with source counterparts.
  2132. * @returns {Object} Returns `object`.
  2133. */
  2134. function baseMerge(object, source, customizer, stackA, stackB) {
  2135. if (!isObject(object)) {
  2136. return object;
  2137. }
  2138. var isSrcArr = isArrayLike(source) && (isArray(source) || isTypedArray(source)),
  2139. props = isSrcArr ? null : keys(source);
  2140. arrayEach(props || source, function(srcValue, key) {
  2141. if (props) {
  2142. key = srcValue;
  2143. srcValue = source[key];
  2144. }
  2145. if (isObjectLike(srcValue)) {
  2146. stackA || (stackA = []);
  2147. stackB || (stackB = []);
  2148. baseMergeDeep(object, source, key, baseMerge, customizer, stackA, stackB);
  2149. }
  2150. else {
  2151. var value = object[key],
  2152. result = customizer ? customizer(value, srcValue, key, object, source) : undefined,
  2153. isCommon = result === undefined;
  2154. if (isCommon) {
  2155. result = srcValue;
  2156. }
  2157. if ((result !== undefined || (isSrcArr && !(key in object))) &&
  2158. (isCommon || (result === result ? (result !== value) : (value === value)))) {
  2159. object[key] = result;
  2160. }
  2161. }
  2162. });
  2163. return object;
  2164. }
  2165. /**
  2166. * A specialized version of `baseMerge` for arrays and objects which performs
  2167. * deep merges and tracks traversed objects enabling objects with circular
  2168. * references to be merged.
  2169. *
  2170. * @private
  2171. * @param {Object} object The destination object.
  2172. * @param {Object} source The source object.
  2173. * @param {string} key The key of the value to merge.
  2174. * @param {Function} mergeFunc The function to merge values.
  2175. * @param {Function} [customizer] The function to customize merging properties.
  2176. * @param {Array} [stackA=[]] Tracks traversed source objects.
  2177. * @param {Array} [stackB=[]] Associates values with source counterparts.
  2178. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  2179. */
  2180. function baseMergeDeep(object, source, key, mergeFunc, customizer, stackA, stackB) {
  2181. var length = stackA.length,
  2182. srcValue = source[key];
  2183. while (length--) {
  2184. if (stackA[length] == srcValue) {
  2185. object[key] = stackB[length];
  2186. return;
  2187. }
  2188. }
  2189. var value = object[key],
  2190. result = customizer ? customizer(value, srcValue, key, object, source) : undefined,
  2191. isCommon = result === undefined;
  2192. if (isCommon) {
  2193. result = srcValue;
  2194. if (isArrayLike(srcValue) && (isArray(srcValue) || isTypedArray(srcValue))) {
  2195. result = isArray(value)
  2196. ? value
  2197. : (isArrayLike(value) ? arrayCopy(value) : []);
  2198. }
  2199. else if (isPlainObject(srcValue) || isArguments(srcValue)) {
  2200. result = isArguments(value)
  2201. ? toPlainObject(value)
  2202. : (isPlainObject(value) ? value : {});
  2203. }
  2204. else {
  2205. isCommon = false;
  2206. }
  2207. }
  2208. // Add the source value to the stack of traversed objects and associate
  2209. // it with its merged value.
  2210. stackA.push(srcValue);
  2211. stackB.push(result);
  2212. if (isCommon) {
  2213. // Recursively merge objects and arrays (susceptible to call stack limits).
  2214. object[key] = mergeFunc(result, srcValue, customizer, stackA, stackB);
  2215. } else if (result === result ? (result !== value) : (value === value)) {
  2216. object[key] = result;
  2217. }
  2218. }
  2219. /**
  2220. * The base implementation of `_.property` without support for deep paths.
  2221. *
  2222. * @private
  2223. * @param {string} key The key of the property to get.
  2224. * @returns {Function} Returns the new function.
  2225. */
  2226. function baseProperty(key) {
  2227. return function(object) {
  2228. return object == null ? undefined : object[key];
  2229. };
  2230. }
  2231. /**
  2232. * A specialized version of `baseProperty` which supports deep paths.
  2233. *
  2234. * @private
  2235. * @param {Array|string} path The path of the property to get.
  2236. * @returns {Function} Returns the new function.
  2237. */
  2238. function basePropertyDeep(path) {
  2239. var pathKey = (path + '');
  2240. path = toPath(path);
  2241. return function(object) {
  2242. return baseGet(object, path, pathKey);
  2243. };
  2244. }
  2245. /**
  2246. * The base implementation of `_.pullAt` without support for individual
  2247. * index arguments and capturing the removed elements.
  2248. *
  2249. * @private
  2250. * @param {Array} array The array to modify.
  2251. * @param {number[]} indexes The indexes of elements to remove.
  2252. * @returns {Array} Returns `array`.
  2253. */
  2254. function basePullAt(array, indexes) {
  2255. var length = array ? indexes.length : 0;
  2256. while (length--) {
  2257. var index = indexes[length];
  2258. if (index != previous && isIndex(index)) {
  2259. var previous = index;
  2260. splice.call(array, index, 1);
  2261. }
  2262. }
  2263. return array;
  2264. }
  2265. /**
  2266. * The base implementation of `_.random` without support for argument juggling
  2267. * and returning floating-point numbers.
  2268. *
  2269. * @private
  2270. * @param {number} min The minimum possible value.
  2271. * @param {number} max The maximum possible value.
  2272. * @returns {number} Returns the random number.
  2273. */
  2274. function baseRandom(min, max) {
  2275. return min + floor(nativeRandom() * (max - min + 1));
  2276. }
  2277. /**
  2278. * The base implementation of `_.reduce` and `_.reduceRight` without support
  2279. * for callback shorthands and `this` binding, which iterates over `collection`
  2280. * using the provided `eachFunc`.
  2281. *
  2282. * @private
  2283. * @param {Array|Object|string} collection The collection to iterate over.
  2284. * @param {Function} iteratee The function invoked per iteration.
  2285. * @param {*} accumulator The initial value.
  2286. * @param {boolean} initFromCollection Specify using the first or last element
  2287. * of `collection` as the initial value.
  2288. * @param {Function} eachFunc The function to iterate over `collection`.
  2289. * @returns {*} Returns the accumulated value.
  2290. */
  2291. function baseReduce(collection, iteratee, accumulator, initFromCollection, eachFunc) {
  2292. eachFunc(collection, function(value, index, collection) {
  2293. accumulator = initFromCollection
  2294. ? (initFromCollection = false, value)
  2295. : iteratee(accumulator, value, index, collection);
  2296. });
  2297. return accumulator;
  2298. }
  2299. /**
  2300. * The base implementation of `setData` without support for hot loop detection.
  2301. *
  2302. * @private
  2303. * @param {Function} func The function to associate metadata with.
  2304. * @param {*} data The metadata.
  2305. * @returns {Function} Returns `func`.
  2306. */
  2307. var baseSetData = !metaMap ? identity : function(func, data) {
  2308. metaMap.set(func, data);
  2309. return func;
  2310. };
  2311. /**
  2312. * The base implementation of `_.slice` without an iteratee call guard.
  2313. *
  2314. * @private
  2315. * @param {Array} array The array to slice.
  2316. * @param {number} [start=0] The start position.
  2317. * @param {number} [end=array.length] The end position.
  2318. * @returns {Array} Returns the slice of `array`.
  2319. */
  2320. function baseSlice(array, start, end) {
  2321. var index = -1,
  2322. length = array.length;
  2323. start = start == null ? 0 : (+start || 0);
  2324. if (start < 0) {
  2325. start = -start > length ? 0 : (length + start);
  2326. }
  2327. end = (end === undefined || end > length) ? length : (+end || 0);
  2328. if (end < 0) {
  2329. end += length;
  2330. }
  2331. length = start > end ? 0 : ((end - start) >>> 0);
  2332. start >>>= 0;
  2333. var result = Array(length);
  2334. while (++index < length) {
  2335. result[index] = array[index + start];
  2336. }
  2337. return result;
  2338. }
  2339. /**
  2340. * The base implementation of `_.some` without support for callback shorthands
  2341. * and `this` binding.
  2342. *
  2343. * @private
  2344. * @param {Array|Object|string} collection The collection to iterate over.
  2345. * @param {Function} predicate The function invoked per iteration.
  2346. * @returns {boolean} Returns `true` if any element passes the predicate check,
  2347. * else `false`.
  2348. */
  2349. function baseSome(collection, predicate) {
  2350. var result;
  2351. baseEach(collection, function(value, index, collection) {
  2352. result = predicate(value, index, collection);
  2353. return !result;
  2354. });
  2355. return !!result;
  2356. }
  2357. /**
  2358. * The base implementation of `_.sortBy` which uses `comparer` to define
  2359. * the sort order of `array` and replaces criteria objects with their
  2360. * corresponding values.
  2361. *
  2362. * @private
  2363. * @param {Array} array The array to sort.
  2364. * @param {Function} comparer The function to define sort order.
  2365. * @returns {Array} Returns `array`.
  2366. */
  2367. function baseSortBy(array, comparer) {
  2368. var length = array.length;
  2369. array.sort(comparer);
  2370. while (length--) {
  2371. array[length] = array[length].value;
  2372. }
  2373. return array;
  2374. }
  2375. /**
  2376. * The base implementation of `_.sortByOrder` without param guards.
  2377. *
  2378. * @private
  2379. * @param {Array|Object|string} collection The collection to iterate over.
  2380. * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by.
  2381. * @param {boolean[]} orders The sort orders of `iteratees`.
  2382. * @returns {Array} Returns the new sorted array.
  2383. */
  2384. function baseSortByOrder(collection, iteratees, orders) {
  2385. var callback = getCallback(),
  2386. index = -1;
  2387. iteratees = arrayMap(iteratees, function(iteratee) { return callback(iteratee); });
  2388. var result = baseMap(collection, function(value) {
  2389. var criteria = arrayMap(iteratees, function(iteratee) { return iteratee(value); });
  2390. return { 'criteria': criteria, 'index': ++index, 'value': value };
  2391. });
  2392. return baseSortBy(result, function(object, other) {
  2393. return compareMultiple(object, other, orders);
  2394. });
  2395. }
  2396. /**
  2397. * The base implementation of `_.sum` without support for callback shorthands
  2398. * and `this` binding.
  2399. *
  2400. * @private
  2401. * @param {Array|Object|string} collection The collection to iterate over.
  2402. * @param {Function} iteratee The function invoked per iteration.
  2403. * @returns {number} Returns the sum.
  2404. */
  2405. function baseSum(collection, iteratee) {
  2406. var result = 0;
  2407. baseEach(collection, function(value, index, collection) {
  2408. result += +iteratee(value, index, collection) || 0;
  2409. });
  2410. return result;
  2411. }
  2412. /**
  2413. * The base implementation of `_.uniq` without support for callback shorthands
  2414. * and `this` binding.
  2415. *
  2416. * @private
  2417. * @param {Array} array The array to inspect.
  2418. * @param {Function} [iteratee] The function invoked per iteration.
  2419. * @returns {Array} Returns the new duplicate-value-free array.
  2420. */
  2421. function baseUniq(array, iteratee) {
  2422. var index = -1,
  2423. indexOf = getIndexOf(),
  2424. length = array.length,
  2425. isCommon = indexOf == baseIndexOf,
  2426. isLarge = isCommon && length >= 200,
  2427. seen = isLarge ? createCache() : null,
  2428. result = [];
  2429. if (seen) {
  2430. indexOf = cacheIndexOf;
  2431. isCommon = false;
  2432. } else {
  2433. isLarge = false;
  2434. seen = iteratee ? [] : result;
  2435. }
  2436. outer:
  2437. while (++index < length) {
  2438. var value = array[index],
  2439. computed = iteratee ? iteratee(value, index, array) : value;
  2440. if (isCommon && value === value) {
  2441. var seenIndex = seen.length;
  2442. while (seenIndex--) {
  2443. if (seen[seenIndex] === computed) {
  2444. continue outer;
  2445. }
  2446. }
  2447. if (iteratee) {
  2448. seen.push(computed);
  2449. }
  2450. result.push(value);
  2451. }
  2452. else if (indexOf(seen, computed, 0) < 0) {
  2453. if (iteratee || isLarge) {
  2454. seen.push(computed);
  2455. }
  2456. result.push(value);
  2457. }
  2458. }
  2459. return result;
  2460. }
  2461. /**
  2462. * The base implementation of `_.values` and `_.valuesIn` which creates an
  2463. * array of `object` property values corresponding to the property names
  2464. * of `props`.
  2465. *
  2466. * @private
  2467. * @param {Object} object The object to query.
  2468. * @param {Array} props The property names to get values for.
  2469. * @returns {Object} Returns the array of property values.
  2470. */
  2471. function baseValues(object, props) {
  2472. var index = -1,
  2473. length = props.length,
  2474. result = Array(length);
  2475. while (++index < length) {
  2476. result[index] = object[props[index]];
  2477. }
  2478. return result;
  2479. }
  2480. /**
  2481. * The base implementation of `_.dropRightWhile`, `_.dropWhile`, `_.takeRightWhile`,
  2482. * and `_.takeWhile` without support for callback shorthands and `this` binding.
  2483. *
  2484. * @private
  2485. * @param {Array} array The array to query.
  2486. * @param {Function} predicate The function invoked per iteration.
  2487. * @param {boolean} [isDrop] Specify dropping elements instead of taking them.
  2488. * @param {boolean} [fromRight] Specify iterating from right to left.
  2489. * @returns {Array} Returns the slice of `array`.
  2490. */
  2491. function baseWhile(array, predicate, isDrop, fromRight) {
  2492. var length = array.length,
  2493. index = fromRight ? length : -1;
  2494. while ((fromRight ? index-- : ++index < length) && predicate(array[index], index, array)) {}
  2495. return isDrop
  2496. ? baseSlice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length))
  2497. : baseSlice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index));
  2498. }
  2499. /**
  2500. * The base implementation of `wrapperValue` which returns the result of
  2501. * performing a sequence of actions on the unwrapped `value`, where each
  2502. * successive action is supplied the return value of the previous.
  2503. *
  2504. * @private
  2505. * @param {*} value The unwrapped value.
  2506. * @param {Array} actions Actions to peform to resolve the unwrapped value.
  2507. * @returns {*} Returns the resolved value.
  2508. */
  2509. function baseWrapperValue(value, actions) {
  2510. var result = value;
  2511. if (result instanceof LazyWrapper) {
  2512. result = result.value();
  2513. }
  2514. var index = -1,
  2515. length = actions.length;
  2516. while (++index < length) {
  2517. var args = [result],
  2518. action = actions[index];
  2519. push.apply(args, action.args);
  2520. result = action.func.apply(action.thisArg, args);
  2521. }
  2522. return result;
  2523. }
  2524. /**
  2525. * Performs a binary search of `array` to determine the index at which `value`
  2526. * should be inserted into `array` in order to maintain its sort order.
  2527. *
  2528. * @private
  2529. * @param {Array} array The sorted array to inspect.
  2530. * @param {*} value The value to evaluate.
  2531. * @param {boolean} [retHighest] Specify returning the highest qualified index.
  2532. * @returns {number} Returns the index at which `value` should be inserted
  2533. * into `array`.
  2534. */
  2535. function binaryIndex(array, value, retHighest) {
  2536. var low = 0,
  2537. high = array ? array.length : low;
  2538. if (typeof value == 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) {
  2539. while (low < high) {
  2540. var mid = (low + high) >>> 1,
  2541. computed = array[mid];
  2542. if ((retHighest ? (computed <= value) : (computed < value)) && computed !== null) {
  2543. low = mid + 1;
  2544. } else {
  2545. high = mid;
  2546. }
  2547. }
  2548. return high;
  2549. }
  2550. return binaryIndexBy(array, value, identity, retHighest);
  2551. }
  2552. /**
  2553. * This function is like `binaryIndex` except that it invokes `iteratee` for
  2554. * `value` and each element of `array` to compute their sort ranking. The
  2555. * iteratee is invoked with one argument; (value).
  2556. *
  2557. * @private
  2558. * @param {Array} array The sorted array to inspect.
  2559. * @param {*} value The value to evaluate.
  2560. * @param {Function} iteratee The function invoked per iteration.
  2561. * @param {boolean} [retHighest] Specify returning the highest qualified index.
  2562. * @returns {number} Returns the index at which `value` should be inserted
  2563. * into `array`.
  2564. */
  2565. function binaryIndexBy(array, value, iteratee, retHighest) {
  2566. value = iteratee(value);
  2567. var low = 0,
  2568. high = array ? array.length : 0,
  2569. valIsNaN = value !== value,
  2570. valIsNull = value === null,
  2571. valIsUndef = value === undefined;
  2572. while (low < high) {
  2573. var mid = floor((low + high) / 2),
  2574. computed = iteratee(array[mid]),
  2575. isDef = computed !== undefined,
  2576. isReflexive = computed === computed;
  2577. if (valIsNaN) {
  2578. var setLow = isReflexive || retHighest;
  2579. } else if (valIsNull) {
  2580. setLow = isReflexive && isDef && (retHighest || computed != null);
  2581. } else if (valIsUndef) {
  2582. setLow = isReflexive && (retHighest || isDef);
  2583. } else if (computed == null) {
  2584. setLow = false;
  2585. } else {
  2586. setLow = retHighest ? (computed <= value) : (computed < value);
  2587. }
  2588. if (setLow) {
  2589. low = mid + 1;
  2590. } else {
  2591. high = mid;
  2592. }
  2593. }
  2594. return nativeMin(high, MAX_ARRAY_INDEX);
  2595. }
  2596. /**
  2597. * A specialized version of `baseCallback` which only supports `this` binding
  2598. * and specifying the number of arguments to provide to `func`.
  2599. *
  2600. * @private
  2601. * @param {Function} func The function to bind.
  2602. * @param {*} thisArg The `this` binding of `func`.
  2603. * @param {number} [argCount] The number of arguments to provide to `func`.
  2604. * @returns {Function} Returns the callback.
  2605. */
  2606. function bindCallback(func, thisArg, argCount) {
  2607. if (typeof func != 'function') {
  2608. return identity;
  2609. }
  2610. if (thisArg === undefined) {
  2611. return func;
  2612. }
  2613. switch (argCount) {
  2614. case 1: return function(value) {
  2615. return func.call(thisArg, value);
  2616. };
  2617. case 3: return function(value, index, collection) {
  2618. return func.call(thisArg, value, index, collection);
  2619. };
  2620. case 4: return function(accumulator, value, index, collection) {
  2621. return func.call(thisArg, accumulator, value, index, collection);
  2622. };
  2623. case 5: return function(value, other, key, object, source) {
  2624. return func.call(thisArg, value, other, key, object, source);
  2625. };
  2626. }
  2627. return function() {
  2628. return func.apply(thisArg, arguments);
  2629. };
  2630. }
  2631. /**
  2632. * Creates a clone of the given array buffer.
  2633. *
  2634. * @private
  2635. * @param {ArrayBuffer} buffer The array buffer to clone.
  2636. * @returns {ArrayBuffer} Returns the cloned array buffer.
  2637. */
  2638. function bufferClone(buffer) {
  2639. return bufferSlice.call(buffer, 0);
  2640. }
  2641. if (!bufferSlice) {
  2642. // PhantomJS has `ArrayBuffer` and `Uint8Array` but not `Float64Array`.
  2643. bufferClone = !(ArrayBuffer && Uint8Array) ? constant(null) : function(buffer) {
  2644. var byteLength = buffer.byteLength,
  2645. floatLength = Float64Array ? floor(byteLength / FLOAT64_BYTES_PER_ELEMENT) : 0,
  2646. offset = floatLength * FLOAT64_BYTES_PER_ELEMENT,
  2647. result = new ArrayBuffer(byteLength);
  2648. if (floatLength) {
  2649. var view = new Float64Array(result, 0, floatLength);
  2650. view.set(new Float64Array(buffer, 0, floatLength));
  2651. }
  2652. if (byteLength != offset) {
  2653. view = new Uint8Array(result, offset);
  2654. view.set(new Uint8Array(buffer, offset));
  2655. }
  2656. return result;
  2657. };
  2658. }
  2659. /**
  2660. * Creates an array that is the composition of partially applied arguments,
  2661. * placeholders, and provided arguments into a single array of arguments.
  2662. *
  2663. * @private
  2664. * @param {Array|Object} args The provided arguments.
  2665. * @param {Array} partials The arguments to prepend to those provided.
  2666. * @param {Array} holders The `partials` placeholder indexes.
  2667. * @returns {Array} Returns the new array of composed arguments.
  2668. */
  2669. function composeArgs(args, partials, holders) {
  2670. var holdersLength = holders.length,
  2671. argsIndex = -1,
  2672. argsLength = nativeMax(args.length - holdersLength, 0),
  2673. leftIndex = -1,
  2674. leftLength = partials.length,
  2675. result = Array(argsLength + leftLength);
  2676. while (++leftIndex < leftLength) {
  2677. result[leftIndex] = partials[leftIndex];
  2678. }
  2679. while (++argsIndex < holdersLength) {
  2680. result[holders[argsIndex]] = args[argsIndex];
  2681. }
  2682. while (argsLength--) {
  2683. result[leftIndex++] = args[argsIndex++];
  2684. }
  2685. return result;
  2686. }
  2687. /**
  2688. * This function is like `composeArgs` except that the arguments composition
  2689. * is tailored for `_.partialRight`.
  2690. *
  2691. * @private
  2692. * @param {Array|Object} args The provided arguments.
  2693. * @param {Array} partials The arguments to append to those provided.
  2694. * @param {Array} holders The `partials` placeholder indexes.
  2695. * @returns {Array} Returns the new array of composed arguments.
  2696. */
  2697. function composeArgsRight(args, partials, holders) {
  2698. var holdersIndex = -1,
  2699. holdersLength = holders.length,
  2700. argsIndex = -1,
  2701. argsLength = nativeMax(args.length - holdersLength, 0),
  2702. rightIndex = -1,
  2703. rightLength = partials.length,
  2704. result = Array(argsLength + rightLength);
  2705. while (++argsIndex < argsLength) {
  2706. result[argsIndex] = args[argsIndex];
  2707. }
  2708. var offset = argsIndex;
  2709. while (++rightIndex < rightLength) {
  2710. result[offset + rightIndex] = partials[rightIndex];
  2711. }
  2712. while (++holdersIndex < holdersLength) {
  2713. result[offset + holders[holdersIndex]] = args[argsIndex++];
  2714. }
  2715. return result;
  2716. }
  2717. /**
  2718. * Creates a function that aggregates a collection, creating an accumulator
  2719. * object composed from the results of running each element in the collection
  2720. * through an iteratee.
  2721. *
  2722. * **Note:** This function is used to create `_.countBy`, `_.groupBy`, `_.indexBy`,
  2723. * and `_.partition`.
  2724. *
  2725. * @private
  2726. * @param {Function} setter The function to set keys and values of the accumulator object.
  2727. * @param {Function} [initializer] The function to initialize the accumulator object.
  2728. * @returns {Function} Returns the new aggregator function.
  2729. */
  2730. function createAggregator(setter, initializer) {
  2731. return function(collection, iteratee, thisArg) {
  2732. var result = initializer ? initializer() : {};
  2733. iteratee = getCallback(iteratee, thisArg, 3);
  2734. if (isArray(collection)) {
  2735. var index = -1,
  2736. length = collection.length;
  2737. while (++index < length) {
  2738. var value = collection[index];
  2739. setter(result, value, iteratee(value, index, collection), collection);
  2740. }
  2741. } else {
  2742. baseEach(collection, function(value, key, collection) {
  2743. setter(result, value, iteratee(value, key, collection), collection);
  2744. });
  2745. }
  2746. return result;
  2747. };
  2748. }
  2749. /**
  2750. * Creates a function that assigns properties of source object(s) to a given
  2751. * destination object.
  2752. *
  2753. * **Note:** This function is used to create `_.assign`, `_.defaults`, and `_.merge`.
  2754. *
  2755. * @private
  2756. * @param {Function} assigner The function to assign values.
  2757. * @returns {Function} Returns the new assigner function.
  2758. */
  2759. function createAssigner(assigner) {
  2760. return restParam(function(object, sources) {
  2761. var index = -1,
  2762. length = object == null ? 0 : sources.length,
  2763. customizer = length > 2 ? sources[length - 2] : undefined,
  2764. guard = length > 2 ? sources[2] : undefined,
  2765. thisArg = length > 1 ? sources[length - 1] : undefined;
  2766. if (typeof customizer == 'function') {
  2767. customizer = bindCallback(customizer, thisArg, 5);
  2768. length -= 2;
  2769. } else {
  2770. customizer = typeof thisArg == 'function' ? thisArg : undefined;
  2771. length -= (customizer ? 1 : 0);
  2772. }
  2773. if (guard && isIterateeCall(sources[0], sources[1], guard)) {
  2774. customizer = length < 3 ? undefined : customizer;
  2775. length = 1;
  2776. }
  2777. while (++index < length) {
  2778. var source = sources[index];
  2779. if (source) {
  2780. assigner(object, source, customizer);
  2781. }
  2782. }
  2783. return object;
  2784. });
  2785. }
  2786. /**
  2787. * Creates a `baseEach` or `baseEachRight` function.
  2788. *
  2789. * @private
  2790. * @param {Function} eachFunc The function to iterate over a collection.
  2791. * @param {boolean} [fromRight] Specify iterating from right to left.
  2792. * @returns {Function} Returns the new base function.
  2793. */
  2794. function createBaseEach(eachFunc, fromRight) {
  2795. return function(collection, iteratee) {
  2796. var length = collection ? getLength(collection) : 0;
  2797. if (!isLength(length)) {
  2798. return eachFunc(collection, iteratee);
  2799. }
  2800. var index = fromRight ? length : -1,
  2801. iterable = toObject(collection);
  2802. while ((fromRight ? index-- : ++index < length)) {
  2803. if (iteratee(iterable[index], index, iterable) === false) {
  2804. break;
  2805. }
  2806. }
  2807. return collection;
  2808. };
  2809. }
  2810. /**
  2811. * Creates a base function for `_.forIn` or `_.forInRight`.
  2812. *
  2813. * @private
  2814. * @param {boolean} [fromRight] Specify iterating from right to left.
  2815. * @returns {Function} Returns the new base function.
  2816. */
  2817. function createBaseFor(fromRight) {
  2818. return function(object, iteratee, keysFunc) {
  2819. var iterable = toObject(object),
  2820. props = keysFunc(object),
  2821. length = props.length,
  2822. index = fromRight ? length : -1;
  2823. while ((fromRight ? index-- : ++index < length)) {
  2824. var key = props[index];
  2825. if (iteratee(iterable[key], key, iterable) === false) {
  2826. break;
  2827. }
  2828. }
  2829. return object;
  2830. };
  2831. }
  2832. /**
  2833. * Creates a function that wraps `func` and invokes it with the `this`
  2834. * binding of `thisArg`.
  2835. *
  2836. * @private
  2837. * @param {Function} func The function to bind.
  2838. * @param {*} [thisArg] The `this` binding of `func`.
  2839. * @returns {Function} Returns the new bound function.
  2840. */
  2841. function createBindWrapper(func, thisArg) {
  2842. var Ctor = createCtorWrapper(func);
  2843. function wrapper() {
  2844. var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  2845. return fn.apply(thisArg, arguments);
  2846. }
  2847. return wrapper;
  2848. }
  2849. /**
  2850. * Creates a `Set` cache object to optimize linear searches of large arrays.
  2851. *
  2852. * @private
  2853. * @param {Array} [values] The values to cache.
  2854. * @returns {null|Object} Returns the new cache object if `Set` is supported, else `null`.
  2855. */
  2856. var createCache = !(nativeCreate && Set) ? constant(null) : function(values) {
  2857. return new SetCache(values);
  2858. };
  2859. /**
  2860. * Creates a function that produces compound words out of the words in a
  2861. * given string.
  2862. *
  2863. * @private
  2864. * @param {Function} callback The function to combine each word.
  2865. * @returns {Function} Returns the new compounder function.
  2866. */
  2867. function createCompounder(callback) {
  2868. return function(string) {
  2869. var index = -1,
  2870. array = words(deburr(string)),
  2871. length = array.length,
  2872. result = '';
  2873. while (++index < length) {
  2874. result = callback(result, array[index], index);
  2875. }
  2876. return result;
  2877. };
  2878. }
  2879. /**
  2880. * Creates a function that produces an instance of `Ctor` regardless of
  2881. * whether it was invoked as part of a `new` expression or by `call` or `apply`.
  2882. *
  2883. * @private
  2884. * @param {Function} Ctor The constructor to wrap.
  2885. * @returns {Function} Returns the new wrapped function.
  2886. */
  2887. function createCtorWrapper(Ctor) {
  2888. return function() {
  2889. // Use a `switch` statement to work with class constructors.
  2890. // See https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ecmascript-function-objects-call-thisargument-argumentslist
  2891. // for more details.
  2892. var args = arguments;
  2893. switch (args.length) {
  2894. case 0: return new Ctor;
  2895. case 1: return new Ctor(args[0]);
  2896. case 2: return new Ctor(args[0], args[1]);
  2897. case 3: return new Ctor(args[0], args[1], args[2]);
  2898. case 4: return new Ctor(args[0], args[1], args[2], args[3]);
  2899. case 5: return new Ctor(args[0], args[1], args[2], args[3], args[4]);
  2900. }
  2901. var thisBinding = baseCreate(Ctor.prototype),
  2902. result = Ctor.apply(thisBinding, args);
  2903. // Mimic the constructor's `return` behavior.
  2904. // See https://es5.github.io/#x13.2.2 for more details.
  2905. return isObject(result) ? result : thisBinding;
  2906. };
  2907. }
  2908. /**
  2909. * Creates a `_.curry` or `_.curryRight` function.
  2910. *
  2911. * @private
  2912. * @param {boolean} flag The curry bit flag.
  2913. * @returns {Function} Returns the new curry function.
  2914. */
  2915. function createCurry(flag) {
  2916. function curryFunc(func, arity, guard) {
  2917. if (guard && isIterateeCall(func, arity, guard)) {
  2918. arity = null;
  2919. }
  2920. var result = createWrapper(func, flag, null, null, null, null, null, arity);
  2921. result.placeholder = curryFunc.placeholder;
  2922. return result;
  2923. }
  2924. return curryFunc;
  2925. }
  2926. /**
  2927. * Creates a `_.max` or `_.min` function.
  2928. *
  2929. * @private
  2930. * @param {Function} comparator The function used to compare values.
  2931. * @param {*} exValue The initial extremum value.
  2932. * @returns {Function} Returns the new extremum function.
  2933. */
  2934. function createExtremum(comparator, exValue) {
  2935. return function(collection, iteratee, thisArg) {
  2936. if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
  2937. iteratee = null;
  2938. }
  2939. iteratee = getCallback(iteratee, thisArg, 3);
  2940. if (iteratee.length == 1) {
  2941. collection = toIterable(collection);
  2942. var result = arrayExtremum(collection, iteratee, comparator, exValue);
  2943. if (!(collection.length && result === exValue)) {
  2944. return result;
  2945. }
  2946. }
  2947. return baseExtremum(collection, iteratee, comparator, exValue);
  2948. };
  2949. }
  2950. /**
  2951. * Creates a `_.find` or `_.findLast` function.
  2952. *
  2953. * @private
  2954. * @param {Function} eachFunc The function to iterate over a collection.
  2955. * @param {boolean} [fromRight] Specify iterating from right to left.
  2956. * @returns {Function} Returns the new find function.
  2957. */
  2958. function createFind(eachFunc, fromRight) {
  2959. return function(collection, predicate, thisArg) {
  2960. predicate = getCallback(predicate, thisArg, 3);
  2961. if (isArray(collection)) {
  2962. var index = baseFindIndex(collection, predicate, fromRight);
  2963. return index > -1 ? collection[index] : undefined;
  2964. }
  2965. return baseFind(collection, predicate, eachFunc);
  2966. };
  2967. }
  2968. /**
  2969. * Creates a `_.findIndex` or `_.findLastIndex` function.
  2970. *
  2971. * @private
  2972. * @param {boolean} [fromRight] Specify iterating from right to left.
  2973. * @returns {Function} Returns the new find function.
  2974. */
  2975. function createFindIndex(fromRight) {
  2976. return function(array, predicate, thisArg) {
  2977. if (!(array && array.length)) {
  2978. return -1;
  2979. }
  2980. predicate = getCallback(predicate, thisArg, 3);
  2981. return baseFindIndex(array, predicate, fromRight);
  2982. };
  2983. }
  2984. /**
  2985. * Creates a `_.findKey` or `_.findLastKey` function.
  2986. *
  2987. * @private
  2988. * @param {Function} objectFunc The function to iterate over an object.
  2989. * @returns {Function} Returns the new find function.
  2990. */
  2991. function createFindKey(objectFunc) {
  2992. return function(object, predicate, thisArg) {
  2993. predicate = getCallback(predicate, thisArg, 3);
  2994. return baseFind(object, predicate, objectFunc, true);
  2995. };
  2996. }
  2997. /**
  2998. * Creates a `_.flow` or `_.flowRight` function.
  2999. *
  3000. * @private
  3001. * @param {boolean} [fromRight] Specify iterating from right to left.
  3002. * @returns {Function} Returns the new flow function.
  3003. */
  3004. function createFlow(fromRight) {
  3005. return function() {
  3006. var wrapper,
  3007. length = arguments.length,
  3008. index = fromRight ? length : -1,
  3009. leftIndex = 0,
  3010. funcs = Array(length);
  3011. while ((fromRight ? index-- : ++index < length)) {
  3012. var func = funcs[leftIndex++] = arguments[index];
  3013. if (typeof func != 'function') {
  3014. throw new TypeError(FUNC_ERROR_TEXT);
  3015. }
  3016. if (!wrapper && LodashWrapper.prototype.thru && getFuncName(func) == 'wrapper') {
  3017. wrapper = new LodashWrapper([]);
  3018. }
  3019. }
  3020. index = wrapper ? -1 : length;
  3021. while (++index < length) {
  3022. func = funcs[index];
  3023. var funcName = getFuncName(func),
  3024. data = funcName == 'wrapper' ? getData(func) : null;
  3025. if (data && isLaziable(data[0]) && data[1] == (ARY_FLAG | CURRY_FLAG | PARTIAL_FLAG | REARG_FLAG) && !data[4].length && data[9] == 1) {
  3026. wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]);
  3027. } else {
  3028. wrapper = (func.length == 1 && isLaziable(func)) ? wrapper[funcName]() : wrapper.thru(func);
  3029. }
  3030. }
  3031. return function() {
  3032. var args = arguments;
  3033. if (wrapper && args.length == 1 && isArray(args[0])) {
  3034. return wrapper.plant(args[0]).value();
  3035. }
  3036. var index = 0,
  3037. result = length ? funcs[index].apply(this, args) : args[0];
  3038. while (++index < length) {
  3039. result = funcs[index].call(this, result);
  3040. }
  3041. return result;
  3042. };
  3043. };
  3044. }
  3045. /**
  3046. * Creates a function for `_.forEach` or `_.forEachRight`.
  3047. *
  3048. * @private
  3049. * @param {Function} arrayFunc The function to iterate over an array.
  3050. * @param {Function} eachFunc The function to iterate over a collection.
  3051. * @returns {Function} Returns the new each function.
  3052. */
  3053. function createForEach(arrayFunc, eachFunc) {
  3054. return function(collection, iteratee, thisArg) {
  3055. return (typeof iteratee == 'function' && thisArg === undefined && isArray(collection))
  3056. ? arrayFunc(collection, iteratee)
  3057. : eachFunc(collection, bindCallback(iteratee, thisArg, 3));
  3058. };
  3059. }
  3060. /**
  3061. * Creates a function for `_.forIn` or `_.forInRight`.
  3062. *
  3063. * @private
  3064. * @param {Function} objectFunc The function to iterate over an object.
  3065. * @returns {Function} Returns the new each function.
  3066. */
  3067. function createForIn(objectFunc) {
  3068. return function(object, iteratee, thisArg) {
  3069. if (typeof iteratee != 'function' || thisArg !== undefined) {
  3070. iteratee = bindCallback(iteratee, thisArg, 3);
  3071. }
  3072. return objectFunc(object, iteratee, keysIn);
  3073. };
  3074. }
  3075. /**
  3076. * Creates a function for `_.forOwn` or `_.forOwnRight`.
  3077. *
  3078. * @private
  3079. * @param {Function} objectFunc The function to iterate over an object.
  3080. * @returns {Function} Returns the new each function.
  3081. */
  3082. function createForOwn(objectFunc) {
  3083. return function(object, iteratee, thisArg) {
  3084. if (typeof iteratee != 'function' || thisArg !== undefined) {
  3085. iteratee = bindCallback(iteratee, thisArg, 3);
  3086. }
  3087. return objectFunc(object, iteratee);
  3088. };
  3089. }
  3090. /**
  3091. * Creates a function for `_.mapKeys` or `_.mapValues`.
  3092. *
  3093. * @private
  3094. * @param {boolean} [isMapKeys] Specify mapping keys instead of values.
  3095. * @returns {Function} Returns the new map function.
  3096. */
  3097. function createObjectMapper(isMapKeys) {
  3098. return function(object, iteratee, thisArg) {
  3099. var result = {};
  3100. iteratee = getCallback(iteratee, thisArg, 3);
  3101. baseForOwn(object, function(value, key, object) {
  3102. var mapped = iteratee(value, key, object);
  3103. key = isMapKeys ? mapped : key;
  3104. value = isMapKeys ? value : mapped;
  3105. result[key] = value;
  3106. });
  3107. return result;
  3108. };
  3109. }
  3110. /**
  3111. * Creates a function for `_.padLeft` or `_.padRight`.
  3112. *
  3113. * @private
  3114. * @param {boolean} [fromRight] Specify padding from the right.
  3115. * @returns {Function} Returns the new pad function.
  3116. */
  3117. function createPadDir(fromRight) {
  3118. return function(string, length, chars) {
  3119. string = baseToString(string);
  3120. return (fromRight ? string : '') + createPadding(string, length, chars) + (fromRight ? '' : string);
  3121. };
  3122. }
  3123. /**
  3124. * Creates a `_.partial` or `_.partialRight` function.
  3125. *
  3126. * @private
  3127. * @param {boolean} flag The partial bit flag.
  3128. * @returns {Function} Returns the new partial function.
  3129. */
  3130. function createPartial(flag) {
  3131. var partialFunc = restParam(function(func, partials) {
  3132. var holders = replaceHolders(partials, partialFunc.placeholder);
  3133. return createWrapper(func, flag, null, partials, holders);
  3134. });
  3135. return partialFunc;
  3136. }
  3137. /**
  3138. * Creates a function for `_.reduce` or `_.reduceRight`.
  3139. *
  3140. * @private
  3141. * @param {Function} arrayFunc The function to iterate over an array.
  3142. * @param {Function} eachFunc The function to iterate over a collection.
  3143. * @returns {Function} Returns the new each function.
  3144. */
  3145. function createReduce(arrayFunc, eachFunc) {
  3146. return function(collection, iteratee, accumulator, thisArg) {
  3147. var initFromArray = arguments.length < 3;
  3148. return (typeof iteratee == 'function' && thisArg === undefined && isArray(collection))
  3149. ? arrayFunc(collection, iteratee, accumulator, initFromArray)
  3150. : baseReduce(collection, getCallback(iteratee, thisArg, 4), accumulator, initFromArray, eachFunc);
  3151. };
  3152. }
  3153. /**
  3154. * Creates a function that wraps `func` and invokes it with optional `this`
  3155. * binding of, partial application, and currying.
  3156. *
  3157. * @private
  3158. * @param {Function|string} func The function or method name to reference.
  3159. * @param {number} bitmask The bitmask of flags. See `createWrapper` for more details.
  3160. * @param {*} [thisArg] The `this` binding of `func`.
  3161. * @param {Array} [partials] The arguments to prepend to those provided to the new function.
  3162. * @param {Array} [holders] The `partials` placeholder indexes.
  3163. * @param {Array} [partialsRight] The arguments to append to those provided to the new function.
  3164. * @param {Array} [holdersRight] The `partialsRight` placeholder indexes.
  3165. * @param {Array} [argPos] The argument positions of the new function.
  3166. * @param {number} [ary] The arity cap of `func`.
  3167. * @param {number} [arity] The arity of `func`.
  3168. * @returns {Function} Returns the new wrapped function.
  3169. */
  3170. function createHybridWrapper(func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity) {
  3171. var isAry = bitmask & ARY_FLAG,
  3172. isBind = bitmask & BIND_FLAG,
  3173. isBindKey = bitmask & BIND_KEY_FLAG,
  3174. isCurry = bitmask & CURRY_FLAG,
  3175. isCurryBound = bitmask & CURRY_BOUND_FLAG,
  3176. isCurryRight = bitmask & CURRY_RIGHT_FLAG,
  3177. Ctor = isBindKey ? null : createCtorWrapper(func);
  3178. function wrapper() {
  3179. // Avoid `arguments` object use disqualifying optimizations by
  3180. // converting it to an array before providing it to other functions.
  3181. var length = arguments.length,
  3182. index = length,
  3183. args = Array(length);
  3184. while (index--) {
  3185. args[index] = arguments[index];
  3186. }
  3187. if (partials) {
  3188. args = composeArgs(args, partials, holders);
  3189. }
  3190. if (partialsRight) {
  3191. args = composeArgsRight(args, partialsRight, holdersRight);
  3192. }
  3193. if (isCurry || isCurryRight) {
  3194. var placeholder = wrapper.placeholder,
  3195. argsHolders = replaceHolders(args, placeholder);
  3196. length -= argsHolders.length;
  3197. if (length < arity) {
  3198. var newArgPos = argPos ? arrayCopy(argPos) : null,
  3199. newArity = nativeMax(arity - length, 0),
  3200. newsHolders = isCurry ? argsHolders : null,
  3201. newHoldersRight = isCurry ? null : argsHolders,
  3202. newPartials = isCurry ? args : null,
  3203. newPartialsRight = isCurry ? null : args;
  3204. bitmask |= (isCurry ? PARTIAL_FLAG : PARTIAL_RIGHT_FLAG);
  3205. bitmask &= ~(isCurry ? PARTIAL_RIGHT_FLAG : PARTIAL_FLAG);
  3206. if (!isCurryBound) {
  3207. bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG);
  3208. }
  3209. var newData = [func, bitmask, thisArg, newPartials, newsHolders, newPartialsRight, newHoldersRight, newArgPos, ary, newArity],
  3210. result = createHybridWrapper.apply(undefined, newData);
  3211. if (isLaziable(func)) {
  3212. setData(result, newData);
  3213. }
  3214. result.placeholder = placeholder;
  3215. return result;
  3216. }
  3217. }
  3218. var thisBinding = isBind ? thisArg : this,
  3219. fn = isBindKey ? thisBinding[func] : func;
  3220. if (argPos) {
  3221. args = reorder(args, argPos);
  3222. }
  3223. if (isAry && ary < args.length) {
  3224. args.length = ary;
  3225. }
  3226. if (this && this !== root && this instanceof wrapper) {
  3227. fn = Ctor || createCtorWrapper(func);
  3228. }
  3229. return fn.apply(thisBinding, args);
  3230. }
  3231. return wrapper;
  3232. }
  3233. /**
  3234. * Creates the padding required for `string` based on the given `length`.
  3235. * The `chars` string is truncated if the number of characters exceeds `length`.
  3236. *
  3237. * @private
  3238. * @param {string} string The string to create padding for.
  3239. * @param {number} [length=0] The padding length.
  3240. * @param {string} [chars=' '] The string used as padding.
  3241. * @returns {string} Returns the pad for `string`.
  3242. */
  3243. function createPadding(string, length, chars) {
  3244. var strLength = string.length;
  3245. length = +length;
  3246. if (strLength >= length || !nativeIsFinite(length)) {
  3247. return '';
  3248. }
  3249. var padLength = length - strLength;
  3250. chars = chars == null ? ' ' : (chars + '');
  3251. return repeat(chars, ceil(padLength / chars.length)).slice(0, padLength);
  3252. }
  3253. /**
  3254. * Creates a function that wraps `func` and invokes it with the optional `this`
  3255. * binding of `thisArg` and the `partials` prepended to those provided to
  3256. * the wrapper.
  3257. *
  3258. * @private
  3259. * @param {Function} func The function to partially apply arguments to.
  3260. * @param {number} bitmask The bitmask of flags. See `createWrapper` for more details.
  3261. * @param {*} thisArg The `this` binding of `func`.
  3262. * @param {Array} partials The arguments to prepend to those provided to the new function.
  3263. * @returns {Function} Returns the new bound function.
  3264. */
  3265. function createPartialWrapper(func, bitmask, thisArg, partials) {
  3266. var isBind = bitmask & BIND_FLAG,
  3267. Ctor = createCtorWrapper(func);
  3268. function wrapper() {
  3269. // Avoid `arguments` object use disqualifying optimizations by
  3270. // converting it to an array before providing it `func`.
  3271. var argsIndex = -1,
  3272. argsLength = arguments.length,
  3273. leftIndex = -1,
  3274. leftLength = partials.length,
  3275. args = Array(argsLength + leftLength);
  3276. while (++leftIndex < leftLength) {
  3277. args[leftIndex] = partials[leftIndex];
  3278. }
  3279. while (argsLength--) {
  3280. args[leftIndex++] = arguments[++argsIndex];
  3281. }
  3282. var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  3283. return fn.apply(isBind ? thisArg : this, args);
  3284. }
  3285. return wrapper;
  3286. }
  3287. /**
  3288. * Creates a `_.sortedIndex` or `_.sortedLastIndex` function.
  3289. *
  3290. * @private
  3291. * @param {boolean} [retHighest] Specify returning the highest qualified index.
  3292. * @returns {Function} Returns the new index function.
  3293. */
  3294. function createSortedIndex(retHighest) {
  3295. return function(array, value, iteratee, thisArg) {
  3296. var callback = getCallback(iteratee);
  3297. return (iteratee == null && callback === baseCallback)
  3298. ? binaryIndex(array, value, retHighest)
  3299. : binaryIndexBy(array, value, callback(iteratee, thisArg, 1), retHighest);
  3300. };
  3301. }
  3302. /**
  3303. * Creates a function that either curries or invokes `func` with optional
  3304. * `this` binding and partially applied arguments.
  3305. *
  3306. * @private
  3307. * @param {Function|string} func The function or method name to reference.
  3308. * @param {number} bitmask The bitmask of flags.
  3309. * The bitmask may be composed of the following flags:
  3310. * 1 - `_.bind`
  3311. * 2 - `_.bindKey`
  3312. * 4 - `_.curry` or `_.curryRight` of a bound function
  3313. * 8 - `_.curry`
  3314. * 16 - `_.curryRight`
  3315. * 32 - `_.partial`
  3316. * 64 - `_.partialRight`
  3317. * 128 - `_.rearg`
  3318. * 256 - `_.ary`
  3319. * @param {*} [thisArg] The `this` binding of `func`.
  3320. * @param {Array} [partials] The arguments to be partially applied.
  3321. * @param {Array} [holders] The `partials` placeholder indexes.
  3322. * @param {Array} [argPos] The argument positions of the new function.
  3323. * @param {number} [ary] The arity cap of `func`.
  3324. * @param {number} [arity] The arity of `func`.
  3325. * @returns {Function} Returns the new wrapped function.
  3326. */
  3327. function createWrapper(func, bitmask, thisArg, partials, holders, argPos, ary, arity) {
  3328. var isBindKey = bitmask & BIND_KEY_FLAG;
  3329. if (!isBindKey && typeof func != 'function') {
  3330. throw new TypeError(FUNC_ERROR_TEXT);
  3331. }
  3332. var length = partials ? partials.length : 0;
  3333. if (!length) {
  3334. bitmask &= ~(PARTIAL_FLAG | PARTIAL_RIGHT_FLAG);
  3335. partials = holders = null;
  3336. }
  3337. length -= (holders ? holders.length : 0);
  3338. if (bitmask & PARTIAL_RIGHT_FLAG) {
  3339. var partialsRight = partials,
  3340. holdersRight = holders;
  3341. partials = holders = null;
  3342. }
  3343. var data = isBindKey ? null : getData(func),
  3344. newData = [func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity];
  3345. if (data) {
  3346. mergeData(newData, data);
  3347. bitmask = newData[1];
  3348. arity = newData[9];
  3349. }
  3350. newData[9] = arity == null
  3351. ? (isBindKey ? 0 : func.length)
  3352. : (nativeMax(arity - length, 0) || 0);
  3353. if (bitmask == BIND_FLAG) {
  3354. var result = createBindWrapper(newData[0], newData[2]);
  3355. } else if ((bitmask == PARTIAL_FLAG || bitmask == (BIND_FLAG | PARTIAL_FLAG)) && !newData[4].length) {
  3356. result = createPartialWrapper.apply(undefined, newData);
  3357. } else {
  3358. result = createHybridWrapper.apply(undefined, newData);
  3359. }
  3360. var setter = data ? baseSetData : setData;
  3361. return setter(result, newData);
  3362. }
  3363. /**
  3364. * A specialized version of `baseIsEqualDeep` for arrays with support for
  3365. * partial deep comparisons.
  3366. *
  3367. * @private
  3368. * @param {Array} array The array to compare.
  3369. * @param {Array} other The other array to compare.
  3370. * @param {Function} equalFunc The function to determine equivalents of values.
  3371. * @param {Function} [customizer] The function to customize comparing arrays.
  3372. * @param {boolean} [isLoose] Specify performing partial comparisons.
  3373. * @param {Array} [stackA] Tracks traversed `value` objects.
  3374. * @param {Array} [stackB] Tracks traversed `other` objects.
  3375. * @returns {boolean} Returns `true` if the arrays are equivalent, else `false`.
  3376. */
  3377. function equalArrays(array, other, equalFunc, customizer, isLoose, stackA, stackB) {
  3378. var index = -1,
  3379. arrLength = array.length,
  3380. othLength = other.length;
  3381. if (arrLength != othLength && !(isLoose && othLength > arrLength)) {
  3382. return false;
  3383. }
  3384. // Ignore non-index properties.
  3385. while (++index < arrLength) {
  3386. var arrValue = array[index],
  3387. othValue = other[index],
  3388. result = customizer ? customizer(isLoose ? othValue : arrValue, isLoose ? arrValue : othValue, index) : undefined;
  3389. if (result !== undefined) {
  3390. if (result) {
  3391. continue;
  3392. }
  3393. return false;
  3394. }
  3395. // Recursively compare arrays (susceptible to call stack limits).
  3396. if (isLoose) {
  3397. if (!arraySome(other, function(othValue) {
  3398. return arrValue === othValue || equalFunc(arrValue, othValue, customizer, isLoose, stackA, stackB);
  3399. })) {
  3400. return false;
  3401. }
  3402. } else if (!(arrValue === othValue || equalFunc(arrValue, othValue, customizer, isLoose, stackA, stackB))) {
  3403. return false;
  3404. }
  3405. }
  3406. return true;
  3407. }
  3408. /**
  3409. * A specialized version of `baseIsEqualDeep` for comparing objects of
  3410. * the same `toStringTag`.
  3411. *
  3412. * **Note:** This function only supports comparing values with tags of
  3413. * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
  3414. *
  3415. * @private
  3416. * @param {Object} value The object to compare.
  3417. * @param {Object} other The other object to compare.
  3418. * @param {string} tag The `toStringTag` of the objects to compare.
  3419. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  3420. */
  3421. function equalByTag(object, other, tag) {
  3422. switch (tag) {
  3423. case boolTag:
  3424. case dateTag:
  3425. // Coerce dates and booleans to numbers, dates to milliseconds and booleans
  3426. // to `1` or `0` treating invalid dates coerced to `NaN` as not equal.
  3427. return +object == +other;
  3428. case errorTag:
  3429. return object.name == other.name && object.message == other.message;
  3430. case numberTag:
  3431. // Treat `NaN` vs. `NaN` as equal.
  3432. return (object != +object)
  3433. ? other != +other
  3434. : object == +other;
  3435. case regexpTag:
  3436. case stringTag:
  3437. // Coerce regexes to strings and treat strings primitives and string
  3438. // objects as equal. See https://es5.github.io/#x15.10.6.4 for more details.
  3439. return object == (other + '');
  3440. }
  3441. return false;
  3442. }
  3443. /**
  3444. * A specialized version of `baseIsEqualDeep` for objects with support for
  3445. * partial deep comparisons.
  3446. *
  3447. * @private
  3448. * @param {Object} object The object to compare.
  3449. * @param {Object} other The other object to compare.
  3450. * @param {Function} equalFunc The function to determine equivalents of values.
  3451. * @param {Function} [customizer] The function to customize comparing values.
  3452. * @param {boolean} [isLoose] Specify performing partial comparisons.
  3453. * @param {Array} [stackA] Tracks traversed `value` objects.
  3454. * @param {Array} [stackB] Tracks traversed `other` objects.
  3455. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  3456. */
  3457. function equalObjects(object, other, equalFunc, customizer, isLoose, stackA, stackB) {
  3458. var objProps = keys(object),
  3459. objLength = objProps.length,
  3460. othProps = keys(other),
  3461. othLength = othProps.length;
  3462. if (objLength != othLength && !isLoose) {
  3463. return false;
  3464. }
  3465. var index = objLength;
  3466. while (index--) {
  3467. var key = objProps[index];
  3468. if (!(isLoose ? key in other : hasOwnProperty.call(other, key))) {
  3469. return false;
  3470. }
  3471. }
  3472. var skipCtor = isLoose;
  3473. while (++index < objLength) {
  3474. key = objProps[index];
  3475. var objValue = object[key],
  3476. othValue = other[key],
  3477. result = customizer ? customizer(isLoose ? othValue : objValue, isLoose? objValue : othValue, key) : undefined;
  3478. // Recursively compare objects (susceptible to call stack limits).
  3479. if (!(result === undefined ? equalFunc(objValue, othValue, customizer, isLoose, stackA, stackB) : result)) {
  3480. return false;
  3481. }
  3482. skipCtor || (skipCtor = key == 'constructor');
  3483. }
  3484. if (!skipCtor) {
  3485. var objCtor = object.constructor,
  3486. othCtor = other.constructor;
  3487. // Non `Object` object instances with different constructors are not equal.
  3488. if (objCtor != othCtor &&
  3489. ('constructor' in object && 'constructor' in other) &&
  3490. !(typeof objCtor == 'function' && objCtor instanceof objCtor &&
  3491. typeof othCtor == 'function' && othCtor instanceof othCtor)) {
  3492. return false;
  3493. }
  3494. }
  3495. return true;
  3496. }
  3497. /**
  3498. * Gets the appropriate "callback" function. If the `_.callback` method is
  3499. * customized this function returns the custom method, otherwise it returns
  3500. * the `baseCallback` function. If arguments are provided the chosen function
  3501. * is invoked with them and its result is returned.
  3502. *
  3503. * @private
  3504. * @returns {Function} Returns the chosen function or its result.
  3505. */
  3506. function getCallback(func, thisArg, argCount) {
  3507. var result = lodash.callback || callback;
  3508. result = result === callback ? baseCallback : result;
  3509. return argCount ? result(func, thisArg, argCount) : result;
  3510. }
  3511. /**
  3512. * Gets metadata for `func`.
  3513. *
  3514. * @private
  3515. * @param {Function} func The function to query.
  3516. * @returns {*} Returns the metadata for `func`.
  3517. */
  3518. var getData = !metaMap ? noop : function(func) {
  3519. return metaMap.get(func);
  3520. };
  3521. /**
  3522. * Gets the name of `func`.
  3523. *
  3524. * @private
  3525. * @param {Function} func The function to query.
  3526. * @returns {string} Returns the function name.
  3527. */
  3528. function getFuncName(func) {
  3529. var result = func.name,
  3530. array = realNames[result],
  3531. length = array ? array.length : 0;
  3532. while (length--) {
  3533. var data = array[length],
  3534. otherFunc = data.func;
  3535. if (otherFunc == null || otherFunc == func) {
  3536. return data.name;
  3537. }
  3538. }
  3539. return result;
  3540. }
  3541. /**
  3542. * Gets the appropriate "indexOf" function. If the `_.indexOf` method is
  3543. * customized this function returns the custom method, otherwise it returns
  3544. * the `baseIndexOf` function. If arguments are provided the chosen function
  3545. * is invoked with them and its result is returned.
  3546. *
  3547. * @private
  3548. * @returns {Function|number} Returns the chosen function or its result.
  3549. */
  3550. function getIndexOf(collection, target, fromIndex) {
  3551. var result = lodash.indexOf || indexOf;
  3552. result = result === indexOf ? baseIndexOf : result;
  3553. return collection ? result(collection, target, fromIndex) : result;
  3554. }
  3555. /**
  3556. * Gets the "length" property value of `object`.
  3557. *
  3558. * **Note:** This function is used to avoid a [JIT bug](https://bugs.webkit.org/show_bug.cgi?id=142792)
  3559. * that affects Safari on at least iOS 8.1-8.3 ARM64.
  3560. *
  3561. * @private
  3562. * @param {Object} object The object to query.
  3563. * @returns {*} Returns the "length" value.
  3564. */
  3565. var getLength = baseProperty('length');
  3566. /**
  3567. * Gets the propery names, values, and compare flags of `object`.
  3568. *
  3569. * @private
  3570. * @param {Object} object The object to query.
  3571. * @returns {Array} Returns the match data of `object`.
  3572. */
  3573. function getMatchData(object) {
  3574. var result = pairs(object),
  3575. length = result.length;
  3576. while (length--) {
  3577. result[length][2] = isStrictComparable(result[length][1]);
  3578. }
  3579. return result;
  3580. }
  3581. /**
  3582. * Gets the native function at `key` of `object`.
  3583. *
  3584. * @private
  3585. * @param {Object} object The object to query.
  3586. * @param {string} key The key of the method to get.
  3587. * @returns {*} Returns the function if it's native, else `undefined`.
  3588. */
  3589. function getNative(object, key) {
  3590. var value = object == null ? undefined : object[key];
  3591. return isNative(value) ? value : undefined;
  3592. }
  3593. /**
  3594. * Gets the view, applying any `transforms` to the `start` and `end` positions.
  3595. *
  3596. * @private
  3597. * @param {number} start The start of the view.
  3598. * @param {number} end The end of the view.
  3599. * @param {Array} [transforms] The transformations to apply to the view.
  3600. * @returns {Object} Returns an object containing the `start` and `end`
  3601. * positions of the view.
  3602. */
  3603. function getView(start, end, transforms) {
  3604. var index = -1,
  3605. length = transforms ? transforms.length : 0;
  3606. while (++index < length) {
  3607. var data = transforms[index],
  3608. size = data.size;
  3609. switch (data.type) {
  3610. case 'drop': start += size; break;
  3611. case 'dropRight': end -= size; break;
  3612. case 'take': end = nativeMin(end, start + size); break;
  3613. case 'takeRight': start = nativeMax(start, end - size); break;
  3614. }
  3615. }
  3616. return { 'start': start, 'end': end };
  3617. }
  3618. /**
  3619. * Initializes an array clone.
  3620. *
  3621. * @private
  3622. * @param {Array} array The array to clone.
  3623. * @returns {Array} Returns the initialized clone.
  3624. */
  3625. function initCloneArray(array) {
  3626. var length = array.length,
  3627. result = new array.constructor(length);
  3628. // Add array properties assigned by `RegExp#exec`.
  3629. if (length && typeof array[0] == 'string' && hasOwnProperty.call(array, 'index')) {
  3630. result.index = array.index;
  3631. result.input = array.input;
  3632. }
  3633. return result;
  3634. }
  3635. /**
  3636. * Initializes an object clone.
  3637. *
  3638. * @private
  3639. * @param {Object} object The object to clone.
  3640. * @returns {Object} Returns the initialized clone.
  3641. */
  3642. function initCloneObject(object) {
  3643. var Ctor = object.constructor;
  3644. if (!(typeof Ctor == 'function' && Ctor instanceof Ctor)) {
  3645. Ctor = Object;
  3646. }
  3647. return new Ctor;
  3648. }
  3649. /**
  3650. * Initializes an object clone based on its `toStringTag`.
  3651. *
  3652. * **Note:** This function only supports cloning values with tags of
  3653. * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
  3654. *
  3655. * @private
  3656. * @param {Object} object The object to clone.
  3657. * @param {string} tag The `toStringTag` of the object to clone.
  3658. * @param {boolean} [isDeep] Specify a deep clone.
  3659. * @returns {Object} Returns the initialized clone.
  3660. */
  3661. function initCloneByTag(object, tag, isDeep) {
  3662. var Ctor = object.constructor;
  3663. switch (tag) {
  3664. case arrayBufferTag:
  3665. return bufferClone(object);
  3666. case boolTag:
  3667. case dateTag:
  3668. return new Ctor(+object);
  3669. case float32Tag: case float64Tag:
  3670. case int8Tag: case int16Tag: case int32Tag:
  3671. case uint8Tag: case uint8ClampedTag: case uint16Tag: case uint32Tag:
  3672. var buffer = object.buffer;
  3673. return new Ctor(isDeep ? bufferClone(buffer) : buffer, object.byteOffset, object.length);
  3674. case numberTag:
  3675. case stringTag:
  3676. return new Ctor(object);
  3677. case regexpTag:
  3678. var result = new Ctor(object.source, reFlags.exec(object));
  3679. result.lastIndex = object.lastIndex;
  3680. }
  3681. return result;
  3682. }
  3683. /**
  3684. * Invokes the method at `path` on `object`.
  3685. *
  3686. * @private
  3687. * @param {Object} object The object to query.
  3688. * @param {Array|string} path The path of the method to invoke.
  3689. * @param {Array} args The arguments to invoke the method with.
  3690. * @returns {*} Returns the result of the invoked method.
  3691. */
  3692. function invokePath(object, path, args) {
  3693. if (object != null && !isKey(path, object)) {
  3694. path = toPath(path);
  3695. object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  3696. path = last(path);
  3697. }
  3698. var func = object == null ? object : object[path];
  3699. return func == null ? undefined : func.apply(object, args);
  3700. }
  3701. /**
  3702. * Checks if `value` is array-like.
  3703. *
  3704. * @private
  3705. * @param {*} value The value to check.
  3706. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  3707. */
  3708. function isArrayLike(value) {
  3709. return value != null && isLength(getLength(value));
  3710. }
  3711. /**
  3712. * Checks if `value` is a valid array-like index.
  3713. *
  3714. * @private
  3715. * @param {*} value The value to check.
  3716. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  3717. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  3718. */
  3719. function isIndex(value, length) {
  3720. value = (typeof value == 'number' || reIsUint.test(value)) ? +value : -1;
  3721. length = length == null ? MAX_SAFE_INTEGER : length;
  3722. return value > -1 && value % 1 == 0 && value < length;
  3723. }
  3724. /**
  3725. * Checks if the provided arguments are from an iteratee call.
  3726. *
  3727. * @private
  3728. * @param {*} value The potential iteratee value argument.
  3729. * @param {*} index The potential iteratee index or key argument.
  3730. * @param {*} object The potential iteratee object argument.
  3731. * @returns {boolean} Returns `true` if the arguments are from an iteratee call, else `false`.
  3732. */
  3733. function isIterateeCall(value, index, object) {
  3734. if (!isObject(object)) {
  3735. return false;
  3736. }
  3737. var type = typeof index;
  3738. if (type == 'number'
  3739. ? (isArrayLike(object) && isIndex(index, object.length))
  3740. : (type == 'string' && index in object)) {
  3741. var other = object[index];
  3742. return value === value ? (value === other) : (other !== other);
  3743. }
  3744. return false;
  3745. }
  3746. /**
  3747. * Checks if `value` is a property name and not a property path.
  3748. *
  3749. * @private
  3750. * @param {*} value The value to check.
  3751. * @param {Object} [object] The object to query keys on.
  3752. * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
  3753. */
  3754. function isKey(value, object) {
  3755. var type = typeof value;
  3756. if ((type == 'string' && reIsPlainProp.test(value)) || type == 'number') {
  3757. return true;
  3758. }
  3759. if (isArray(value)) {
  3760. return false;
  3761. }
  3762. var result = !reIsDeepProp.test(value);
  3763. return result || (object != null && value in toObject(object));
  3764. }
  3765. /**
  3766. * Checks if `func` has a lazy counterpart.
  3767. *
  3768. * @private
  3769. * @param {Function} func The function to check.
  3770. * @returns {boolean} Returns `true` if `func` has a lazy counterpart, else `false`.
  3771. */
  3772. function isLaziable(func) {
  3773. var funcName = getFuncName(func);
  3774. if (!(funcName in LazyWrapper.prototype)) {
  3775. return false;
  3776. }
  3777. var other = lodash[funcName];
  3778. if (func === other) {
  3779. return true;
  3780. }
  3781. var data = getData(other);
  3782. return !!data && func === data[0];
  3783. }
  3784. /**
  3785. * Checks if `value` is a valid array-like length.
  3786. *
  3787. * **Note:** This function is based on [`ToLength`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength).
  3788. *
  3789. * @private
  3790. * @param {*} value The value to check.
  3791. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  3792. */
  3793. function isLength(value) {
  3794. return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  3795. }
  3796. /**
  3797. * Checks if `value` is suitable for strict equality comparisons, i.e. `===`.
  3798. *
  3799. * @private
  3800. * @param {*} value The value to check.
  3801. * @returns {boolean} Returns `true` if `value` if suitable for strict
  3802. * equality comparisons, else `false`.
  3803. */
  3804. function isStrictComparable(value) {
  3805. return value === value && !isObject(value);
  3806. }
  3807. /**
  3808. * Merges the function metadata of `source` into `data`.
  3809. *
  3810. * Merging metadata reduces the number of wrappers required to invoke a function.
  3811. * This is possible because methods like `_.bind`, `_.curry`, and `_.partial`
  3812. * may be applied regardless of execution order. Methods like `_.ary` and `_.rearg`
  3813. * augment function arguments, making the order in which they are executed important,
  3814. * preventing the merging of metadata. However, we make an exception for a safe
  3815. * common case where curried functions have `_.ary` and or `_.rearg` applied.
  3816. *
  3817. * @private
  3818. * @param {Array} data The destination metadata.
  3819. * @param {Array} source The source metadata.
  3820. * @returns {Array} Returns `data`.
  3821. */
  3822. function mergeData(data, source) {
  3823. var bitmask = data[1],
  3824. srcBitmask = source[1],
  3825. newBitmask = bitmask | srcBitmask,
  3826. isCommon = newBitmask < ARY_FLAG;
  3827. var isCombo =
  3828. (srcBitmask == ARY_FLAG && bitmask == CURRY_FLAG) ||
  3829. (srcBitmask == ARY_FLAG && bitmask == REARG_FLAG && data[7].length <= source[8]) ||
  3830. (srcBitmask == (ARY_FLAG | REARG_FLAG) && bitmask == CURRY_FLAG);
  3831. // Exit early if metadata can't be merged.
  3832. if (!(isCommon || isCombo)) {
  3833. return data;
  3834. }
  3835. // Use source `thisArg` if available.
  3836. if (srcBitmask & BIND_FLAG) {
  3837. data[2] = source[2];
  3838. // Set when currying a bound function.
  3839. newBitmask |= (bitmask & BIND_FLAG) ? 0 : CURRY_BOUND_FLAG;
  3840. }
  3841. // Compose partial arguments.
  3842. var value = source[3];
  3843. if (value) {
  3844. var partials = data[3];
  3845. data[3] = partials ? composeArgs(partials, value, source[4]) : arrayCopy(value);
  3846. data[4] = partials ? replaceHolders(data[3], PLACEHOLDER) : arrayCopy(source[4]);
  3847. }
  3848. // Compose partial right arguments.
  3849. value = source[5];
  3850. if (value) {
  3851. partials = data[5];
  3852. data[5] = partials ? composeArgsRight(partials, value, source[6]) : arrayCopy(value);
  3853. data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : arrayCopy(source[6]);
  3854. }
  3855. // Use source `argPos` if available.
  3856. value = source[7];
  3857. if (value) {
  3858. data[7] = arrayCopy(value);
  3859. }
  3860. // Use source `ary` if it's smaller.
  3861. if (srcBitmask & ARY_FLAG) {
  3862. data[8] = data[8] == null ? source[8] : nativeMin(data[8], source[8]);
  3863. }
  3864. // Use source `arity` if one is not provided.
  3865. if (data[9] == null) {
  3866. data[9] = source[9];
  3867. }
  3868. // Use source `func` and merge bitmasks.
  3869. data[0] = source[0];
  3870. data[1] = newBitmask;
  3871. return data;
  3872. }
  3873. /**
  3874. * A specialized version of `_.pick` which picks `object` properties specified
  3875. * by `props`.
  3876. *
  3877. * @private
  3878. * @param {Object} object The source object.
  3879. * @param {string[]} props The property names to pick.
  3880. * @returns {Object} Returns the new object.
  3881. */
  3882. function pickByArray(object, props) {
  3883. object = toObject(object);
  3884. var index = -1,
  3885. length = props.length,
  3886. result = {};
  3887. while (++index < length) {
  3888. var key = props[index];
  3889. if (key in object) {
  3890. result[key] = object[key];
  3891. }
  3892. }
  3893. return result;
  3894. }
  3895. /**
  3896. * A specialized version of `_.pick` which picks `object` properties `predicate`
  3897. * returns truthy for.
  3898. *
  3899. * @private
  3900. * @param {Object} object The source object.
  3901. * @param {Function} predicate The function invoked per iteration.
  3902. * @returns {Object} Returns the new object.
  3903. */
  3904. function pickByCallback(object, predicate) {
  3905. var result = {};
  3906. baseForIn(object, function(value, key, object) {
  3907. if (predicate(value, key, object)) {
  3908. result[key] = value;
  3909. }
  3910. });
  3911. return result;
  3912. }
  3913. /**
  3914. * Reorder `array` according to the specified indexes where the element at
  3915. * the first index is assigned as the first element, the element at
  3916. * the second index is assigned as the second element, and so on.
  3917. *
  3918. * @private
  3919. * @param {Array} array The array to reorder.
  3920. * @param {Array} indexes The arranged array indexes.
  3921. * @returns {Array} Returns `array`.
  3922. */
  3923. function reorder(array, indexes) {
  3924. var arrLength = array.length,
  3925. length = nativeMin(indexes.length, arrLength),
  3926. oldArray = arrayCopy(array);
  3927. while (length--) {
  3928. var index = indexes[length];
  3929. array[length] = isIndex(index, arrLength) ? oldArray[index] : undefined;
  3930. }
  3931. return array;
  3932. }
  3933. /**
  3934. * Sets metadata for `func`.
  3935. *
  3936. * **Note:** If this function becomes hot, i.e. is invoked a lot in a short
  3937. * period of time, it will trip its breaker and transition to an identity function
  3938. * to avoid garbage collection pauses in V8. See [V8 issue 2070](https://code.google.com/p/v8/issues/detail?id=2070)
  3939. * for more details.
  3940. *
  3941. * @private
  3942. * @param {Function} func The function to associate metadata with.
  3943. * @param {*} data The metadata.
  3944. * @returns {Function} Returns `func`.
  3945. */
  3946. var setData = (function() {
  3947. var count = 0,
  3948. lastCalled = 0;
  3949. return function(key, value) {
  3950. var stamp = now(),
  3951. remaining = HOT_SPAN - (stamp - lastCalled);
  3952. lastCalled = stamp;
  3953. if (remaining > 0) {
  3954. if (++count >= HOT_COUNT) {
  3955. return key;
  3956. }
  3957. } else {
  3958. count = 0;
  3959. }
  3960. return baseSetData(key, value);
  3961. };
  3962. }());
  3963. /**
  3964. * A fallback implementation of `_.isPlainObject` which checks if `value`
  3965. * is an object created by the `Object` constructor or has a `[[Prototype]]`
  3966. * of `null`.
  3967. *
  3968. * @private
  3969. * @param {*} value The value to check.
  3970. * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
  3971. */
  3972. function shimIsPlainObject(value) {
  3973. var Ctor,
  3974. support = lodash.support;
  3975. // Exit early for non `Object` objects.
  3976. if (!(isObjectLike(value) && objToString.call(value) == objectTag) ||
  3977. (!hasOwnProperty.call(value, 'constructor') &&
  3978. (Ctor = value.constructor, typeof Ctor == 'function' && !(Ctor instanceof Ctor)))) {
  3979. return false;
  3980. }
  3981. // IE < 9 iterates inherited properties before own properties. If the first
  3982. // iterated property is an object's own property then there are no inherited
  3983. // enumerable properties.
  3984. var result;
  3985. // In most environments an object's own properties are iterated before
  3986. // its inherited properties. If the last iterated property is an object's
  3987. // own property then there are no inherited enumerable properties.
  3988. baseForIn(value, function(subValue, key) {
  3989. result = key;
  3990. });
  3991. return result === undefined || hasOwnProperty.call(value, result);
  3992. }
  3993. /**
  3994. * A fallback implementation of `Object.keys` which creates an array of the
  3995. * own enumerable property names of `object`.
  3996. *
  3997. * @private
  3998. * @param {Object} object The object to query.
  3999. * @returns {Array} Returns the array of property names.
  4000. */
  4001. function shimKeys(object) {
  4002. var props = keysIn(object),
  4003. propsLength = props.length,
  4004. length = propsLength && object.length;
  4005. var allowIndexes = !!length && isLength(length) &&
  4006. (isArray(object) || isArguments(object));
  4007. var index = -1,
  4008. result = [];
  4009. while (++index < propsLength) {
  4010. var key = props[index];
  4011. if ((allowIndexes && isIndex(key, length)) || hasOwnProperty.call(object, key)) {
  4012. result.push(key);
  4013. }
  4014. }
  4015. return result;
  4016. }
  4017. /**
  4018. * Converts `value` to an array-like object if it's not one.
  4019. *
  4020. * @private
  4021. * @param {*} value The value to process.
  4022. * @returns {Array|Object} Returns the array-like object.
  4023. */
  4024. function toIterable(value) {
  4025. if (value == null) {
  4026. return [];
  4027. }
  4028. if (!isArrayLike(value)) {
  4029. return values(value);
  4030. }
  4031. return isObject(value) ? value : Object(value);
  4032. }
  4033. /**
  4034. * Converts `value` to an object if it's not one.
  4035. *
  4036. * @private
  4037. * @param {*} value The value to process.
  4038. * @returns {Object} Returns the object.
  4039. */
  4040. function toObject(value) {
  4041. return isObject(value) ? value : Object(value);
  4042. }
  4043. /**
  4044. * Converts `value` to property path array if it's not one.
  4045. *
  4046. * @private
  4047. * @param {*} value The value to process.
  4048. * @returns {Array} Returns the property path array.
  4049. */
  4050. function toPath(value) {
  4051. if (isArray(value)) {
  4052. return value;
  4053. }
  4054. var result = [];
  4055. baseToString(value).replace(rePropName, function(match, number, quote, string) {
  4056. result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
  4057. });
  4058. return result;
  4059. }
  4060. /**
  4061. * Creates a clone of `wrapper`.
  4062. *
  4063. * @private
  4064. * @param {Object} wrapper The wrapper to clone.
  4065. * @returns {Object} Returns the cloned wrapper.
  4066. */
  4067. function wrapperClone(wrapper) {
  4068. return wrapper instanceof LazyWrapper
  4069. ? wrapper.clone()
  4070. : new LodashWrapper(wrapper.__wrapped__, wrapper.__chain__, arrayCopy(wrapper.__actions__));
  4071. }
  4072. /*------------------------------------------------------------------------*/
  4073. /**
  4074. * Creates an array of elements split into groups the length of `size`.
  4075. * If `collection` can't be split evenly, the final chunk will be the remaining
  4076. * elements.
  4077. *
  4078. * @static
  4079. * @memberOf _
  4080. * @category Array
  4081. * @param {Array} array The array to process.
  4082. * @param {number} [size=1] The length of each chunk.
  4083. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4084. * @returns {Array} Returns the new array containing chunks.
  4085. * @example
  4086. *
  4087. * _.chunk(['a', 'b', 'c', 'd'], 2);
  4088. * // => [['a', 'b'], ['c', 'd']]
  4089. *
  4090. * _.chunk(['a', 'b', 'c', 'd'], 3);
  4091. * // => [['a', 'b', 'c'], ['d']]
  4092. */
  4093. function chunk(array, size, guard) {
  4094. if (guard ? isIterateeCall(array, size, guard) : size == null) {
  4095. size = 1;
  4096. } else {
  4097. size = nativeMax(+size || 1, 1);
  4098. }
  4099. var index = 0,
  4100. length = array ? array.length : 0,
  4101. resIndex = -1,
  4102. result = Array(ceil(length / size));
  4103. while (index < length) {
  4104. result[++resIndex] = baseSlice(array, index, (index += size));
  4105. }
  4106. return result;
  4107. }
  4108. /**
  4109. * Creates an array with all falsey values removed. The values `false`, `null`,
  4110. * `0`, `""`, `undefined`, and `NaN` are falsey.
  4111. *
  4112. * @static
  4113. * @memberOf _
  4114. * @category Array
  4115. * @param {Array} array The array to compact.
  4116. * @returns {Array} Returns the new array of filtered values.
  4117. * @example
  4118. *
  4119. * _.compact([0, 1, false, 2, '', 3]);
  4120. * // => [1, 2, 3]
  4121. */
  4122. function compact(array) {
  4123. var index = -1,
  4124. length = array ? array.length : 0,
  4125. resIndex = -1,
  4126. result = [];
  4127. while (++index < length) {
  4128. var value = array[index];
  4129. if (value) {
  4130. result[++resIndex] = value;
  4131. }
  4132. }
  4133. return result;
  4134. }
  4135. /**
  4136. * Creates an array of unique `array` values not included in the other
  4137. * provided arrays using [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
  4138. * for equality comparisons.
  4139. *
  4140. * @static
  4141. * @memberOf _
  4142. * @category Array
  4143. * @param {Array} array The array to inspect.
  4144. * @param {...Array} [values] The arrays of values to exclude.
  4145. * @returns {Array} Returns the new array of filtered values.
  4146. * @example
  4147. *
  4148. * _.difference([1, 2, 3], [4, 2]);
  4149. * // => [1, 3]
  4150. */
  4151. var difference = restParam(function(array, values) {
  4152. return isArrayLike(array)
  4153. ? baseDifference(array, baseFlatten(values, false, true))
  4154. : [];
  4155. });
  4156. /**
  4157. * Creates a slice of `array` with `n` elements dropped from the beginning.
  4158. *
  4159. * @static
  4160. * @memberOf _
  4161. * @category Array
  4162. * @param {Array} array The array to query.
  4163. * @param {number} [n=1] The number of elements to drop.
  4164. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4165. * @returns {Array} Returns the slice of `array`.
  4166. * @example
  4167. *
  4168. * _.drop([1, 2, 3]);
  4169. * // => [2, 3]
  4170. *
  4171. * _.drop([1, 2, 3], 2);
  4172. * // => [3]
  4173. *
  4174. * _.drop([1, 2, 3], 5);
  4175. * // => []
  4176. *
  4177. * _.drop([1, 2, 3], 0);
  4178. * // => [1, 2, 3]
  4179. */
  4180. function drop(array, n, guard) {
  4181. var length = array ? array.length : 0;
  4182. if (!length) {
  4183. return [];
  4184. }
  4185. if (guard ? isIterateeCall(array, n, guard) : n == null) {
  4186. n = 1;
  4187. }
  4188. return baseSlice(array, n < 0 ? 0 : n);
  4189. }
  4190. /**
  4191. * Creates a slice of `array` with `n` elements dropped from the end.
  4192. *
  4193. * @static
  4194. * @memberOf _
  4195. * @category Array
  4196. * @param {Array} array The array to query.
  4197. * @param {number} [n=1] The number of elements to drop.
  4198. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4199. * @returns {Array} Returns the slice of `array`.
  4200. * @example
  4201. *
  4202. * _.dropRight([1, 2, 3]);
  4203. * // => [1, 2]
  4204. *
  4205. * _.dropRight([1, 2, 3], 2);
  4206. * // => [1]
  4207. *
  4208. * _.dropRight([1, 2, 3], 5);
  4209. * // => []
  4210. *
  4211. * _.dropRight([1, 2, 3], 0);
  4212. * // => [1, 2, 3]
  4213. */
  4214. function dropRight(array, n, guard) {
  4215. var length = array ? array.length : 0;
  4216. if (!length) {
  4217. return [];
  4218. }
  4219. if (guard ? isIterateeCall(array, n, guard) : n == null) {
  4220. n = 1;
  4221. }
  4222. n = length - (+n || 0);
  4223. return baseSlice(array, 0, n < 0 ? 0 : n);
  4224. }
  4225. /**
  4226. * Creates a slice of `array` excluding elements dropped from the end.
  4227. * Elements are dropped until `predicate` returns falsey. The predicate is
  4228. * bound to `thisArg` and invoked with three arguments: (value, index, array).
  4229. *
  4230. * If a property name is provided for `predicate` the created `_.property`
  4231. * style callback returns the property value of the given element.
  4232. *
  4233. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4234. * style callback returns `true` for elements that have a matching property
  4235. * value, else `false`.
  4236. *
  4237. * If an object is provided for `predicate` the created `_.matches` style
  4238. * callback returns `true` for elements that match the properties of the given
  4239. * object, else `false`.
  4240. *
  4241. * @static
  4242. * @memberOf _
  4243. * @category Array
  4244. * @param {Array} array The array to query.
  4245. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  4246. * per iteration.
  4247. * @param {*} [thisArg] The `this` binding of `predicate`.
  4248. * @returns {Array} Returns the slice of `array`.
  4249. * @example
  4250. *
  4251. * _.dropRightWhile([1, 2, 3], function(n) {
  4252. * return n > 1;
  4253. * });
  4254. * // => [1]
  4255. *
  4256. * var users = [
  4257. * { 'user': 'barney', 'active': true },
  4258. * { 'user': 'fred', 'active': false },
  4259. * { 'user': 'pebbles', 'active': false }
  4260. * ];
  4261. *
  4262. * // using the `_.matches` callback shorthand
  4263. * _.pluck(_.dropRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user');
  4264. * // => ['barney', 'fred']
  4265. *
  4266. * // using the `_.matchesProperty` callback shorthand
  4267. * _.pluck(_.dropRightWhile(users, 'active', false), 'user');
  4268. * // => ['barney']
  4269. *
  4270. * // using the `_.property` callback shorthand
  4271. * _.pluck(_.dropRightWhile(users, 'active'), 'user');
  4272. * // => ['barney', 'fred', 'pebbles']
  4273. */
  4274. function dropRightWhile(array, predicate, thisArg) {
  4275. return (array && array.length)
  4276. ? baseWhile(array, getCallback(predicate, thisArg, 3), true, true)
  4277. : [];
  4278. }
  4279. /**
  4280. * Creates a slice of `array` excluding elements dropped from the beginning.
  4281. * Elements are dropped until `predicate` returns falsey. The predicate is
  4282. * bound to `thisArg` and invoked with three arguments: (value, index, array).
  4283. *
  4284. * If a property name is provided for `predicate` the created `_.property`
  4285. * style callback returns the property value of the given element.
  4286. *
  4287. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4288. * style callback returns `true` for elements that have a matching property
  4289. * value, else `false`.
  4290. *
  4291. * If an object is provided for `predicate` the created `_.matches` style
  4292. * callback returns `true` for elements that have the properties of the given
  4293. * object, else `false`.
  4294. *
  4295. * @static
  4296. * @memberOf _
  4297. * @category Array
  4298. * @param {Array} array The array to query.
  4299. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  4300. * per iteration.
  4301. * @param {*} [thisArg] The `this` binding of `predicate`.
  4302. * @returns {Array} Returns the slice of `array`.
  4303. * @example
  4304. *
  4305. * _.dropWhile([1, 2, 3], function(n) {
  4306. * return n < 3;
  4307. * });
  4308. * // => [3]
  4309. *
  4310. * var users = [
  4311. * { 'user': 'barney', 'active': false },
  4312. * { 'user': 'fred', 'active': false },
  4313. * { 'user': 'pebbles', 'active': true }
  4314. * ];
  4315. *
  4316. * // using the `_.matches` callback shorthand
  4317. * _.pluck(_.dropWhile(users, { 'user': 'barney', 'active': false }), 'user');
  4318. * // => ['fred', 'pebbles']
  4319. *
  4320. * // using the `_.matchesProperty` callback shorthand
  4321. * _.pluck(_.dropWhile(users, 'active', false), 'user');
  4322. * // => ['pebbles']
  4323. *
  4324. * // using the `_.property` callback shorthand
  4325. * _.pluck(_.dropWhile(users, 'active'), 'user');
  4326. * // => ['barney', 'fred', 'pebbles']
  4327. */
  4328. function dropWhile(array, predicate, thisArg) {
  4329. return (array && array.length)
  4330. ? baseWhile(array, getCallback(predicate, thisArg, 3), true)
  4331. : [];
  4332. }
  4333. /**
  4334. * Fills elements of `array` with `value` from `start` up to, but not
  4335. * including, `end`.
  4336. *
  4337. * **Note:** This method mutates `array`.
  4338. *
  4339. * @static
  4340. * @memberOf _
  4341. * @category Array
  4342. * @param {Array} array The array to fill.
  4343. * @param {*} value The value to fill `array` with.
  4344. * @param {number} [start=0] The start position.
  4345. * @param {number} [end=array.length] The end position.
  4346. * @returns {Array} Returns `array`.
  4347. * @example
  4348. *
  4349. * var array = [1, 2, 3];
  4350. *
  4351. * _.fill(array, 'a');
  4352. * console.log(array);
  4353. * // => ['a', 'a', 'a']
  4354. *
  4355. * _.fill(Array(3), 2);
  4356. * // => [2, 2, 2]
  4357. *
  4358. * _.fill([4, 6, 8], '*', 1, 2);
  4359. * // => [4, '*', 8]
  4360. */
  4361. function fill(array, value, start, end) {
  4362. var length = array ? array.length : 0;
  4363. if (!length) {
  4364. return [];
  4365. }
  4366. if (start && typeof start != 'number' && isIterateeCall(array, value, start)) {
  4367. start = 0;
  4368. end = length;
  4369. }
  4370. return baseFill(array, value, start, end);
  4371. }
  4372. /**
  4373. * This method is like `_.find` except that it returns the index of the first
  4374. * element `predicate` returns truthy for instead of the element itself.
  4375. *
  4376. * If a property name is provided for `predicate` the created `_.property`
  4377. * style callback returns the property value of the given element.
  4378. *
  4379. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4380. * style callback returns `true` for elements that have a matching property
  4381. * value, else `false`.
  4382. *
  4383. * If an object is provided for `predicate` the created `_.matches` style
  4384. * callback returns `true` for elements that have the properties of the given
  4385. * object, else `false`.
  4386. *
  4387. * @static
  4388. * @memberOf _
  4389. * @category Array
  4390. * @param {Array} array The array to search.
  4391. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  4392. * per iteration.
  4393. * @param {*} [thisArg] The `this` binding of `predicate`.
  4394. * @returns {number} Returns the index of the found element, else `-1`.
  4395. * @example
  4396. *
  4397. * var users = [
  4398. * { 'user': 'barney', 'active': false },
  4399. * { 'user': 'fred', 'active': false },
  4400. * { 'user': 'pebbles', 'active': true }
  4401. * ];
  4402. *
  4403. * _.findIndex(users, function(chr) {
  4404. * return chr.user == 'barney';
  4405. * });
  4406. * // => 0
  4407. *
  4408. * // using the `_.matches` callback shorthand
  4409. * _.findIndex(users, { 'user': 'fred', 'active': false });
  4410. * // => 1
  4411. *
  4412. * // using the `_.matchesProperty` callback shorthand
  4413. * _.findIndex(users, 'active', false);
  4414. * // => 0
  4415. *
  4416. * // using the `_.property` callback shorthand
  4417. * _.findIndex(users, 'active');
  4418. * // => 2
  4419. */
  4420. var findIndex = createFindIndex();
  4421. /**
  4422. * This method is like `_.findIndex` except that it iterates over elements
  4423. * of `collection` from right to left.
  4424. *
  4425. * If a property name is provided for `predicate` the created `_.property`
  4426. * style callback returns the property value of the given element.
  4427. *
  4428. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4429. * style callback returns `true` for elements that have a matching property
  4430. * value, else `false`.
  4431. *
  4432. * If an object is provided for `predicate` the created `_.matches` style
  4433. * callback returns `true` for elements that have the properties of the given
  4434. * object, else `false`.
  4435. *
  4436. * @static
  4437. * @memberOf _
  4438. * @category Array
  4439. * @param {Array} array The array to search.
  4440. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  4441. * per iteration.
  4442. * @param {*} [thisArg] The `this` binding of `predicate`.
  4443. * @returns {number} Returns the index of the found element, else `-1`.
  4444. * @example
  4445. *
  4446. * var users = [
  4447. * { 'user': 'barney', 'active': true },
  4448. * { 'user': 'fred', 'active': false },
  4449. * { 'user': 'pebbles', 'active': false }
  4450. * ];
  4451. *
  4452. * _.findLastIndex(users, function(chr) {
  4453. * return chr.user == 'pebbles';
  4454. * });
  4455. * // => 2
  4456. *
  4457. * // using the `_.matches` callback shorthand
  4458. * _.findLastIndex(users, { 'user': 'barney', 'active': true });
  4459. * // => 0
  4460. *
  4461. * // using the `_.matchesProperty` callback shorthand
  4462. * _.findLastIndex(users, 'active', false);
  4463. * // => 2
  4464. *
  4465. * // using the `_.property` callback shorthand
  4466. * _.findLastIndex(users, 'active');
  4467. * // => 0
  4468. */
  4469. var findLastIndex = createFindIndex(true);
  4470. /**
  4471. * Gets the first element of `array`.
  4472. *
  4473. * @static
  4474. * @memberOf _
  4475. * @alias head
  4476. * @category Array
  4477. * @param {Array} array The array to query.
  4478. * @returns {*} Returns the first element of `array`.
  4479. * @example
  4480. *
  4481. * _.first([1, 2, 3]);
  4482. * // => 1
  4483. *
  4484. * _.first([]);
  4485. * // => undefined
  4486. */
  4487. function first(array) {
  4488. return array ? array[0] : undefined;
  4489. }
  4490. /**
  4491. * Flattens a nested array. If `isDeep` is `true` the array is recursively
  4492. * flattened, otherwise it is only flattened a single level.
  4493. *
  4494. * @static
  4495. * @memberOf _
  4496. * @category Array
  4497. * @param {Array} array The array to flatten.
  4498. * @param {boolean} [isDeep] Specify a deep flatten.
  4499. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4500. * @returns {Array} Returns the new flattened array.
  4501. * @example
  4502. *
  4503. * _.flatten([1, [2, 3, [4]]]);
  4504. * // => [1, 2, 3, [4]]
  4505. *
  4506. * // using `isDeep`
  4507. * _.flatten([1, [2, 3, [4]]], true);
  4508. * // => [1, 2, 3, 4]
  4509. */
  4510. function flatten(array, isDeep, guard) {
  4511. var length = array ? array.length : 0;
  4512. if (guard && isIterateeCall(array, isDeep, guard)) {
  4513. isDeep = false;
  4514. }
  4515. return length ? baseFlatten(array, isDeep) : [];
  4516. }
  4517. /**
  4518. * Recursively flattens a nested array.
  4519. *
  4520. * @static
  4521. * @memberOf _
  4522. * @category Array
  4523. * @param {Array} array The array to recursively flatten.
  4524. * @returns {Array} Returns the new flattened array.
  4525. * @example
  4526. *
  4527. * _.flattenDeep([1, [2, 3, [4]]]);
  4528. * // => [1, 2, 3, 4]
  4529. */
  4530. function flattenDeep(array) {
  4531. var length = array ? array.length : 0;
  4532. return length ? baseFlatten(array, true) : [];
  4533. }
  4534. /**
  4535. * Gets the index at which the first occurrence of `value` is found in `array`
  4536. * using [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
  4537. * for equality comparisons. If `fromIndex` is negative, it is used as the offset
  4538. * from the end of `array`. If `array` is sorted providing `true` for `fromIndex`
  4539. * performs a faster binary search.
  4540. *
  4541. * @static
  4542. * @memberOf _
  4543. * @category Array
  4544. * @param {Array} array The array to search.
  4545. * @param {*} value The value to search for.
  4546. * @param {boolean|number} [fromIndex=0] The index to search from or `true`
  4547. * to perform a binary search on a sorted array.
  4548. * @returns {number} Returns the index of the matched value, else `-1`.
  4549. * @example
  4550. *
  4551. * _.indexOf([1, 2, 1, 2], 2);
  4552. * // => 1
  4553. *
  4554. * // using `fromIndex`
  4555. * _.indexOf([1, 2, 1, 2], 2, 2);
  4556. * // => 3
  4557. *
  4558. * // performing a binary search
  4559. * _.indexOf([1, 1, 2, 2], 2, true);
  4560. * // => 2
  4561. */
  4562. function indexOf(array, value, fromIndex) {
  4563. var length = array ? array.length : 0;
  4564. if (!length) {
  4565. return -1;
  4566. }
  4567. if (typeof fromIndex == 'number') {
  4568. fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex;
  4569. } else if (fromIndex) {
  4570. var index = binaryIndex(array, value),
  4571. other = array[index];
  4572. if (value === value ? (value === other) : (other !== other)) {
  4573. return index;
  4574. }
  4575. return -1;
  4576. }
  4577. return baseIndexOf(array, value, fromIndex || 0);
  4578. }
  4579. /**
  4580. * Gets all but the last element of `array`.
  4581. *
  4582. * @static
  4583. * @memberOf _
  4584. * @category Array
  4585. * @param {Array} array The array to query.
  4586. * @returns {Array} Returns the slice of `array`.
  4587. * @example
  4588. *
  4589. * _.initial([1, 2, 3]);
  4590. * // => [1, 2]
  4591. */
  4592. function initial(array) {
  4593. return dropRight(array, 1);
  4594. }
  4595. /**
  4596. * Creates an array of unique values that are included in all of the provided
  4597. * arrays using [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
  4598. * for equality comparisons.
  4599. *
  4600. * @static
  4601. * @memberOf _
  4602. * @category Array
  4603. * @param {...Array} [arrays] The arrays to inspect.
  4604. * @returns {Array} Returns the new array of shared values.
  4605. * @example
  4606. * _.intersection([1, 2], [4, 2], [2, 1]);
  4607. * // => [2]
  4608. */
  4609. var intersection = restParam(function(arrays) {
  4610. var othLength = arrays.length,
  4611. othIndex = othLength,
  4612. caches = Array(length),
  4613. indexOf = getIndexOf(),
  4614. isCommon = indexOf == baseIndexOf,
  4615. result = [];
  4616. while (othIndex--) {
  4617. var value = arrays[othIndex] = isArrayLike(value = arrays[othIndex]) ? value : [];
  4618. caches[othIndex] = (isCommon && value.length >= 120) ? createCache(othIndex && value) : null;
  4619. }
  4620. var array = arrays[0],
  4621. index = -1,
  4622. length = array ? array.length : 0,
  4623. seen = caches[0];
  4624. outer:
  4625. while (++index < length) {
  4626. value = array[index];
  4627. if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value, 0)) < 0) {
  4628. var othIndex = othLength;
  4629. while (--othIndex) {
  4630. var cache = caches[othIndex];
  4631. if ((cache ? cacheIndexOf(cache, value) : indexOf(arrays[othIndex], value, 0)) < 0) {
  4632. continue outer;
  4633. }
  4634. }
  4635. if (seen) {
  4636. seen.push(value);
  4637. }
  4638. result.push(value);
  4639. }
  4640. }
  4641. return result;
  4642. });
  4643. /**
  4644. * Gets the last element of `array`.
  4645. *
  4646. * @static
  4647. * @memberOf _
  4648. * @category Array
  4649. * @param {Array} array The array to query.
  4650. * @returns {*} Returns the last element of `array`.
  4651. * @example
  4652. *
  4653. * _.last([1, 2, 3]);
  4654. * // => 3
  4655. */
  4656. function last(array) {
  4657. var length = array ? array.length : 0;
  4658. return length ? array[length - 1] : undefined;
  4659. }
  4660. /**
  4661. * This method is like `_.indexOf` except that it iterates over elements of
  4662. * `array` from right to left.
  4663. *
  4664. * @static
  4665. * @memberOf _
  4666. * @category Array
  4667. * @param {Array} array The array to search.
  4668. * @param {*} value The value to search for.
  4669. * @param {boolean|number} [fromIndex=array.length-1] The index to search from
  4670. * or `true` to perform a binary search on a sorted array.
  4671. * @returns {number} Returns the index of the matched value, else `-1`.
  4672. * @example
  4673. *
  4674. * _.lastIndexOf([1, 2, 1, 2], 2);
  4675. * // => 3
  4676. *
  4677. * // using `fromIndex`
  4678. * _.lastIndexOf([1, 2, 1, 2], 2, 2);
  4679. * // => 1
  4680. *
  4681. * // performing a binary search
  4682. * _.lastIndexOf([1, 1, 2, 2], 2, true);
  4683. * // => 3
  4684. */
  4685. function lastIndexOf(array, value, fromIndex) {
  4686. var length = array ? array.length : 0;
  4687. if (!length) {
  4688. return -1;
  4689. }
  4690. var index = length;
  4691. if (typeof fromIndex == 'number') {
  4692. index = (fromIndex < 0 ? nativeMax(length + fromIndex, 0) : nativeMin(fromIndex || 0, length - 1)) + 1;
  4693. } else if (fromIndex) {
  4694. index = binaryIndex(array, value, true) - 1;
  4695. var other = array[index];
  4696. if (value === value ? (value === other) : (other !== other)) {
  4697. return index;
  4698. }
  4699. return -1;
  4700. }
  4701. if (value !== value) {
  4702. return indexOfNaN(array, index, true);
  4703. }
  4704. while (index--) {
  4705. if (array[index] === value) {
  4706. return index;
  4707. }
  4708. }
  4709. return -1;
  4710. }
  4711. /**
  4712. * Removes all provided values from `array` using
  4713. * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
  4714. * for equality comparisons.
  4715. *
  4716. * **Note:** Unlike `_.without`, this method mutates `array`.
  4717. *
  4718. * @static
  4719. * @memberOf _
  4720. * @category Array
  4721. * @param {Array} array The array to modify.
  4722. * @param {...*} [values] The values to remove.
  4723. * @returns {Array} Returns `array`.
  4724. * @example
  4725. *
  4726. * var array = [1, 2, 3, 1, 2, 3];
  4727. *
  4728. * _.pull(array, 2, 3);
  4729. * console.log(array);
  4730. * // => [1, 1]
  4731. */
  4732. function pull() {
  4733. var args = arguments,
  4734. array = args[0];
  4735. if (!(array && array.length)) {
  4736. return array;
  4737. }
  4738. var index = 0,
  4739. indexOf = getIndexOf(),
  4740. length = args.length;
  4741. while (++index < length) {
  4742. var fromIndex = 0,
  4743. value = args[index];
  4744. while ((fromIndex = indexOf(array, value, fromIndex)) > -1) {
  4745. splice.call(array, fromIndex, 1);
  4746. }
  4747. }
  4748. return array;
  4749. }
  4750. /**
  4751. * Removes elements from `array` corresponding to the given indexes and returns
  4752. * an array of the removed elements. Indexes may be specified as an array of
  4753. * indexes or as individual arguments.
  4754. *
  4755. * **Note:** Unlike `_.at`, this method mutates `array`.
  4756. *
  4757. * @static
  4758. * @memberOf _
  4759. * @category Array
  4760. * @param {Array} array The array to modify.
  4761. * @param {...(number|number[])} [indexes] The indexes of elements to remove,
  4762. * specified as individual indexes or arrays of indexes.
  4763. * @returns {Array} Returns the new array of removed elements.
  4764. * @example
  4765. *
  4766. * var array = [5, 10, 15, 20];
  4767. * var evens = _.pullAt(array, 1, 3);
  4768. *
  4769. * console.log(array);
  4770. * // => [5, 15]
  4771. *
  4772. * console.log(evens);
  4773. * // => [10, 20]
  4774. */
  4775. var pullAt = restParam(function(array, indexes) {
  4776. indexes = baseFlatten(indexes);
  4777. var result = baseAt(array, indexes);
  4778. basePullAt(array, indexes.sort(baseCompareAscending));
  4779. return result;
  4780. });
  4781. /**
  4782. * Removes all elements from `array` that `predicate` returns truthy for
  4783. * and returns an array of the removed elements. The predicate is bound to
  4784. * `thisArg` and invoked with three arguments: (value, index, array).
  4785. *
  4786. * If a property name is provided for `predicate` the created `_.property`
  4787. * style callback returns the property value of the given element.
  4788. *
  4789. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4790. * style callback returns `true` for elements that have a matching property
  4791. * value, else `false`.
  4792. *
  4793. * If an object is provided for `predicate` the created `_.matches` style
  4794. * callback returns `true` for elements that have the properties of the given
  4795. * object, else `false`.
  4796. *
  4797. * **Note:** Unlike `_.filter`, this method mutates `array`.
  4798. *
  4799. * @static
  4800. * @memberOf _
  4801. * @category Array
  4802. * @param {Array} array The array to modify.
  4803. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  4804. * per iteration.
  4805. * @param {*} [thisArg] The `this` binding of `predicate`.
  4806. * @returns {Array} Returns the new array of removed elements.
  4807. * @example
  4808. *
  4809. * var array = [1, 2, 3, 4];
  4810. * var evens = _.remove(array, function(n) {
  4811. * return n % 2 == 0;
  4812. * });
  4813. *
  4814. * console.log(array);
  4815. * // => [1, 3]
  4816. *
  4817. * console.log(evens);
  4818. * // => [2, 4]
  4819. */
  4820. function remove(array, predicate, thisArg) {
  4821. var result = [];
  4822. if (!(array && array.length)) {
  4823. return result;
  4824. }
  4825. var index = -1,
  4826. indexes = [],
  4827. length = array.length;
  4828. predicate = getCallback(predicate, thisArg, 3);
  4829. while (++index < length) {
  4830. var value = array[index];
  4831. if (predicate(value, index, array)) {
  4832. result.push(value);
  4833. indexes.push(index);
  4834. }
  4835. }
  4836. basePullAt(array, indexes);
  4837. return result;
  4838. }
  4839. /**
  4840. * Gets all but the first element of `array`.
  4841. *
  4842. * @static
  4843. * @memberOf _
  4844. * @alias tail
  4845. * @category Array
  4846. * @param {Array} array The array to query.
  4847. * @returns {Array} Returns the slice of `array`.
  4848. * @example
  4849. *
  4850. * _.rest([1, 2, 3]);
  4851. * // => [2, 3]
  4852. */
  4853. function rest(array) {
  4854. return drop(array, 1);
  4855. }
  4856. /**
  4857. * Creates a slice of `array` from `start` up to, but not including, `end`.
  4858. *
  4859. * **Note:** This method is used instead of `Array#slice` to support node
  4860. * lists in IE < 9 and to ensure dense arrays are returned.
  4861. *
  4862. * @static
  4863. * @memberOf _
  4864. * @category Array
  4865. * @param {Array} array The array to slice.
  4866. * @param {number} [start=0] The start position.
  4867. * @param {number} [end=array.length] The end position.
  4868. * @returns {Array} Returns the slice of `array`.
  4869. */
  4870. function slice(array, start, end) {
  4871. var length = array ? array.length : 0;
  4872. if (!length) {
  4873. return [];
  4874. }
  4875. if (end && typeof end != 'number' && isIterateeCall(array, start, end)) {
  4876. start = 0;
  4877. end = length;
  4878. }
  4879. return baseSlice(array, start, end);
  4880. }
  4881. /**
  4882. * Uses a binary search to determine the lowest index at which `value` should
  4883. * be inserted into `array` in order to maintain its sort order. If an iteratee
  4884. * function is provided it is invoked for `value` and each element of `array`
  4885. * to compute their sort ranking. The iteratee is bound to `thisArg` and
  4886. * invoked with one argument; (value).
  4887. *
  4888. * If a property name is provided for `iteratee` the created `_.property`
  4889. * style callback returns the property value of the given element.
  4890. *
  4891. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  4892. * style callback returns `true` for elements that have a matching property
  4893. * value, else `false`.
  4894. *
  4895. * If an object is provided for `iteratee` the created `_.matches` style
  4896. * callback returns `true` for elements that have the properties of the given
  4897. * object, else `false`.
  4898. *
  4899. * @static
  4900. * @memberOf _
  4901. * @category Array
  4902. * @param {Array} array The sorted array to inspect.
  4903. * @param {*} value The value to evaluate.
  4904. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  4905. * per iteration.
  4906. * @param {*} [thisArg] The `this` binding of `iteratee`.
  4907. * @returns {number} Returns the index at which `value` should be inserted
  4908. * into `array`.
  4909. * @example
  4910. *
  4911. * _.sortedIndex([30, 50], 40);
  4912. * // => 1
  4913. *
  4914. * _.sortedIndex([4, 4, 5, 5], 5);
  4915. * // => 2
  4916. *
  4917. * var dict = { 'data': { 'thirty': 30, 'forty': 40, 'fifty': 50 } };
  4918. *
  4919. * // using an iteratee function
  4920. * _.sortedIndex(['thirty', 'fifty'], 'forty', function(word) {
  4921. * return this.data[word];
  4922. * }, dict);
  4923. * // => 1
  4924. *
  4925. * // using the `_.property` callback shorthand
  4926. * _.sortedIndex([{ 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
  4927. * // => 1
  4928. */
  4929. var sortedIndex = createSortedIndex();
  4930. /**
  4931. * This method is like `_.sortedIndex` except that it returns the highest
  4932. * index at which `value` should be inserted into `array` in order to
  4933. * maintain its sort order.
  4934. *
  4935. * @static
  4936. * @memberOf _
  4937. * @category Array
  4938. * @param {Array} array The sorted array to inspect.
  4939. * @param {*} value The value to evaluate.
  4940. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  4941. * per iteration.
  4942. * @param {*} [thisArg] The `this` binding of `iteratee`.
  4943. * @returns {number} Returns the index at which `value` should be inserted
  4944. * into `array`.
  4945. * @example
  4946. *
  4947. * _.sortedLastIndex([4, 4, 5, 5], 5);
  4948. * // => 4
  4949. */
  4950. var sortedLastIndex = createSortedIndex(true);
  4951. /**
  4952. * Creates a slice of `array` with `n` elements taken from the beginning.
  4953. *
  4954. * @static
  4955. * @memberOf _
  4956. * @category Array
  4957. * @param {Array} array The array to query.
  4958. * @param {number} [n=1] The number of elements to take.
  4959. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4960. * @returns {Array} Returns the slice of `array`.
  4961. * @example
  4962. *
  4963. * _.take([1, 2, 3]);
  4964. * // => [1]
  4965. *
  4966. * _.take([1, 2, 3], 2);
  4967. * // => [1, 2]
  4968. *
  4969. * _.take([1, 2, 3], 5);
  4970. * // => [1, 2, 3]
  4971. *
  4972. * _.take([1, 2, 3], 0);
  4973. * // => []
  4974. */
  4975. function take(array, n, guard) {
  4976. var length = array ? array.length : 0;
  4977. if (!length) {
  4978. return [];
  4979. }
  4980. if (guard ? isIterateeCall(array, n, guard) : n == null) {
  4981. n = 1;
  4982. }
  4983. return baseSlice(array, 0, n < 0 ? 0 : n);
  4984. }
  4985. /**
  4986. * Creates a slice of `array` with `n` elements taken from the end.
  4987. *
  4988. * @static
  4989. * @memberOf _
  4990. * @category Array
  4991. * @param {Array} array The array to query.
  4992. * @param {number} [n=1] The number of elements to take.
  4993. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  4994. * @returns {Array} Returns the slice of `array`.
  4995. * @example
  4996. *
  4997. * _.takeRight([1, 2, 3]);
  4998. * // => [3]
  4999. *
  5000. * _.takeRight([1, 2, 3], 2);
  5001. * // => [2, 3]
  5002. *
  5003. * _.takeRight([1, 2, 3], 5);
  5004. * // => [1, 2, 3]
  5005. *
  5006. * _.takeRight([1, 2, 3], 0);
  5007. * // => []
  5008. */
  5009. function takeRight(array, n, guard) {
  5010. var length = array ? array.length : 0;
  5011. if (!length) {
  5012. return [];
  5013. }
  5014. if (guard ? isIterateeCall(array, n, guard) : n == null) {
  5015. n = 1;
  5016. }
  5017. n = length - (+n || 0);
  5018. return baseSlice(array, n < 0 ? 0 : n);
  5019. }
  5020. /**
  5021. * Creates a slice of `array` with elements taken from the end. Elements are
  5022. * taken until `predicate` returns falsey. The predicate is bound to `thisArg`
  5023. * and invoked with three arguments: (value, index, array).
  5024. *
  5025. * If a property name is provided for `predicate` the created `_.property`
  5026. * style callback returns the property value of the given element.
  5027. *
  5028. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5029. * style callback returns `true` for elements that have a matching property
  5030. * value, else `false`.
  5031. *
  5032. * If an object is provided for `predicate` the created `_.matches` style
  5033. * callback returns `true` for elements that have the properties of the given
  5034. * object, else `false`.
  5035. *
  5036. * @static
  5037. * @memberOf _
  5038. * @category Array
  5039. * @param {Array} array The array to query.
  5040. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5041. * per iteration.
  5042. * @param {*} [thisArg] The `this` binding of `predicate`.
  5043. * @returns {Array} Returns the slice of `array`.
  5044. * @example
  5045. *
  5046. * _.takeRightWhile([1, 2, 3], function(n) {
  5047. * return n > 1;
  5048. * });
  5049. * // => [2, 3]
  5050. *
  5051. * var users = [
  5052. * { 'user': 'barney', 'active': true },
  5053. * { 'user': 'fred', 'active': false },
  5054. * { 'user': 'pebbles', 'active': false }
  5055. * ];
  5056. *
  5057. * // using the `_.matches` callback shorthand
  5058. * _.pluck(_.takeRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user');
  5059. * // => ['pebbles']
  5060. *
  5061. * // using the `_.matchesProperty` callback shorthand
  5062. * _.pluck(_.takeRightWhile(users, 'active', false), 'user');
  5063. * // => ['fred', 'pebbles']
  5064. *
  5065. * // using the `_.property` callback shorthand
  5066. * _.pluck(_.takeRightWhile(users, 'active'), 'user');
  5067. * // => []
  5068. */
  5069. function takeRightWhile(array, predicate, thisArg) {
  5070. return (array && array.length)
  5071. ? baseWhile(array, getCallback(predicate, thisArg, 3), false, true)
  5072. : [];
  5073. }
  5074. /**
  5075. * Creates a slice of `array` with elements taken from the beginning. Elements
  5076. * are taken until `predicate` returns falsey. The predicate is bound to
  5077. * `thisArg` and invoked with three arguments: (value, index, array).
  5078. *
  5079. * If a property name is provided for `predicate` the created `_.property`
  5080. * style callback returns the property value of the given element.
  5081. *
  5082. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5083. * style callback returns `true` for elements that have a matching property
  5084. * value, else `false`.
  5085. *
  5086. * If an object is provided for `predicate` the created `_.matches` style
  5087. * callback returns `true` for elements that have the properties of the given
  5088. * object, else `false`.
  5089. *
  5090. * @static
  5091. * @memberOf _
  5092. * @category Array
  5093. * @param {Array} array The array to query.
  5094. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5095. * per iteration.
  5096. * @param {*} [thisArg] The `this` binding of `predicate`.
  5097. * @returns {Array} Returns the slice of `array`.
  5098. * @example
  5099. *
  5100. * _.takeWhile([1, 2, 3], function(n) {
  5101. * return n < 3;
  5102. * });
  5103. * // => [1, 2]
  5104. *
  5105. * var users = [
  5106. * { 'user': 'barney', 'active': false },
  5107. * { 'user': 'fred', 'active': false},
  5108. * { 'user': 'pebbles', 'active': true }
  5109. * ];
  5110. *
  5111. * // using the `_.matches` callback shorthand
  5112. * _.pluck(_.takeWhile(users, { 'user': 'barney', 'active': false }), 'user');
  5113. * // => ['barney']
  5114. *
  5115. * // using the `_.matchesProperty` callback shorthand
  5116. * _.pluck(_.takeWhile(users, 'active', false), 'user');
  5117. * // => ['barney', 'fred']
  5118. *
  5119. * // using the `_.property` callback shorthand
  5120. * _.pluck(_.takeWhile(users, 'active'), 'user');
  5121. * // => []
  5122. */
  5123. function takeWhile(array, predicate, thisArg) {
  5124. return (array && array.length)
  5125. ? baseWhile(array, getCallback(predicate, thisArg, 3))
  5126. : [];
  5127. }
  5128. /**
  5129. * Creates an array of unique values, in order, from all of the provided arrays
  5130. * using [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
  5131. * for equality comparisons.
  5132. *
  5133. * @static
  5134. * @memberOf _
  5135. * @category Array
  5136. * @param {...Array} [arrays] The arrays to inspect.
  5137. * @returns {Array} Returns the new array of combined values.
  5138. * @example
  5139. *
  5140. * _.union([1, 2], [4, 2], [2, 1]);
  5141. * // => [1, 2, 4]
  5142. */
  5143. var union = restParam(function(arrays) {
  5144. return baseUniq(baseFlatten(arrays, false, true));
  5145. });
  5146. /**
  5147. * Creates a duplicate-free version of an array, using
  5148. * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
  5149. * for equality comparisons, in which only the first occurence of each element
  5150. * is kept. Providing `true` for `isSorted` performs a faster search algorithm
  5151. * for sorted arrays. If an iteratee function is provided it is invoked for
  5152. * each element in the array to generate the criterion by which uniqueness
  5153. * is computed. The `iteratee` is bound to `thisArg` and invoked with three
  5154. * arguments: (value, index, array).
  5155. *
  5156. * If a property name is provided for `iteratee` the created `_.property`
  5157. * style callback returns the property value of the given element.
  5158. *
  5159. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5160. * style callback returns `true` for elements that have a matching property
  5161. * value, else `false`.
  5162. *
  5163. * If an object is provided for `iteratee` the created `_.matches` style
  5164. * callback returns `true` for elements that have the properties of the given
  5165. * object, else `false`.
  5166. *
  5167. * @static
  5168. * @memberOf _
  5169. * @alias unique
  5170. * @category Array
  5171. * @param {Array} array The array to inspect.
  5172. * @param {boolean} [isSorted] Specify the array is sorted.
  5173. * @param {Function|Object|string} [iteratee] The function invoked per iteration.
  5174. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5175. * @returns {Array} Returns the new duplicate-value-free array.
  5176. * @example
  5177. *
  5178. * _.uniq([2, 1, 2]);
  5179. * // => [2, 1]
  5180. *
  5181. * // using `isSorted`
  5182. * _.uniq([1, 1, 2], true);
  5183. * // => [1, 2]
  5184. *
  5185. * // using an iteratee function
  5186. * _.uniq([1, 2.5, 1.5, 2], function(n) {
  5187. * return this.floor(n);
  5188. * }, Math);
  5189. * // => [1, 2.5]
  5190. *
  5191. * // using the `_.property` callback shorthand
  5192. * _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
  5193. * // => [{ 'x': 1 }, { 'x': 2 }]
  5194. */
  5195. function uniq(array, isSorted, iteratee, thisArg) {
  5196. var length = array ? array.length : 0;
  5197. if (!length) {
  5198. return [];
  5199. }
  5200. if (isSorted != null && typeof isSorted != 'boolean') {
  5201. thisArg = iteratee;
  5202. iteratee = isIterateeCall(array, isSorted, thisArg) ? null : isSorted;
  5203. isSorted = false;
  5204. }
  5205. var callback = getCallback();
  5206. if (!(iteratee == null && callback === baseCallback)) {
  5207. iteratee = callback(iteratee, thisArg, 3);
  5208. }
  5209. return (isSorted && getIndexOf() == baseIndexOf)
  5210. ? sortedUniq(array, iteratee)
  5211. : baseUniq(array, iteratee);
  5212. }
  5213. /**
  5214. * This method is like `_.zip` except that it accepts an array of grouped
  5215. * elements and creates an array regrouping the elements to their pre-zip
  5216. * configuration.
  5217. *
  5218. * @static
  5219. * @memberOf _
  5220. * @category Array
  5221. * @param {Array} array The array of grouped elements to process.
  5222. * @returns {Array} Returns the new array of regrouped elements.
  5223. * @example
  5224. *
  5225. * var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
  5226. * // => [['fred', 30, true], ['barney', 40, false]]
  5227. *
  5228. * _.unzip(zipped);
  5229. * // => [['fred', 'barney'], [30, 40], [true, false]]
  5230. */
  5231. function unzip(array) {
  5232. if (!(array && array.length)) {
  5233. return [];
  5234. }
  5235. var index = -1,
  5236. length = 0;
  5237. array = arrayFilter(array, function(group) {
  5238. if (isArrayLike(group)) {
  5239. length = nativeMax(group.length, length);
  5240. return true;
  5241. }
  5242. });
  5243. var result = Array(length);
  5244. while (++index < length) {
  5245. result[index] = arrayMap(array, baseProperty(index));
  5246. }
  5247. return result;
  5248. }
  5249. /**
  5250. * This method is like `_.unzip` except that it accepts an iteratee to specify
  5251. * how regrouped values should be combined. The `iteratee` is bound to `thisArg`
  5252. * and invoked with four arguments: (accumulator, value, index, group).
  5253. *
  5254. * @static
  5255. * @memberOf _
  5256. * @category Array
  5257. * @param {Array} array The array of grouped elements to process.
  5258. * @param {Function} [iteratee] The function to combine regrouped values.
  5259. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5260. * @returns {Array} Returns the new array of regrouped elements.
  5261. * @example
  5262. *
  5263. * var zipped = _.zip([1, 2], [10, 20], [100, 200]);
  5264. * // => [[1, 10, 100], [2, 20, 200]]
  5265. *
  5266. * _.unzipWith(zipped, _.add);
  5267. * // => [3, 30, 300]
  5268. */
  5269. function unzipWith(array, iteratee, thisArg) {
  5270. var length = array ? array.length : 0;
  5271. if (!length) {
  5272. return [];
  5273. }
  5274. var result = unzip(array);
  5275. if (iteratee == null) {
  5276. return result;
  5277. }
  5278. iteratee = bindCallback(iteratee, thisArg, 4);
  5279. return arrayMap(result, function(group) {
  5280. return arrayReduce(group, iteratee, undefined, true);
  5281. });
  5282. }
  5283. /**
  5284. * Creates an array excluding all provided values using
  5285. * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
  5286. * for equality comparisons.
  5287. *
  5288. * @static
  5289. * @memberOf _
  5290. * @category Array
  5291. * @param {Array} array The array to filter.
  5292. * @param {...*} [values] The values to exclude.
  5293. * @returns {Array} Returns the new array of filtered values.
  5294. * @example
  5295. *
  5296. * _.without([1, 2, 1, 3], 1, 2);
  5297. * // => [3]
  5298. */
  5299. var without = restParam(function(array, values) {
  5300. return isArrayLike(array)
  5301. ? baseDifference(array, values)
  5302. : [];
  5303. });
  5304. /**
  5305. * Creates an array of unique values that is the [symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference)
  5306. * of the provided arrays.
  5307. *
  5308. * @static
  5309. * @memberOf _
  5310. * @category Array
  5311. * @param {...Array} [arrays] The arrays to inspect.
  5312. * @returns {Array} Returns the new array of values.
  5313. * @example
  5314. *
  5315. * _.xor([1, 2], [4, 2]);
  5316. * // => [1, 4]
  5317. */
  5318. function xor() {
  5319. var index = -1,
  5320. length = arguments.length;
  5321. while (++index < length) {
  5322. var array = arguments[index];
  5323. if (isArrayLike(array)) {
  5324. var result = result
  5325. ? baseDifference(result, array).concat(baseDifference(array, result))
  5326. : array;
  5327. }
  5328. }
  5329. return result ? baseUniq(result) : [];
  5330. }
  5331. /**
  5332. * Creates an array of grouped elements, the first of which contains the first
  5333. * elements of the given arrays, the second of which contains the second elements
  5334. * of the given arrays, and so on.
  5335. *
  5336. * @static
  5337. * @memberOf _
  5338. * @category Array
  5339. * @param {...Array} [arrays] The arrays to process.
  5340. * @returns {Array} Returns the new array of grouped elements.
  5341. * @example
  5342. *
  5343. * _.zip(['fred', 'barney'], [30, 40], [true, false]);
  5344. * // => [['fred', 30, true], ['barney', 40, false]]
  5345. */
  5346. var zip = restParam(unzip);
  5347. /**
  5348. * The inverse of `_.pairs`; this method returns an object composed from arrays
  5349. * of property names and values. Provide either a single two dimensional array,
  5350. * e.g. `[[key1, value1], [key2, value2]]` or two arrays, one of property names
  5351. * and one of corresponding values.
  5352. *
  5353. * @static
  5354. * @memberOf _
  5355. * @alias object
  5356. * @category Array
  5357. * @param {Array} props The property names.
  5358. * @param {Array} [values=[]] The property values.
  5359. * @returns {Object} Returns the new object.
  5360. * @example
  5361. *
  5362. * _.zipObject([['fred', 30], ['barney', 40]]);
  5363. * // => { 'fred': 30, 'barney': 40 }
  5364. *
  5365. * _.zipObject(['fred', 'barney'], [30, 40]);
  5366. * // => { 'fred': 30, 'barney': 40 }
  5367. */
  5368. function zipObject(props, values) {
  5369. var index = -1,
  5370. length = props ? props.length : 0,
  5371. result = {};
  5372. if (length && !values && !isArray(props[0])) {
  5373. values = [];
  5374. }
  5375. while (++index < length) {
  5376. var key = props[index];
  5377. if (values) {
  5378. result[key] = values[index];
  5379. } else if (key) {
  5380. result[key[0]] = key[1];
  5381. }
  5382. }
  5383. return result;
  5384. }
  5385. /**
  5386. * This method is like `_.zip` except that it accepts an iteratee to specify
  5387. * how grouped values should be combined. The `iteratee` is bound to `thisArg`
  5388. * and invoked with four arguments: (accumulator, value, index, group).
  5389. *
  5390. * @static
  5391. * @memberOf _
  5392. * @category Array
  5393. * @param {...Array} [arrays] The arrays to process.
  5394. * @param {Function} [iteratee] The function to combine grouped values.
  5395. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5396. * @returns {Array} Returns the new array of grouped elements.
  5397. * @example
  5398. *
  5399. * _.zipWith([1, 2], [10, 20], [100, 200], _.add);
  5400. * // => [111, 222]
  5401. */
  5402. var zipWith = restParam(function(arrays) {
  5403. var length = arrays.length,
  5404. iteratee = length > 2 ? arrays[length - 2] : undefined,
  5405. thisArg = length > 1 ? arrays[length - 1] : undefined;
  5406. if (length > 2 && typeof iteratee == 'function') {
  5407. length -= 2;
  5408. } else {
  5409. iteratee = (length > 1 && typeof thisArg == 'function') ? (--length, thisArg) : undefined;
  5410. thisArg = undefined;
  5411. }
  5412. arrays.length = length;
  5413. return unzipWith(arrays, iteratee, thisArg);
  5414. });
  5415. /*------------------------------------------------------------------------*/
  5416. /**
  5417. * Creates a `lodash` object that wraps `value` with explicit method
  5418. * chaining enabled.
  5419. *
  5420. * @static
  5421. * @memberOf _
  5422. * @category Chain
  5423. * @param {*} value The value to wrap.
  5424. * @returns {Object} Returns the new `lodash` wrapper instance.
  5425. * @example
  5426. *
  5427. * var users = [
  5428. * { 'user': 'barney', 'age': 36 },
  5429. * { 'user': 'fred', 'age': 40 },
  5430. * { 'user': 'pebbles', 'age': 1 }
  5431. * ];
  5432. *
  5433. * var youngest = _.chain(users)
  5434. * .sortBy('age')
  5435. * .map(function(chr) {
  5436. * return chr.user + ' is ' + chr.age;
  5437. * })
  5438. * .first()
  5439. * .value();
  5440. * // => 'pebbles is 1'
  5441. */
  5442. function chain(value) {
  5443. var result = lodash(value);
  5444. result.__chain__ = true;
  5445. return result;
  5446. }
  5447. /**
  5448. * This method invokes `interceptor` and returns `value`. The interceptor is
  5449. * bound to `thisArg` and invoked with one argument; (value). The purpose of
  5450. * this method is to "tap into" a method chain in order to perform operations
  5451. * on intermediate results within the chain.
  5452. *
  5453. * @static
  5454. * @memberOf _
  5455. * @category Chain
  5456. * @param {*} value The value to provide to `interceptor`.
  5457. * @param {Function} interceptor The function to invoke.
  5458. * @param {*} [thisArg] The `this` binding of `interceptor`.
  5459. * @returns {*} Returns `value`.
  5460. * @example
  5461. *
  5462. * _([1, 2, 3])
  5463. * .tap(function(array) {
  5464. * array.pop();
  5465. * })
  5466. * .reverse()
  5467. * .value();
  5468. * // => [2, 1]
  5469. */
  5470. function tap(value, interceptor, thisArg) {
  5471. interceptor.call(thisArg, value);
  5472. return value;
  5473. }
  5474. /**
  5475. * This method is like `_.tap` except that it returns the result of `interceptor`.
  5476. *
  5477. * @static
  5478. * @memberOf _
  5479. * @category Chain
  5480. * @param {*} value The value to provide to `interceptor`.
  5481. * @param {Function} interceptor The function to invoke.
  5482. * @param {*} [thisArg] The `this` binding of `interceptor`.
  5483. * @returns {*} Returns the result of `interceptor`.
  5484. * @example
  5485. *
  5486. * _(' abc ')
  5487. * .chain()
  5488. * .trim()
  5489. * .thru(function(value) {
  5490. * return [value];
  5491. * })
  5492. * .value();
  5493. * // => ['abc']
  5494. */
  5495. function thru(value, interceptor, thisArg) {
  5496. return interceptor.call(thisArg, value);
  5497. }
  5498. /**
  5499. * Enables explicit method chaining on the wrapper object.
  5500. *
  5501. * @name chain
  5502. * @memberOf _
  5503. * @category Chain
  5504. * @returns {Object} Returns the new `lodash` wrapper instance.
  5505. * @example
  5506. *
  5507. * var users = [
  5508. * { 'user': 'barney', 'age': 36 },
  5509. * { 'user': 'fred', 'age': 40 }
  5510. * ];
  5511. *
  5512. * // without explicit chaining
  5513. * _(users).first();
  5514. * // => { 'user': 'barney', 'age': 36 }
  5515. *
  5516. * // with explicit chaining
  5517. * _(users).chain()
  5518. * .first()
  5519. * .pick('user')
  5520. * .value();
  5521. * // => { 'user': 'barney' }
  5522. */
  5523. function wrapperChain() {
  5524. return chain(this);
  5525. }
  5526. /**
  5527. * Executes the chained sequence and returns the wrapped result.
  5528. *
  5529. * @name commit
  5530. * @memberOf _
  5531. * @category Chain
  5532. * @returns {Object} Returns the new `lodash` wrapper instance.
  5533. * @example
  5534. *
  5535. * var array = [1, 2];
  5536. * var wrapper = _(array).push(3);
  5537. *
  5538. * console.log(array);
  5539. * // => [1, 2]
  5540. *
  5541. * wrapper = wrapper.commit();
  5542. * console.log(array);
  5543. * // => [1, 2, 3]
  5544. *
  5545. * wrapper.last();
  5546. * // => 3
  5547. *
  5548. * console.log(array);
  5549. * // => [1, 2, 3]
  5550. */
  5551. function wrapperCommit() {
  5552. return new LodashWrapper(this.value(), this.__chain__);
  5553. }
  5554. /**
  5555. * Creates a clone of the chained sequence planting `value` as the wrapped value.
  5556. *
  5557. * @name plant
  5558. * @memberOf _
  5559. * @category Chain
  5560. * @returns {Object} Returns the new `lodash` wrapper instance.
  5561. * @example
  5562. *
  5563. * var array = [1, 2];
  5564. * var wrapper = _(array).map(function(value) {
  5565. * return Math.pow(value, 2);
  5566. * });
  5567. *
  5568. * var other = [3, 4];
  5569. * var otherWrapper = wrapper.plant(other);
  5570. *
  5571. * otherWrapper.value();
  5572. * // => [9, 16]
  5573. *
  5574. * wrapper.value();
  5575. * // => [1, 4]
  5576. */
  5577. function wrapperPlant(value) {
  5578. var result,
  5579. parent = this;
  5580. while (parent instanceof baseLodash) {
  5581. var clone = wrapperClone(parent);
  5582. if (result) {
  5583. previous.__wrapped__ = clone;
  5584. } else {
  5585. result = clone;
  5586. }
  5587. var previous = clone;
  5588. parent = parent.__wrapped__;
  5589. }
  5590. previous.__wrapped__ = value;
  5591. return result;
  5592. }
  5593. /**
  5594. * Reverses the wrapped array so the first element becomes the last, the
  5595. * second element becomes the second to last, and so on.
  5596. *
  5597. * **Note:** This method mutates the wrapped array.
  5598. *
  5599. * @name reverse
  5600. * @memberOf _
  5601. * @category Chain
  5602. * @returns {Object} Returns the new reversed `lodash` wrapper instance.
  5603. * @example
  5604. *
  5605. * var array = [1, 2, 3];
  5606. *
  5607. * _(array).reverse().value()
  5608. * // => [3, 2, 1]
  5609. *
  5610. * console.log(array);
  5611. * // => [3, 2, 1]
  5612. */
  5613. function wrapperReverse() {
  5614. var value = this.__wrapped__;
  5615. if (value instanceof LazyWrapper) {
  5616. if (this.__actions__.length) {
  5617. value = new LazyWrapper(this);
  5618. }
  5619. return new LodashWrapper(value.reverse(), this.__chain__);
  5620. }
  5621. return this.thru(function(value) {
  5622. return value.reverse();
  5623. });
  5624. }
  5625. /**
  5626. * Produces the result of coercing the unwrapped value to a string.
  5627. *
  5628. * @name toString
  5629. * @memberOf _
  5630. * @category Chain
  5631. * @returns {string} Returns the coerced string value.
  5632. * @example
  5633. *
  5634. * _([1, 2, 3]).toString();
  5635. * // => '1,2,3'
  5636. */
  5637. function wrapperToString() {
  5638. return (this.value() + '');
  5639. }
  5640. /**
  5641. * Executes the chained sequence to extract the unwrapped value.
  5642. *
  5643. * @name value
  5644. * @memberOf _
  5645. * @alias run, toJSON, valueOf
  5646. * @category Chain
  5647. * @returns {*} Returns the resolved unwrapped value.
  5648. * @example
  5649. *
  5650. * _([1, 2, 3]).value();
  5651. * // => [1, 2, 3]
  5652. */
  5653. function wrapperValue() {
  5654. return baseWrapperValue(this.__wrapped__, this.__actions__);
  5655. }
  5656. /*------------------------------------------------------------------------*/
  5657. /**
  5658. * Creates an array of elements corresponding to the given keys, or indexes,
  5659. * of `collection`. Keys may be specified as individual arguments or as arrays
  5660. * of keys.
  5661. *
  5662. * @static
  5663. * @memberOf _
  5664. * @category Collection
  5665. * @param {Array|Object|string} collection The collection to iterate over.
  5666. * @param {...(number|number[]|string|string[])} [props] The property names
  5667. * or indexes of elements to pick, specified individually or in arrays.
  5668. * @returns {Array} Returns the new array of picked elements.
  5669. * @example
  5670. *
  5671. * _.at(['a', 'b', 'c'], [0, 2]);
  5672. * // => ['a', 'c']
  5673. *
  5674. * _.at(['barney', 'fred', 'pebbles'], 0, 2);
  5675. * // => ['barney', 'pebbles']
  5676. */
  5677. var at = restParam(function(collection, props) {
  5678. return baseAt(collection, baseFlatten(props));
  5679. });
  5680. /**
  5681. * Creates an object composed of keys generated from the results of running
  5682. * each element of `collection` through `iteratee`. The corresponding value
  5683. * of each key is the number of times the key was returned by `iteratee`.
  5684. * The `iteratee` is bound to `thisArg` and invoked with three arguments:
  5685. * (value, index|key, collection).
  5686. *
  5687. * If a property name is provided for `iteratee` the created `_.property`
  5688. * style callback returns the property value of the given element.
  5689. *
  5690. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5691. * style callback returns `true` for elements that have a matching property
  5692. * value, else `false`.
  5693. *
  5694. * If an object is provided for `iteratee` the created `_.matches` style
  5695. * callback returns `true` for elements that have the properties of the given
  5696. * object, else `false`.
  5697. *
  5698. * @static
  5699. * @memberOf _
  5700. * @category Collection
  5701. * @param {Array|Object|string} collection The collection to iterate over.
  5702. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  5703. * per iteration.
  5704. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5705. * @returns {Object} Returns the composed aggregate object.
  5706. * @example
  5707. *
  5708. * _.countBy([4.3, 6.1, 6.4], function(n) {
  5709. * return Math.floor(n);
  5710. * });
  5711. * // => { '4': 1, '6': 2 }
  5712. *
  5713. * _.countBy([4.3, 6.1, 6.4], function(n) {
  5714. * return this.floor(n);
  5715. * }, Math);
  5716. * // => { '4': 1, '6': 2 }
  5717. *
  5718. * _.countBy(['one', 'two', 'three'], 'length');
  5719. * // => { '3': 2, '5': 1 }
  5720. */
  5721. var countBy = createAggregator(function(result, value, key) {
  5722. hasOwnProperty.call(result, key) ? ++result[key] : (result[key] = 1);
  5723. });
  5724. /**
  5725. * Checks if `predicate` returns truthy for **all** elements of `collection`.
  5726. * The predicate is bound to `thisArg` and invoked with three arguments:
  5727. * (value, index|key, collection).
  5728. *
  5729. * If a property name is provided for `predicate` the created `_.property`
  5730. * style callback returns the property value of the given element.
  5731. *
  5732. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5733. * style callback returns `true` for elements that have a matching property
  5734. * value, else `false`.
  5735. *
  5736. * If an object is provided for `predicate` the created `_.matches` style
  5737. * callback returns `true` for elements that have the properties of the given
  5738. * object, else `false`.
  5739. *
  5740. * @static
  5741. * @memberOf _
  5742. * @alias all
  5743. * @category Collection
  5744. * @param {Array|Object|string} collection The collection to iterate over.
  5745. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5746. * per iteration.
  5747. * @param {*} [thisArg] The `this` binding of `predicate`.
  5748. * @returns {boolean} Returns `true` if all elements pass the predicate check,
  5749. * else `false`.
  5750. * @example
  5751. *
  5752. * _.every([true, 1, null, 'yes'], Boolean);
  5753. * // => false
  5754. *
  5755. * var users = [
  5756. * { 'user': 'barney', 'active': false },
  5757. * { 'user': 'fred', 'active': false }
  5758. * ];
  5759. *
  5760. * // using the `_.matches` callback shorthand
  5761. * _.every(users, { 'user': 'barney', 'active': false });
  5762. * // => false
  5763. *
  5764. * // using the `_.matchesProperty` callback shorthand
  5765. * _.every(users, 'active', false);
  5766. * // => true
  5767. *
  5768. * // using the `_.property` callback shorthand
  5769. * _.every(users, 'active');
  5770. * // => false
  5771. */
  5772. function every(collection, predicate, thisArg) {
  5773. var func = isArray(collection) ? arrayEvery : baseEvery;
  5774. if (thisArg && isIterateeCall(collection, predicate, thisArg)) {
  5775. predicate = null;
  5776. }
  5777. if (typeof predicate != 'function' || thisArg !== undefined) {
  5778. predicate = getCallback(predicate, thisArg, 3);
  5779. }
  5780. return func(collection, predicate);
  5781. }
  5782. /**
  5783. * Iterates over elements of `collection`, returning an array of all elements
  5784. * `predicate` returns truthy for. The predicate is bound to `thisArg` and
  5785. * invoked with three arguments: (value, index|key, collection).
  5786. *
  5787. * If a property name is provided for `predicate` the created `_.property`
  5788. * style callback returns the property value of the given element.
  5789. *
  5790. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5791. * style callback returns `true` for elements that have a matching property
  5792. * value, else `false`.
  5793. *
  5794. * If an object is provided for `predicate` the created `_.matches` style
  5795. * callback returns `true` for elements that have the properties of the given
  5796. * object, else `false`.
  5797. *
  5798. * @static
  5799. * @memberOf _
  5800. * @alias select
  5801. * @category Collection
  5802. * @param {Array|Object|string} collection The collection to iterate over.
  5803. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5804. * per iteration.
  5805. * @param {*} [thisArg] The `this` binding of `predicate`.
  5806. * @returns {Array} Returns the new filtered array.
  5807. * @example
  5808. *
  5809. * _.filter([4, 5, 6], function(n) {
  5810. * return n % 2 == 0;
  5811. * });
  5812. * // => [4, 6]
  5813. *
  5814. * var users = [
  5815. * { 'user': 'barney', 'age': 36, 'active': true },
  5816. * { 'user': 'fred', 'age': 40, 'active': false }
  5817. * ];
  5818. *
  5819. * // using the `_.matches` callback shorthand
  5820. * _.pluck(_.filter(users, { 'age': 36, 'active': true }), 'user');
  5821. * // => ['barney']
  5822. *
  5823. * // using the `_.matchesProperty` callback shorthand
  5824. * _.pluck(_.filter(users, 'active', false), 'user');
  5825. * // => ['fred']
  5826. *
  5827. * // using the `_.property` callback shorthand
  5828. * _.pluck(_.filter(users, 'active'), 'user');
  5829. * // => ['barney']
  5830. */
  5831. function filter(collection, predicate, thisArg) {
  5832. var func = isArray(collection) ? arrayFilter : baseFilter;
  5833. predicate = getCallback(predicate, thisArg, 3);
  5834. return func(collection, predicate);
  5835. }
  5836. /**
  5837. * Iterates over elements of `collection`, returning the first element
  5838. * `predicate` returns truthy for. The predicate is bound to `thisArg` and
  5839. * invoked with three arguments: (value, index|key, collection).
  5840. *
  5841. * If a property name is provided for `predicate` the created `_.property`
  5842. * style callback returns the property value of the given element.
  5843. *
  5844. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  5845. * style callback returns `true` for elements that have a matching property
  5846. * value, else `false`.
  5847. *
  5848. * If an object is provided for `predicate` the created `_.matches` style
  5849. * callback returns `true` for elements that have the properties of the given
  5850. * object, else `false`.
  5851. *
  5852. * @static
  5853. * @memberOf _
  5854. * @alias detect
  5855. * @category Collection
  5856. * @param {Array|Object|string} collection The collection to search.
  5857. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5858. * per iteration.
  5859. * @param {*} [thisArg] The `this` binding of `predicate`.
  5860. * @returns {*} Returns the matched element, else `undefined`.
  5861. * @example
  5862. *
  5863. * var users = [
  5864. * { 'user': 'barney', 'age': 36, 'active': true },
  5865. * { 'user': 'fred', 'age': 40, 'active': false },
  5866. * { 'user': 'pebbles', 'age': 1, 'active': true }
  5867. * ];
  5868. *
  5869. * _.result(_.find(users, function(chr) {
  5870. * return chr.age < 40;
  5871. * }), 'user');
  5872. * // => 'barney'
  5873. *
  5874. * // using the `_.matches` callback shorthand
  5875. * _.result(_.find(users, { 'age': 1, 'active': true }), 'user');
  5876. * // => 'pebbles'
  5877. *
  5878. * // using the `_.matchesProperty` callback shorthand
  5879. * _.result(_.find(users, 'active', false), 'user');
  5880. * // => 'fred'
  5881. *
  5882. * // using the `_.property` callback shorthand
  5883. * _.result(_.find(users, 'active'), 'user');
  5884. * // => 'barney'
  5885. */
  5886. var find = createFind(baseEach);
  5887. /**
  5888. * This method is like `_.find` except that it iterates over elements of
  5889. * `collection` from right to left.
  5890. *
  5891. * @static
  5892. * @memberOf _
  5893. * @category Collection
  5894. * @param {Array|Object|string} collection The collection to search.
  5895. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  5896. * per iteration.
  5897. * @param {*} [thisArg] The `this` binding of `predicate`.
  5898. * @returns {*} Returns the matched element, else `undefined`.
  5899. * @example
  5900. *
  5901. * _.findLast([1, 2, 3, 4], function(n) {
  5902. * return n % 2 == 1;
  5903. * });
  5904. * // => 3
  5905. */
  5906. var findLast = createFind(baseEachRight, true);
  5907. /**
  5908. * Performs a deep comparison between each element in `collection` and the
  5909. * source object, returning the first element that has equivalent property
  5910. * values.
  5911. *
  5912. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  5913. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  5914. * their own, not inherited, enumerable properties. For comparing a single
  5915. * own or inherited property value see `_.matchesProperty`.
  5916. *
  5917. * @static
  5918. * @memberOf _
  5919. * @category Collection
  5920. * @param {Array|Object|string} collection The collection to search.
  5921. * @param {Object} source The object of property values to match.
  5922. * @returns {*} Returns the matched element, else `undefined`.
  5923. * @example
  5924. *
  5925. * var users = [
  5926. * { 'user': 'barney', 'age': 36, 'active': true },
  5927. * { 'user': 'fred', 'age': 40, 'active': false }
  5928. * ];
  5929. *
  5930. * _.result(_.findWhere(users, { 'age': 36, 'active': true }), 'user');
  5931. * // => 'barney'
  5932. *
  5933. * _.result(_.findWhere(users, { 'age': 40, 'active': false }), 'user');
  5934. * // => 'fred'
  5935. */
  5936. function findWhere(collection, source) {
  5937. return find(collection, baseMatches(source));
  5938. }
  5939. /**
  5940. * Iterates over elements of `collection` invoking `iteratee` for each element.
  5941. * The `iteratee` is bound to `thisArg` and invoked with three arguments:
  5942. * (value, index|key, collection). Iteratee functions may exit iteration early
  5943. * by explicitly returning `false`.
  5944. *
  5945. * **Note:** As with other "Collections" methods, objects with a "length" property
  5946. * are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn`
  5947. * may be used for object iteration.
  5948. *
  5949. * @static
  5950. * @memberOf _
  5951. * @alias each
  5952. * @category Collection
  5953. * @param {Array|Object|string} collection The collection to iterate over.
  5954. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  5955. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5956. * @returns {Array|Object|string} Returns `collection`.
  5957. * @example
  5958. *
  5959. * _([1, 2]).forEach(function(n) {
  5960. * console.log(n);
  5961. * }).value();
  5962. * // => logs each value from left to right and returns the array
  5963. *
  5964. * _.forEach({ 'a': 1, 'b': 2 }, function(n, key) {
  5965. * console.log(n, key);
  5966. * });
  5967. * // => logs each value-key pair and returns the object (iteration order is not guaranteed)
  5968. */
  5969. var forEach = createForEach(arrayEach, baseEach);
  5970. /**
  5971. * This method is like `_.forEach` except that it iterates over elements of
  5972. * `collection` from right to left.
  5973. *
  5974. * @static
  5975. * @memberOf _
  5976. * @alias eachRight
  5977. * @category Collection
  5978. * @param {Array|Object|string} collection The collection to iterate over.
  5979. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  5980. * @param {*} [thisArg] The `this` binding of `iteratee`.
  5981. * @returns {Array|Object|string} Returns `collection`.
  5982. * @example
  5983. *
  5984. * _([1, 2]).forEachRight(function(n) {
  5985. * console.log(n);
  5986. * }).value();
  5987. * // => logs each value from right to left and returns the array
  5988. */
  5989. var forEachRight = createForEach(arrayEachRight, baseEachRight);
  5990. /**
  5991. * Creates an object composed of keys generated from the results of running
  5992. * each element of `collection` through `iteratee`. The corresponding value
  5993. * of each key is an array of the elements responsible for generating the key.
  5994. * The `iteratee` is bound to `thisArg` and invoked with three arguments:
  5995. * (value, index|key, collection).
  5996. *
  5997. * If a property name is provided for `iteratee` the created `_.property`
  5998. * style callback returns the property value of the given element.
  5999. *
  6000. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6001. * style callback returns `true` for elements that have a matching property
  6002. * value, else `false`.
  6003. *
  6004. * If an object is provided for `iteratee` the created `_.matches` style
  6005. * callback returns `true` for elements that have the properties of the given
  6006. * object, else `false`.
  6007. *
  6008. * @static
  6009. * @memberOf _
  6010. * @category Collection
  6011. * @param {Array|Object|string} collection The collection to iterate over.
  6012. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  6013. * per iteration.
  6014. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6015. * @returns {Object} Returns the composed aggregate object.
  6016. * @example
  6017. *
  6018. * _.groupBy([4.2, 6.1, 6.4], function(n) {
  6019. * return Math.floor(n);
  6020. * });
  6021. * // => { '4': [4.2], '6': [6.1, 6.4] }
  6022. *
  6023. * _.groupBy([4.2, 6.1, 6.4], function(n) {
  6024. * return this.floor(n);
  6025. * }, Math);
  6026. * // => { '4': [4.2], '6': [6.1, 6.4] }
  6027. *
  6028. * // using the `_.property` callback shorthand
  6029. * _.groupBy(['one', 'two', 'three'], 'length');
  6030. * // => { '3': ['one', 'two'], '5': ['three'] }
  6031. */
  6032. var groupBy = createAggregator(function(result, value, key) {
  6033. if (hasOwnProperty.call(result, key)) {
  6034. result[key].push(value);
  6035. } else {
  6036. result[key] = [value];
  6037. }
  6038. });
  6039. /**
  6040. * Checks if `value` is in `collection` using
  6041. * [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
  6042. * for equality comparisons. If `fromIndex` is negative, it is used as the offset
  6043. * from the end of `collection`.
  6044. *
  6045. * @static
  6046. * @memberOf _
  6047. * @alias contains, include
  6048. * @category Collection
  6049. * @param {Array|Object|string} collection The collection to search.
  6050. * @param {*} target The value to search for.
  6051. * @param {number} [fromIndex=0] The index to search from.
  6052. * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
  6053. * @returns {boolean} Returns `true` if a matching element is found, else `false`.
  6054. * @example
  6055. *
  6056. * _.includes([1, 2, 3], 1);
  6057. * // => true
  6058. *
  6059. * _.includes([1, 2, 3], 1, 2);
  6060. * // => false
  6061. *
  6062. * _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
  6063. * // => true
  6064. *
  6065. * _.includes('pebbles', 'eb');
  6066. * // => true
  6067. */
  6068. function includes(collection, target, fromIndex, guard) {
  6069. var length = collection ? getLength(collection) : 0;
  6070. if (!isLength(length)) {
  6071. collection = values(collection);
  6072. length = collection.length;
  6073. }
  6074. if (!length) {
  6075. return false;
  6076. }
  6077. if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
  6078. fromIndex = 0;
  6079. } else {
  6080. fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
  6081. }
  6082. return (typeof collection == 'string' || !isArray(collection) && isString(collection))
  6083. ? (fromIndex < length && collection.indexOf(target, fromIndex) > -1)
  6084. : (getIndexOf(collection, target, fromIndex) > -1);
  6085. }
  6086. /**
  6087. * Creates an object composed of keys generated from the results of running
  6088. * each element of `collection` through `iteratee`. The corresponding value
  6089. * of each key is the last element responsible for generating the key. The
  6090. * iteratee function is bound to `thisArg` and invoked with three arguments:
  6091. * (value, index|key, collection).
  6092. *
  6093. * If a property name is provided for `iteratee` the created `_.property`
  6094. * style callback returns the property value of the given element.
  6095. *
  6096. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6097. * style callback returns `true` for elements that have a matching property
  6098. * value, else `false`.
  6099. *
  6100. * If an object is provided for `iteratee` the created `_.matches` style
  6101. * callback returns `true` for elements that have the properties of the given
  6102. * object, else `false`.
  6103. *
  6104. * @static
  6105. * @memberOf _
  6106. * @category Collection
  6107. * @param {Array|Object|string} collection The collection to iterate over.
  6108. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  6109. * per iteration.
  6110. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6111. * @returns {Object} Returns the composed aggregate object.
  6112. * @example
  6113. *
  6114. * var keyData = [
  6115. * { 'dir': 'left', 'code': 97 },
  6116. * { 'dir': 'right', 'code': 100 }
  6117. * ];
  6118. *
  6119. * _.indexBy(keyData, 'dir');
  6120. * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
  6121. *
  6122. * _.indexBy(keyData, function(object) {
  6123. * return String.fromCharCode(object.code);
  6124. * });
  6125. * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  6126. *
  6127. * _.indexBy(keyData, function(object) {
  6128. * return this.fromCharCode(object.code);
  6129. * }, String);
  6130. * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  6131. */
  6132. var indexBy = createAggregator(function(result, value, key) {
  6133. result[key] = value;
  6134. });
  6135. /**
  6136. * Invokes the method at `path` of each element in `collection`, returning
  6137. * an array of the results of each invoked method. Any additional arguments
  6138. * are provided to each invoked method. If `methodName` is a function it is
  6139. * invoked for, and `this` bound to, each element in `collection`.
  6140. *
  6141. * @static
  6142. * @memberOf _
  6143. * @category Collection
  6144. * @param {Array|Object|string} collection The collection to iterate over.
  6145. * @param {Array|Function|string} path The path of the method to invoke or
  6146. * the function invoked per iteration.
  6147. * @param {...*} [args] The arguments to invoke the method with.
  6148. * @returns {Array} Returns the array of results.
  6149. * @example
  6150. *
  6151. * _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  6152. * // => [[1, 5, 7], [1, 2, 3]]
  6153. *
  6154. * _.invoke([123, 456], String.prototype.split, '');
  6155. * // => [['1', '2', '3'], ['4', '5', '6']]
  6156. */
  6157. var invoke = restParam(function(collection, path, args) {
  6158. var index = -1,
  6159. isFunc = typeof path == 'function',
  6160. isProp = isKey(path),
  6161. result = isArrayLike(collection) ? Array(collection.length) : [];
  6162. baseEach(collection, function(value) {
  6163. var func = isFunc ? path : ((isProp && value != null) ? value[path] : null);
  6164. result[++index] = func ? func.apply(value, args) : invokePath(value, path, args);
  6165. });
  6166. return result;
  6167. });
  6168. /**
  6169. * Creates an array of values by running each element in `collection` through
  6170. * `iteratee`. The `iteratee` is bound to `thisArg` and invoked with three
  6171. * arguments: (value, index|key, collection).
  6172. *
  6173. * If a property name is provided for `iteratee` the created `_.property`
  6174. * style callback returns the property value of the given element.
  6175. *
  6176. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6177. * style callback returns `true` for elements that have a matching property
  6178. * value, else `false`.
  6179. *
  6180. * If an object is provided for `iteratee` the created `_.matches` style
  6181. * callback returns `true` for elements that have the properties of the given
  6182. * object, else `false`.
  6183. *
  6184. * Many lodash methods are guarded to work as iteratees for methods like
  6185. * `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`.
  6186. *
  6187. * The guarded methods are:
  6188. * `ary`, `callback`, `chunk`, `clone`, `create`, `curry`, `curryRight`,
  6189. * `drop`, `dropRight`, `every`, `fill`, `flatten`, `invert`, `max`, `min`,
  6190. * `parseInt`, `slice`, `sortBy`, `take`, `takeRight`, `template`, `trim`,
  6191. * `trimLeft`, `trimRight`, `trunc`, `random`, `range`, `sample`, `some`,
  6192. * `sum`, `uniq`, and `words`
  6193. *
  6194. * @static
  6195. * @memberOf _
  6196. * @alias collect
  6197. * @category Collection
  6198. * @param {Array|Object|string} collection The collection to iterate over.
  6199. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  6200. * per iteration.
  6201. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6202. * @returns {Array} Returns the new mapped array.
  6203. * @example
  6204. *
  6205. * function timesThree(n) {
  6206. * return n * 3;
  6207. * }
  6208. *
  6209. * _.map([1, 2], timesThree);
  6210. * // => [3, 6]
  6211. *
  6212. * _.map({ 'a': 1, 'b': 2 }, timesThree);
  6213. * // => [3, 6] (iteration order is not guaranteed)
  6214. *
  6215. * var users = [
  6216. * { 'user': 'barney' },
  6217. * { 'user': 'fred' }
  6218. * ];
  6219. *
  6220. * // using the `_.property` callback shorthand
  6221. * _.map(users, 'user');
  6222. * // => ['barney', 'fred']
  6223. */
  6224. function map(collection, iteratee, thisArg) {
  6225. var func = isArray(collection) ? arrayMap : baseMap;
  6226. iteratee = getCallback(iteratee, thisArg, 3);
  6227. return func(collection, iteratee);
  6228. }
  6229. /**
  6230. * Creates an array of elements split into two groups, the first of which
  6231. * contains elements `predicate` returns truthy for, while the second of which
  6232. * contains elements `predicate` returns falsey for. The predicate is bound
  6233. * to `thisArg` and invoked with three arguments: (value, index|key, collection).
  6234. *
  6235. * If a property name is provided for `predicate` the created `_.property`
  6236. * style callback returns the property value of the given element.
  6237. *
  6238. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6239. * style callback returns `true` for elements that have a matching property
  6240. * value, else `false`.
  6241. *
  6242. * If an object is provided for `predicate` the created `_.matches` style
  6243. * callback returns `true` for elements that have the properties of the given
  6244. * object, else `false`.
  6245. *
  6246. * @static
  6247. * @memberOf _
  6248. * @category Collection
  6249. * @param {Array|Object|string} collection The collection to iterate over.
  6250. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  6251. * per iteration.
  6252. * @param {*} [thisArg] The `this` binding of `predicate`.
  6253. * @returns {Array} Returns the array of grouped elements.
  6254. * @example
  6255. *
  6256. * _.partition([1, 2, 3], function(n) {
  6257. * return n % 2;
  6258. * });
  6259. * // => [[1, 3], [2]]
  6260. *
  6261. * _.partition([1.2, 2.3, 3.4], function(n) {
  6262. * return this.floor(n) % 2;
  6263. * }, Math);
  6264. * // => [[1.2, 3.4], [2.3]]
  6265. *
  6266. * var users = [
  6267. * { 'user': 'barney', 'age': 36, 'active': false },
  6268. * { 'user': 'fred', 'age': 40, 'active': true },
  6269. * { 'user': 'pebbles', 'age': 1, 'active': false }
  6270. * ];
  6271. *
  6272. * var mapper = function(array) {
  6273. * return _.pluck(array, 'user');
  6274. * };
  6275. *
  6276. * // using the `_.matches` callback shorthand
  6277. * _.map(_.partition(users, { 'age': 1, 'active': false }), mapper);
  6278. * // => [['pebbles'], ['barney', 'fred']]
  6279. *
  6280. * // using the `_.matchesProperty` callback shorthand
  6281. * _.map(_.partition(users, 'active', false), mapper);
  6282. * // => [['barney', 'pebbles'], ['fred']]
  6283. *
  6284. * // using the `_.property` callback shorthand
  6285. * _.map(_.partition(users, 'active'), mapper);
  6286. * // => [['fred'], ['barney', 'pebbles']]
  6287. */
  6288. var partition = createAggregator(function(result, value, key) {
  6289. result[key ? 0 : 1].push(value);
  6290. }, function() { return [[], []]; });
  6291. /**
  6292. * Gets the property value of `path` from all elements in `collection`.
  6293. *
  6294. * @static
  6295. * @memberOf _
  6296. * @category Collection
  6297. * @param {Array|Object|string} collection The collection to iterate over.
  6298. * @param {Array|string} path The path of the property to pluck.
  6299. * @returns {Array} Returns the property values.
  6300. * @example
  6301. *
  6302. * var users = [
  6303. * { 'user': 'barney', 'age': 36 },
  6304. * { 'user': 'fred', 'age': 40 }
  6305. * ];
  6306. *
  6307. * _.pluck(users, 'user');
  6308. * // => ['barney', 'fred']
  6309. *
  6310. * var userIndex = _.indexBy(users, 'user');
  6311. * _.pluck(userIndex, 'age');
  6312. * // => [36, 40] (iteration order is not guaranteed)
  6313. */
  6314. function pluck(collection, path) {
  6315. return map(collection, property(path));
  6316. }
  6317. /**
  6318. * Reduces `collection` to a value which is the accumulated result of running
  6319. * each element in `collection` through `iteratee`, where each successive
  6320. * invocation is supplied the return value of the previous. If `accumulator`
  6321. * is not provided the first element of `collection` is used as the initial
  6322. * value. The `iteratee` is bound to `thisArg` and invoked with four arguments:
  6323. * (accumulator, value, index|key, collection).
  6324. *
  6325. * Many lodash methods are guarded to work as iteratees for methods like
  6326. * `_.reduce`, `_.reduceRight`, and `_.transform`.
  6327. *
  6328. * The guarded methods are:
  6329. * `assign`, `defaults`, `includes`, `merge`, `sortByAll`, and `sortByOrder`
  6330. *
  6331. * @static
  6332. * @memberOf _
  6333. * @alias foldl, inject
  6334. * @category Collection
  6335. * @param {Array|Object|string} collection The collection to iterate over.
  6336. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  6337. * @param {*} [accumulator] The initial value.
  6338. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6339. * @returns {*} Returns the accumulated value.
  6340. * @example
  6341. *
  6342. * _.reduce([1, 2], function(total, n) {
  6343. * return total + n;
  6344. * });
  6345. * // => 3
  6346. *
  6347. * _.reduce({ 'a': 1, 'b': 2 }, function(result, n, key) {
  6348. * result[key] = n * 3;
  6349. * return result;
  6350. * }, {});
  6351. * // => { 'a': 3, 'b': 6 } (iteration order is not guaranteed)
  6352. */
  6353. var reduce = createReduce(arrayReduce, baseEach);
  6354. /**
  6355. * This method is like `_.reduce` except that it iterates over elements of
  6356. * `collection` from right to left.
  6357. *
  6358. * @static
  6359. * @memberOf _
  6360. * @alias foldr
  6361. * @category Collection
  6362. * @param {Array|Object|string} collection The collection to iterate over.
  6363. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  6364. * @param {*} [accumulator] The initial value.
  6365. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6366. * @returns {*} Returns the accumulated value.
  6367. * @example
  6368. *
  6369. * var array = [[0, 1], [2, 3], [4, 5]];
  6370. *
  6371. * _.reduceRight(array, function(flattened, other) {
  6372. * return flattened.concat(other);
  6373. * }, []);
  6374. * // => [4, 5, 2, 3, 0, 1]
  6375. */
  6376. var reduceRight = createReduce(arrayReduceRight, baseEachRight);
  6377. /**
  6378. * The opposite of `_.filter`; this method returns the elements of `collection`
  6379. * that `predicate` does **not** return truthy for.
  6380. *
  6381. * @static
  6382. * @memberOf _
  6383. * @category Collection
  6384. * @param {Array|Object|string} collection The collection to iterate over.
  6385. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  6386. * per iteration.
  6387. * @param {*} [thisArg] The `this` binding of `predicate`.
  6388. * @returns {Array} Returns the new filtered array.
  6389. * @example
  6390. *
  6391. * _.reject([1, 2, 3, 4], function(n) {
  6392. * return n % 2 == 0;
  6393. * });
  6394. * // => [1, 3]
  6395. *
  6396. * var users = [
  6397. * { 'user': 'barney', 'age': 36, 'active': false },
  6398. * { 'user': 'fred', 'age': 40, 'active': true }
  6399. * ];
  6400. *
  6401. * // using the `_.matches` callback shorthand
  6402. * _.pluck(_.reject(users, { 'age': 40, 'active': true }), 'user');
  6403. * // => ['barney']
  6404. *
  6405. * // using the `_.matchesProperty` callback shorthand
  6406. * _.pluck(_.reject(users, 'active', false), 'user');
  6407. * // => ['fred']
  6408. *
  6409. * // using the `_.property` callback shorthand
  6410. * _.pluck(_.reject(users, 'active'), 'user');
  6411. * // => ['barney']
  6412. */
  6413. function reject(collection, predicate, thisArg) {
  6414. var func = isArray(collection) ? arrayFilter : baseFilter;
  6415. predicate = getCallback(predicate, thisArg, 3);
  6416. return func(collection, function(value, index, collection) {
  6417. return !predicate(value, index, collection);
  6418. });
  6419. }
  6420. /**
  6421. * Gets a random element or `n` random elements from a collection.
  6422. *
  6423. * @static
  6424. * @memberOf _
  6425. * @category Collection
  6426. * @param {Array|Object|string} collection The collection to sample.
  6427. * @param {number} [n] The number of elements to sample.
  6428. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  6429. * @returns {*} Returns the random sample(s).
  6430. * @example
  6431. *
  6432. * _.sample([1, 2, 3, 4]);
  6433. * // => 2
  6434. *
  6435. * _.sample([1, 2, 3, 4], 2);
  6436. * // => [3, 1]
  6437. */
  6438. function sample(collection, n, guard) {
  6439. if (guard ? isIterateeCall(collection, n, guard) : n == null) {
  6440. collection = toIterable(collection);
  6441. var length = collection.length;
  6442. return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
  6443. }
  6444. var index = -1,
  6445. result = toArray(collection),
  6446. length = result.length,
  6447. lastIndex = length - 1;
  6448. n = nativeMin(n < 0 ? 0 : (+n || 0), length);
  6449. while (++index < n) {
  6450. var rand = baseRandom(index, lastIndex),
  6451. value = result[rand];
  6452. result[rand] = result[index];
  6453. result[index] = value;
  6454. }
  6455. result.length = n;
  6456. return result;
  6457. }
  6458. /**
  6459. * Creates an array of shuffled values, using a version of the
  6460. * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
  6461. *
  6462. * @static
  6463. * @memberOf _
  6464. * @category Collection
  6465. * @param {Array|Object|string} collection The collection to shuffle.
  6466. * @returns {Array} Returns the new shuffled array.
  6467. * @example
  6468. *
  6469. * _.shuffle([1, 2, 3, 4]);
  6470. * // => [4, 1, 3, 2]
  6471. */
  6472. function shuffle(collection) {
  6473. return sample(collection, POSITIVE_INFINITY);
  6474. }
  6475. /**
  6476. * Gets the size of `collection` by returning its length for array-like
  6477. * values or the number of own enumerable properties for objects.
  6478. *
  6479. * @static
  6480. * @memberOf _
  6481. * @category Collection
  6482. * @param {Array|Object|string} collection The collection to inspect.
  6483. * @returns {number} Returns the size of `collection`.
  6484. * @example
  6485. *
  6486. * _.size([1, 2, 3]);
  6487. * // => 3
  6488. *
  6489. * _.size({ 'a': 1, 'b': 2 });
  6490. * // => 2
  6491. *
  6492. * _.size('pebbles');
  6493. * // => 7
  6494. */
  6495. function size(collection) {
  6496. var length = collection ? getLength(collection) : 0;
  6497. return isLength(length) ? length : keys(collection).length;
  6498. }
  6499. /**
  6500. * Checks if `predicate` returns truthy for **any** element of `collection`.
  6501. * The function returns as soon as it finds a passing value and does not iterate
  6502. * over the entire collection. The predicate is bound to `thisArg` and invoked
  6503. * with three arguments: (value, index|key, collection).
  6504. *
  6505. * If a property name is provided for `predicate` the created `_.property`
  6506. * style callback returns the property value of the given element.
  6507. *
  6508. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6509. * style callback returns `true` for elements that have a matching property
  6510. * value, else `false`.
  6511. *
  6512. * If an object is provided for `predicate` the created `_.matches` style
  6513. * callback returns `true` for elements that have the properties of the given
  6514. * object, else `false`.
  6515. *
  6516. * @static
  6517. * @memberOf _
  6518. * @alias any
  6519. * @category Collection
  6520. * @param {Array|Object|string} collection The collection to iterate over.
  6521. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  6522. * per iteration.
  6523. * @param {*} [thisArg] The `this` binding of `predicate`.
  6524. * @returns {boolean} Returns `true` if any element passes the predicate check,
  6525. * else `false`.
  6526. * @example
  6527. *
  6528. * _.some([null, 0, 'yes', false], Boolean);
  6529. * // => true
  6530. *
  6531. * var users = [
  6532. * { 'user': 'barney', 'active': true },
  6533. * { 'user': 'fred', 'active': false }
  6534. * ];
  6535. *
  6536. * // using the `_.matches` callback shorthand
  6537. * _.some(users, { 'user': 'barney', 'active': false });
  6538. * // => false
  6539. *
  6540. * // using the `_.matchesProperty` callback shorthand
  6541. * _.some(users, 'active', false);
  6542. * // => true
  6543. *
  6544. * // using the `_.property` callback shorthand
  6545. * _.some(users, 'active');
  6546. * // => true
  6547. */
  6548. function some(collection, predicate, thisArg) {
  6549. var func = isArray(collection) ? arraySome : baseSome;
  6550. if (thisArg && isIterateeCall(collection, predicate, thisArg)) {
  6551. predicate = null;
  6552. }
  6553. if (typeof predicate != 'function' || thisArg !== undefined) {
  6554. predicate = getCallback(predicate, thisArg, 3);
  6555. }
  6556. return func(collection, predicate);
  6557. }
  6558. /**
  6559. * Creates an array of elements, sorted in ascending order by the results of
  6560. * running each element in a collection through `iteratee`. This method performs
  6561. * a stable sort, that is, it preserves the original sort order of equal elements.
  6562. * The `iteratee` is bound to `thisArg` and invoked with three arguments:
  6563. * (value, index|key, collection).
  6564. *
  6565. * If a property name is provided for `iteratee` the created `_.property`
  6566. * style callback returns the property value of the given element.
  6567. *
  6568. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  6569. * style callback returns `true` for elements that have a matching property
  6570. * value, else `false`.
  6571. *
  6572. * If an object is provided for `iteratee` the created `_.matches` style
  6573. * callback returns `true` for elements that have the properties of the given
  6574. * object, else `false`.
  6575. *
  6576. * @static
  6577. * @memberOf _
  6578. * @category Collection
  6579. * @param {Array|Object|string} collection The collection to iterate over.
  6580. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  6581. * per iteration.
  6582. * @param {*} [thisArg] The `this` binding of `iteratee`.
  6583. * @returns {Array} Returns the new sorted array.
  6584. * @example
  6585. *
  6586. * _.sortBy([1, 2, 3], function(n) {
  6587. * return Math.sin(n);
  6588. * });
  6589. * // => [3, 1, 2]
  6590. *
  6591. * _.sortBy([1, 2, 3], function(n) {
  6592. * return this.sin(n);
  6593. * }, Math);
  6594. * // => [3, 1, 2]
  6595. *
  6596. * var users = [
  6597. * { 'user': 'fred' },
  6598. * { 'user': 'pebbles' },
  6599. * { 'user': 'barney' }
  6600. * ];
  6601. *
  6602. * // using the `_.property` callback shorthand
  6603. * _.pluck(_.sortBy(users, 'user'), 'user');
  6604. * // => ['barney', 'fred', 'pebbles']
  6605. */
  6606. function sortBy(collection, iteratee, thisArg) {
  6607. if (collection == null) {
  6608. return [];
  6609. }
  6610. if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
  6611. iteratee = null;
  6612. }
  6613. var index = -1;
  6614. iteratee = getCallback(iteratee, thisArg, 3);
  6615. var result = baseMap(collection, function(value, key, collection) {
  6616. return { 'criteria': iteratee(value, key, collection), 'index': ++index, 'value': value };
  6617. });
  6618. return baseSortBy(result, compareAscending);
  6619. }
  6620. /**
  6621. * This method is like `_.sortBy` except that it can sort by multiple iteratees
  6622. * or property names.
  6623. *
  6624. * If a property name is provided for an iteratee the created `_.property`
  6625. * style callback returns the property value of the given element.
  6626. *
  6627. * If an object is provided for an iteratee the created `_.matches` style
  6628. * callback returns `true` for elements that have the properties of the given
  6629. * object, else `false`.
  6630. *
  6631. * @static
  6632. * @memberOf _
  6633. * @category Collection
  6634. * @param {Array|Object|string} collection The collection to iterate over.
  6635. * @param {...(Function|Function[]|Object|Object[]|string|string[])} iteratees
  6636. * The iteratees to sort by, specified as individual values or arrays of values.
  6637. * @returns {Array} Returns the new sorted array.
  6638. * @example
  6639. *
  6640. * var users = [
  6641. * { 'user': 'fred', 'age': 48 },
  6642. * { 'user': 'barney', 'age': 36 },
  6643. * { 'user': 'fred', 'age': 42 },
  6644. * { 'user': 'barney', 'age': 34 }
  6645. * ];
  6646. *
  6647. * _.map(_.sortByAll(users, ['user', 'age']), _.values);
  6648. * // => [['barney', 34], ['barney', 36], ['fred', 42], ['fred', 48]]
  6649. *
  6650. * _.map(_.sortByAll(users, 'user', function(chr) {
  6651. * return Math.floor(chr.age / 10);
  6652. * }), _.values);
  6653. * // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
  6654. */
  6655. var sortByAll = restParam(function(collection, iteratees) {
  6656. if (collection == null) {
  6657. return [];
  6658. }
  6659. var guard = iteratees[2];
  6660. if (guard && isIterateeCall(iteratees[0], iteratees[1], guard)) {
  6661. iteratees.length = 1;
  6662. }
  6663. return baseSortByOrder(collection, baseFlatten(iteratees), []);
  6664. });
  6665. /**
  6666. * This method is like `_.sortByAll` except that it allows specifying the
  6667. * sort orders of the iteratees to sort by. A truthy value in `orders` will
  6668. * sort the corresponding property name in ascending order while a falsey
  6669. * value will sort it in descending order.
  6670. *
  6671. * If a property name is provided for an iteratee the created `_.property`
  6672. * style callback returns the property value of the given element.
  6673. *
  6674. * If an object is provided for an iteratee the created `_.matches` style
  6675. * callback returns `true` for elements that have the properties of the given
  6676. * object, else `false`.
  6677. *
  6678. * @static
  6679. * @memberOf _
  6680. * @category Collection
  6681. * @param {Array|Object|string} collection The collection to iterate over.
  6682. * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by.
  6683. * @param {boolean[]} orders The sort orders of `iteratees`.
  6684. * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
  6685. * @returns {Array} Returns the new sorted array.
  6686. * @example
  6687. *
  6688. * var users = [
  6689. * { 'user': 'fred', 'age': 48 },
  6690. * { 'user': 'barney', 'age': 34 },
  6691. * { 'user': 'fred', 'age': 42 },
  6692. * { 'user': 'barney', 'age': 36 }
  6693. * ];
  6694. *
  6695. * // sort by `user` in ascending order and by `age` in descending order
  6696. * _.map(_.sortByOrder(users, ['user', 'age'], [true, false]), _.values);
  6697. * // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
  6698. */
  6699. function sortByOrder(collection, iteratees, orders, guard) {
  6700. if (collection == null) {
  6701. return [];
  6702. }
  6703. if (guard && isIterateeCall(iteratees, orders, guard)) {
  6704. orders = null;
  6705. }
  6706. if (!isArray(iteratees)) {
  6707. iteratees = iteratees == null ? [] : [iteratees];
  6708. }
  6709. if (!isArray(orders)) {
  6710. orders = orders == null ? [] : [orders];
  6711. }
  6712. return baseSortByOrder(collection, iteratees, orders);
  6713. }
  6714. /**
  6715. * Performs a deep comparison between each element in `collection` and the
  6716. * source object, returning an array of all elements that have equivalent
  6717. * property values.
  6718. *
  6719. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  6720. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  6721. * their own, not inherited, enumerable properties. For comparing a single
  6722. * own or inherited property value see `_.matchesProperty`.
  6723. *
  6724. * @static
  6725. * @memberOf _
  6726. * @category Collection
  6727. * @param {Array|Object|string} collection The collection to search.
  6728. * @param {Object} source The object of property values to match.
  6729. * @returns {Array} Returns the new filtered array.
  6730. * @example
  6731. *
  6732. * var users = [
  6733. * { 'user': 'barney', 'age': 36, 'active': false, 'pets': ['hoppy'] },
  6734. * { 'user': 'fred', 'age': 40, 'active': true, 'pets': ['baby puss', 'dino'] }
  6735. * ];
  6736. *
  6737. * _.pluck(_.where(users, { 'age': 36, 'active': false }), 'user');
  6738. * // => ['barney']
  6739. *
  6740. * _.pluck(_.where(users, { 'pets': ['dino'] }), 'user');
  6741. * // => ['fred']
  6742. */
  6743. function where(collection, source) {
  6744. return filter(collection, baseMatches(source));
  6745. }
  6746. /*------------------------------------------------------------------------*/
  6747. /**
  6748. * Gets the number of milliseconds that have elapsed since the Unix epoch
  6749. * (1 January 1970 00:00:00 UTC).
  6750. *
  6751. * @static
  6752. * @memberOf _
  6753. * @category Date
  6754. * @example
  6755. *
  6756. * _.defer(function(stamp) {
  6757. * console.log(_.now() - stamp);
  6758. * }, _.now());
  6759. * // => logs the number of milliseconds it took for the deferred function to be invoked
  6760. */
  6761. var now = nativeNow || function() {
  6762. return new Date().getTime();
  6763. };
  6764. /*------------------------------------------------------------------------*/
  6765. /**
  6766. * The opposite of `_.before`; this method creates a function that invokes
  6767. * `func` once it is called `n` or more times.
  6768. *
  6769. * @static
  6770. * @memberOf _
  6771. * @category Function
  6772. * @param {number} n The number of calls before `func` is invoked.
  6773. * @param {Function} func The function to restrict.
  6774. * @returns {Function} Returns the new restricted function.
  6775. * @example
  6776. *
  6777. * var saves = ['profile', 'settings'];
  6778. *
  6779. * var done = _.after(saves.length, function() {
  6780. * console.log('done saving!');
  6781. * });
  6782. *
  6783. * _.forEach(saves, function(type) {
  6784. * asyncSave({ 'type': type, 'complete': done });
  6785. * });
  6786. * // => logs 'done saving!' after the two async saves have completed
  6787. */
  6788. function after(n, func) {
  6789. if (typeof func != 'function') {
  6790. if (typeof n == 'function') {
  6791. var temp = n;
  6792. n = func;
  6793. func = temp;
  6794. } else {
  6795. throw new TypeError(FUNC_ERROR_TEXT);
  6796. }
  6797. }
  6798. n = nativeIsFinite(n = +n) ? n : 0;
  6799. return function() {
  6800. if (--n < 1) {
  6801. return func.apply(this, arguments);
  6802. }
  6803. };
  6804. }
  6805. /**
  6806. * Creates a function that accepts up to `n` arguments ignoring any
  6807. * additional arguments.
  6808. *
  6809. * @static
  6810. * @memberOf _
  6811. * @category Function
  6812. * @param {Function} func The function to cap arguments for.
  6813. * @param {number} [n=func.length] The arity cap.
  6814. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  6815. * @returns {Function} Returns the new function.
  6816. * @example
  6817. *
  6818. * _.map(['6', '8', '10'], _.ary(parseInt, 1));
  6819. * // => [6, 8, 10]
  6820. */
  6821. function ary(func, n, guard) {
  6822. if (guard && isIterateeCall(func, n, guard)) {
  6823. n = null;
  6824. }
  6825. n = (func && n == null) ? func.length : nativeMax(+n || 0, 0);
  6826. return createWrapper(func, ARY_FLAG, null, null, null, null, n);
  6827. }
  6828. /**
  6829. * Creates a function that invokes `func`, with the `this` binding and arguments
  6830. * of the created function, while it is called less than `n` times. Subsequent
  6831. * calls to the created function return the result of the last `func` invocation.
  6832. *
  6833. * @static
  6834. * @memberOf _
  6835. * @category Function
  6836. * @param {number} n The number of calls at which `func` is no longer invoked.
  6837. * @param {Function} func The function to restrict.
  6838. * @returns {Function} Returns the new restricted function.
  6839. * @example
  6840. *
  6841. * jQuery('#add').on('click', _.before(5, addContactToList));
  6842. * // => allows adding up to 4 contacts to the list
  6843. */
  6844. function before(n, func) {
  6845. var result;
  6846. if (typeof func != 'function') {
  6847. if (typeof n == 'function') {
  6848. var temp = n;
  6849. n = func;
  6850. func = temp;
  6851. } else {
  6852. throw new TypeError(FUNC_ERROR_TEXT);
  6853. }
  6854. }
  6855. return function() {
  6856. if (--n > 0) {
  6857. result = func.apply(this, arguments);
  6858. }
  6859. if (n <= 1) {
  6860. func = null;
  6861. }
  6862. return result;
  6863. };
  6864. }
  6865. /**
  6866. * Creates a function that invokes `func` with the `this` binding of `thisArg`
  6867. * and prepends any additional `_.bind` arguments to those provided to the
  6868. * bound function.
  6869. *
  6870. * The `_.bind.placeholder` value, which defaults to `_` in monolithic builds,
  6871. * may be used as a placeholder for partially applied arguments.
  6872. *
  6873. * **Note:** Unlike native `Function#bind` this method does not set the "length"
  6874. * property of bound functions.
  6875. *
  6876. * @static
  6877. * @memberOf _
  6878. * @category Function
  6879. * @param {Function} func The function to bind.
  6880. * @param {*} thisArg The `this` binding of `func`.
  6881. * @param {...*} [partials] The arguments to be partially applied.
  6882. * @returns {Function} Returns the new bound function.
  6883. * @example
  6884. *
  6885. * var greet = function(greeting, punctuation) {
  6886. * return greeting + ' ' + this.user + punctuation;
  6887. * };
  6888. *
  6889. * var object = { 'user': 'fred' };
  6890. *
  6891. * var bound = _.bind(greet, object, 'hi');
  6892. * bound('!');
  6893. * // => 'hi fred!'
  6894. *
  6895. * // using placeholders
  6896. * var bound = _.bind(greet, object, _, '!');
  6897. * bound('hi');
  6898. * // => 'hi fred!'
  6899. */
  6900. var bind = restParam(function(func, thisArg, partials) {
  6901. var bitmask = BIND_FLAG;
  6902. if (partials.length) {
  6903. var holders = replaceHolders(partials, bind.placeholder);
  6904. bitmask |= PARTIAL_FLAG;
  6905. }
  6906. return createWrapper(func, bitmask, thisArg, partials, holders);
  6907. });
  6908. /**
  6909. * Binds methods of an object to the object itself, overwriting the existing
  6910. * method. Method names may be specified as individual arguments or as arrays
  6911. * of method names. If no method names are provided all enumerable function
  6912. * properties, own and inherited, of `object` are bound.
  6913. *
  6914. * **Note:** This method does not set the "length" property of bound functions.
  6915. *
  6916. * @static
  6917. * @memberOf _
  6918. * @category Function
  6919. * @param {Object} object The object to bind and assign the bound methods to.
  6920. * @param {...(string|string[])} [methodNames] The object method names to bind,
  6921. * specified as individual method names or arrays of method names.
  6922. * @returns {Object} Returns `object`.
  6923. * @example
  6924. *
  6925. * var view = {
  6926. * 'label': 'docs',
  6927. * 'onClick': function() {
  6928. * console.log('clicked ' + this.label);
  6929. * }
  6930. * };
  6931. *
  6932. * _.bindAll(view);
  6933. * jQuery('#docs').on('click', view.onClick);
  6934. * // => logs 'clicked docs' when the element is clicked
  6935. */
  6936. var bindAll = restParam(function(object, methodNames) {
  6937. methodNames = methodNames.length ? baseFlatten(methodNames) : functions(object);
  6938. var index = -1,
  6939. length = methodNames.length;
  6940. while (++index < length) {
  6941. var key = methodNames[index];
  6942. object[key] = createWrapper(object[key], BIND_FLAG, object);
  6943. }
  6944. return object;
  6945. });
  6946. /**
  6947. * Creates a function that invokes the method at `object[key]` and prepends
  6948. * any additional `_.bindKey` arguments to those provided to the bound function.
  6949. *
  6950. * This method differs from `_.bind` by allowing bound functions to reference
  6951. * methods that may be redefined or don't yet exist.
  6952. * See [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern)
  6953. * for more details.
  6954. *
  6955. * The `_.bindKey.placeholder` value, which defaults to `_` in monolithic
  6956. * builds, may be used as a placeholder for partially applied arguments.
  6957. *
  6958. * @static
  6959. * @memberOf _
  6960. * @category Function
  6961. * @param {Object} object The object the method belongs to.
  6962. * @param {string} key The key of the method.
  6963. * @param {...*} [partials] The arguments to be partially applied.
  6964. * @returns {Function} Returns the new bound function.
  6965. * @example
  6966. *
  6967. * var object = {
  6968. * 'user': 'fred',
  6969. * 'greet': function(greeting, punctuation) {
  6970. * return greeting + ' ' + this.user + punctuation;
  6971. * }
  6972. * };
  6973. *
  6974. * var bound = _.bindKey(object, 'greet', 'hi');
  6975. * bound('!');
  6976. * // => 'hi fred!'
  6977. *
  6978. * object.greet = function(greeting, punctuation) {
  6979. * return greeting + 'ya ' + this.user + punctuation;
  6980. * };
  6981. *
  6982. * bound('!');
  6983. * // => 'hiya fred!'
  6984. *
  6985. * // using placeholders
  6986. * var bound = _.bindKey(object, 'greet', _, '!');
  6987. * bound('hi');
  6988. * // => 'hiya fred!'
  6989. */
  6990. var bindKey = restParam(function(object, key, partials) {
  6991. var bitmask = BIND_FLAG | BIND_KEY_FLAG;
  6992. if (partials.length) {
  6993. var holders = replaceHolders(partials, bindKey.placeholder);
  6994. bitmask |= PARTIAL_FLAG;
  6995. }
  6996. return createWrapper(key, bitmask, object, partials, holders);
  6997. });
  6998. /**
  6999. * Creates a function that accepts one or more arguments of `func` that when
  7000. * called either invokes `func` returning its result, if all `func` arguments
  7001. * have been provided, or returns a function that accepts one or more of the
  7002. * remaining `func` arguments, and so on. The arity of `func` may be specified
  7003. * if `func.length` is not sufficient.
  7004. *
  7005. * The `_.curry.placeholder` value, which defaults to `_` in monolithic builds,
  7006. * may be used as a placeholder for provided arguments.
  7007. *
  7008. * **Note:** This method does not set the "length" property of curried functions.
  7009. *
  7010. * @static
  7011. * @memberOf _
  7012. * @category Function
  7013. * @param {Function} func The function to curry.
  7014. * @param {number} [arity=func.length] The arity of `func`.
  7015. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  7016. * @returns {Function} Returns the new curried function.
  7017. * @example
  7018. *
  7019. * var abc = function(a, b, c) {
  7020. * return [a, b, c];
  7021. * };
  7022. *
  7023. * var curried = _.curry(abc);
  7024. *
  7025. * curried(1)(2)(3);
  7026. * // => [1, 2, 3]
  7027. *
  7028. * curried(1, 2)(3);
  7029. * // => [1, 2, 3]
  7030. *
  7031. * curried(1, 2, 3);
  7032. * // => [1, 2, 3]
  7033. *
  7034. * // using placeholders
  7035. * curried(1)(_, 3)(2);
  7036. * // => [1, 2, 3]
  7037. */
  7038. var curry = createCurry(CURRY_FLAG);
  7039. /**
  7040. * This method is like `_.curry` except that arguments are applied to `func`
  7041. * in the manner of `_.partialRight` instead of `_.partial`.
  7042. *
  7043. * The `_.curryRight.placeholder` value, which defaults to `_` in monolithic
  7044. * builds, may be used as a placeholder for provided arguments.
  7045. *
  7046. * **Note:** This method does not set the "length" property of curried functions.
  7047. *
  7048. * @static
  7049. * @memberOf _
  7050. * @category Function
  7051. * @param {Function} func The function to curry.
  7052. * @param {number} [arity=func.length] The arity of `func`.
  7053. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  7054. * @returns {Function} Returns the new curried function.
  7055. * @example
  7056. *
  7057. * var abc = function(a, b, c) {
  7058. * return [a, b, c];
  7059. * };
  7060. *
  7061. * var curried = _.curryRight(abc);
  7062. *
  7063. * curried(3)(2)(1);
  7064. * // => [1, 2, 3]
  7065. *
  7066. * curried(2, 3)(1);
  7067. * // => [1, 2, 3]
  7068. *
  7069. * curried(1, 2, 3);
  7070. * // => [1, 2, 3]
  7071. *
  7072. * // using placeholders
  7073. * curried(3)(1, _)(2);
  7074. * // => [1, 2, 3]
  7075. */
  7076. var curryRight = createCurry(CURRY_RIGHT_FLAG);
  7077. /**
  7078. * Creates a debounced function that delays invoking `func` until after `wait`
  7079. * milliseconds have elapsed since the last time the debounced function was
  7080. * invoked. The debounced function comes with a `cancel` method to cancel
  7081. * delayed invocations. Provide an options object to indicate that `func`
  7082. * should be invoked on the leading and/or trailing edge of the `wait` timeout.
  7083. * Subsequent calls to the debounced function return the result of the last
  7084. * `func` invocation.
  7085. *
  7086. * **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
  7087. * on the trailing edge of the timeout only if the the debounced function is
  7088. * invoked more than once during the `wait` timeout.
  7089. *
  7090. * See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation)
  7091. * for details over the differences between `_.debounce` and `_.throttle`.
  7092. *
  7093. * @static
  7094. * @memberOf _
  7095. * @category Function
  7096. * @param {Function} func The function to debounce.
  7097. * @param {number} [wait=0] The number of milliseconds to delay.
  7098. * @param {Object} [options] The options object.
  7099. * @param {boolean} [options.leading=false] Specify invoking on the leading
  7100. * edge of the timeout.
  7101. * @param {number} [options.maxWait] The maximum time `func` is allowed to be
  7102. * delayed before it is invoked.
  7103. * @param {boolean} [options.trailing=true] Specify invoking on the trailing
  7104. * edge of the timeout.
  7105. * @returns {Function} Returns the new debounced function.
  7106. * @example
  7107. *
  7108. * // avoid costly calculations while the window size is in flux
  7109. * jQuery(window).on('resize', _.debounce(calculateLayout, 150));
  7110. *
  7111. * // invoke `sendMail` when the click event is fired, debouncing subsequent calls
  7112. * jQuery('#postbox').on('click', _.debounce(sendMail, 300, {
  7113. * 'leading': true,
  7114. * 'trailing': false
  7115. * }));
  7116. *
  7117. * // ensure `batchLog` is invoked once after 1 second of debounced calls
  7118. * var source = new EventSource('/stream');
  7119. * jQuery(source).on('message', _.debounce(batchLog, 250, {
  7120. * 'maxWait': 1000
  7121. * }));
  7122. *
  7123. * // cancel a debounced call
  7124. * var todoChanges = _.debounce(batchLog, 1000);
  7125. * Object.observe(models.todo, todoChanges);
  7126. *
  7127. * Object.observe(models, function(changes) {
  7128. * if (_.find(changes, { 'user': 'todo', 'type': 'delete'})) {
  7129. * todoChanges.cancel();
  7130. * }
  7131. * }, ['delete']);
  7132. *
  7133. * // ...at some point `models.todo` is changed
  7134. * models.todo.completed = true;
  7135. *
  7136. * // ...before 1 second has passed `models.todo` is deleted
  7137. * // which cancels the debounced `todoChanges` call
  7138. * delete models.todo;
  7139. */
  7140. function debounce(func, wait, options) {
  7141. var args,
  7142. maxTimeoutId,
  7143. result,
  7144. stamp,
  7145. thisArg,
  7146. timeoutId,
  7147. trailingCall,
  7148. lastCalled = 0,
  7149. maxWait = false,
  7150. trailing = true;
  7151. if (typeof func != 'function') {
  7152. throw new TypeError(FUNC_ERROR_TEXT);
  7153. }
  7154. wait = wait < 0 ? 0 : (+wait || 0);
  7155. if (options === true) {
  7156. var leading = true;
  7157. trailing = false;
  7158. } else if (isObject(options)) {
  7159. leading = options.leading;
  7160. maxWait = 'maxWait' in options && nativeMax(+options.maxWait || 0, wait);
  7161. trailing = 'trailing' in options ? options.trailing : trailing;
  7162. }
  7163. function cancel() {
  7164. if (timeoutId) {
  7165. clearTimeout(timeoutId);
  7166. }
  7167. if (maxTimeoutId) {
  7168. clearTimeout(maxTimeoutId);
  7169. }
  7170. maxTimeoutId = timeoutId = trailingCall = undefined;
  7171. }
  7172. function delayed() {
  7173. var remaining = wait - (now() - stamp);
  7174. if (remaining <= 0 || remaining > wait) {
  7175. if (maxTimeoutId) {
  7176. clearTimeout(maxTimeoutId);
  7177. }
  7178. var isCalled = trailingCall;
  7179. maxTimeoutId = timeoutId = trailingCall = undefined;
  7180. if (isCalled) {
  7181. lastCalled = now();
  7182. result = func.apply(thisArg, args);
  7183. if (!timeoutId && !maxTimeoutId) {
  7184. args = thisArg = null;
  7185. }
  7186. }
  7187. } else {
  7188. timeoutId = setTimeout(delayed, remaining);
  7189. }
  7190. }
  7191. function maxDelayed() {
  7192. if (timeoutId) {
  7193. clearTimeout(timeoutId);
  7194. }
  7195. maxTimeoutId = timeoutId = trailingCall = undefined;
  7196. if (trailing || (maxWait !== wait)) {
  7197. lastCalled = now();
  7198. result = func.apply(thisArg, args);
  7199. if (!timeoutId && !maxTimeoutId) {
  7200. args = thisArg = null;
  7201. }
  7202. }
  7203. }
  7204. function debounced() {
  7205. args = arguments;
  7206. stamp = now();
  7207. thisArg = this;
  7208. trailingCall = trailing && (timeoutId || !leading);
  7209. if (maxWait === false) {
  7210. var leadingCall = leading && !timeoutId;
  7211. } else {
  7212. if (!maxTimeoutId && !leading) {
  7213. lastCalled = stamp;
  7214. }
  7215. var remaining = maxWait - (stamp - lastCalled),
  7216. isCalled = remaining <= 0 || remaining > maxWait;
  7217. if (isCalled) {
  7218. if (maxTimeoutId) {
  7219. maxTimeoutId = clearTimeout(maxTimeoutId);
  7220. }
  7221. lastCalled = stamp;
  7222. result = func.apply(thisArg, args);
  7223. }
  7224. else if (!maxTimeoutId) {
  7225. maxTimeoutId = setTimeout(maxDelayed, remaining);
  7226. }
  7227. }
  7228. if (isCalled && timeoutId) {
  7229. timeoutId = clearTimeout(timeoutId);
  7230. }
  7231. else if (!timeoutId && wait !== maxWait) {
  7232. timeoutId = setTimeout(delayed, wait);
  7233. }
  7234. if (leadingCall) {
  7235. isCalled = true;
  7236. result = func.apply(thisArg, args);
  7237. }
  7238. if (isCalled && !timeoutId && !maxTimeoutId) {
  7239. args = thisArg = null;
  7240. }
  7241. return result;
  7242. }
  7243. debounced.cancel = cancel;
  7244. return debounced;
  7245. }
  7246. /**
  7247. * Defers invoking the `func` until the current call stack has cleared. Any
  7248. * additional arguments are provided to `func` when it is invoked.
  7249. *
  7250. * @static
  7251. * @memberOf _
  7252. * @category Function
  7253. * @param {Function} func The function to defer.
  7254. * @param {...*} [args] The arguments to invoke the function with.
  7255. * @returns {number} Returns the timer id.
  7256. * @example
  7257. *
  7258. * _.defer(function(text) {
  7259. * console.log(text);
  7260. * }, 'deferred');
  7261. * // logs 'deferred' after one or more milliseconds
  7262. */
  7263. var defer = restParam(function(func, args) {
  7264. return baseDelay(func, 1, args);
  7265. });
  7266. /**
  7267. * Invokes `func` after `wait` milliseconds. Any additional arguments are
  7268. * provided to `func` when it is invoked.
  7269. *
  7270. * @static
  7271. * @memberOf _
  7272. * @category Function
  7273. * @param {Function} func The function to delay.
  7274. * @param {number} wait The number of milliseconds to delay invocation.
  7275. * @param {...*} [args] The arguments to invoke the function with.
  7276. * @returns {number} Returns the timer id.
  7277. * @example
  7278. *
  7279. * _.delay(function(text) {
  7280. * console.log(text);
  7281. * }, 1000, 'later');
  7282. * // => logs 'later' after one second
  7283. */
  7284. var delay = restParam(function(func, wait, args) {
  7285. return baseDelay(func, wait, args);
  7286. });
  7287. /**
  7288. * Creates a function that returns the result of invoking the provided
  7289. * functions with the `this` binding of the created function, where each
  7290. * successive invocation is supplied the return value of the previous.
  7291. *
  7292. * @static
  7293. * @memberOf _
  7294. * @category Function
  7295. * @param {...Function} [funcs] Functions to invoke.
  7296. * @returns {Function} Returns the new function.
  7297. * @example
  7298. *
  7299. * function square(n) {
  7300. * return n * n;
  7301. * }
  7302. *
  7303. * var addSquare = _.flow(_.add, square);
  7304. * addSquare(1, 2);
  7305. * // => 9
  7306. */
  7307. var flow = createFlow();
  7308. /**
  7309. * This method is like `_.flow` except that it creates a function that
  7310. * invokes the provided functions from right to left.
  7311. *
  7312. * @static
  7313. * @memberOf _
  7314. * @alias backflow, compose
  7315. * @category Function
  7316. * @param {...Function} [funcs] Functions to invoke.
  7317. * @returns {Function} Returns the new function.
  7318. * @example
  7319. *
  7320. * function square(n) {
  7321. * return n * n;
  7322. * }
  7323. *
  7324. * var addSquare = _.flowRight(square, _.add);
  7325. * addSquare(1, 2);
  7326. * // => 9
  7327. */
  7328. var flowRight = createFlow(true);
  7329. /**
  7330. * Creates a function that memoizes the result of `func`. If `resolver` is
  7331. * provided it determines the cache key for storing the result based on the
  7332. * arguments provided to the memoized function. By default, the first argument
  7333. * provided to the memoized function is coerced to a string and used as the
  7334. * cache key. The `func` is invoked with the `this` binding of the memoized
  7335. * function.
  7336. *
  7337. * **Note:** The cache is exposed as the `cache` property on the memoized
  7338. * function. Its creation may be customized by replacing the `_.memoize.Cache`
  7339. * constructor with one whose instances implement the [`Map`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-the-map-prototype-object)
  7340. * method interface of `get`, `has`, and `set`.
  7341. *
  7342. * @static
  7343. * @memberOf _
  7344. * @category Function
  7345. * @param {Function} func The function to have its output memoized.
  7346. * @param {Function} [resolver] The function to resolve the cache key.
  7347. * @returns {Function} Returns the new memoizing function.
  7348. * @example
  7349. *
  7350. * var upperCase = _.memoize(function(string) {
  7351. * return string.toUpperCase();
  7352. * });
  7353. *
  7354. * upperCase('fred');
  7355. * // => 'FRED'
  7356. *
  7357. * // modifying the result cache
  7358. * upperCase.cache.set('fred', 'BARNEY');
  7359. * upperCase('fred');
  7360. * // => 'BARNEY'
  7361. *
  7362. * // replacing `_.memoize.Cache`
  7363. * var object = { 'user': 'fred' };
  7364. * var other = { 'user': 'barney' };
  7365. * var identity = _.memoize(_.identity);
  7366. *
  7367. * identity(object);
  7368. * // => { 'user': 'fred' }
  7369. * identity(other);
  7370. * // => { 'user': 'fred' }
  7371. *
  7372. * _.memoize.Cache = WeakMap;
  7373. * var identity = _.memoize(_.identity);
  7374. *
  7375. * identity(object);
  7376. * // => { 'user': 'fred' }
  7377. * identity(other);
  7378. * // => { 'user': 'barney' }
  7379. */
  7380. function memoize(func, resolver) {
  7381. if (typeof func != 'function' || (resolver && typeof resolver != 'function')) {
  7382. throw new TypeError(FUNC_ERROR_TEXT);
  7383. }
  7384. var memoized = function() {
  7385. var args = arguments,
  7386. key = resolver ? resolver.apply(this, args) : args[0],
  7387. cache = memoized.cache;
  7388. if (cache.has(key)) {
  7389. return cache.get(key);
  7390. }
  7391. var result = func.apply(this, args);
  7392. memoized.cache = cache.set(key, result);
  7393. return result;
  7394. };
  7395. memoized.cache = new memoize.Cache;
  7396. return memoized;
  7397. }
  7398. /**
  7399. * Creates a function that negates the result of the predicate `func`. The
  7400. * `func` predicate is invoked with the `this` binding and arguments of the
  7401. * created function.
  7402. *
  7403. * @static
  7404. * @memberOf _
  7405. * @category Function
  7406. * @param {Function} predicate The predicate to negate.
  7407. * @returns {Function} Returns the new function.
  7408. * @example
  7409. *
  7410. * function isEven(n) {
  7411. * return n % 2 == 0;
  7412. * }
  7413. *
  7414. * _.filter([1, 2, 3, 4, 5, 6], _.negate(isEven));
  7415. * // => [1, 3, 5]
  7416. */
  7417. function negate(predicate) {
  7418. if (typeof predicate != 'function') {
  7419. throw new TypeError(FUNC_ERROR_TEXT);
  7420. }
  7421. return function() {
  7422. return !predicate.apply(this, arguments);
  7423. };
  7424. }
  7425. /**
  7426. * Creates a function that is restricted to invoking `func` once. Repeat calls
  7427. * to the function return the value of the first call. The `func` is invoked
  7428. * with the `this` binding and arguments of the created function.
  7429. *
  7430. * @static
  7431. * @memberOf _
  7432. * @category Function
  7433. * @param {Function} func The function to restrict.
  7434. * @returns {Function} Returns the new restricted function.
  7435. * @example
  7436. *
  7437. * var initialize = _.once(createApplication);
  7438. * initialize();
  7439. * initialize();
  7440. * // `initialize` invokes `createApplication` once
  7441. */
  7442. function once(func) {
  7443. return before(2, func);
  7444. }
  7445. /**
  7446. * Creates a function that invokes `func` with `partial` arguments prepended
  7447. * to those provided to the new function. This method is like `_.bind` except
  7448. * it does **not** alter the `this` binding.
  7449. *
  7450. * The `_.partial.placeholder` value, which defaults to `_` in monolithic
  7451. * builds, may be used as a placeholder for partially applied arguments.
  7452. *
  7453. * **Note:** This method does not set the "length" property of partially
  7454. * applied functions.
  7455. *
  7456. * @static
  7457. * @memberOf _
  7458. * @category Function
  7459. * @param {Function} func The function to partially apply arguments to.
  7460. * @param {...*} [partials] The arguments to be partially applied.
  7461. * @returns {Function} Returns the new partially applied function.
  7462. * @example
  7463. *
  7464. * var greet = function(greeting, name) {
  7465. * return greeting + ' ' + name;
  7466. * };
  7467. *
  7468. * var sayHelloTo = _.partial(greet, 'hello');
  7469. * sayHelloTo('fred');
  7470. * // => 'hello fred'
  7471. *
  7472. * // using placeholders
  7473. * var greetFred = _.partial(greet, _, 'fred');
  7474. * greetFred('hi');
  7475. * // => 'hi fred'
  7476. */
  7477. var partial = createPartial(PARTIAL_FLAG);
  7478. /**
  7479. * This method is like `_.partial` except that partially applied arguments
  7480. * are appended to those provided to the new function.
  7481. *
  7482. * The `_.partialRight.placeholder` value, which defaults to `_` in monolithic
  7483. * builds, may be used as a placeholder for partially applied arguments.
  7484. *
  7485. * **Note:** This method does not set the "length" property of partially
  7486. * applied functions.
  7487. *
  7488. * @static
  7489. * @memberOf _
  7490. * @category Function
  7491. * @param {Function} func The function to partially apply arguments to.
  7492. * @param {...*} [partials] The arguments to be partially applied.
  7493. * @returns {Function} Returns the new partially applied function.
  7494. * @example
  7495. *
  7496. * var greet = function(greeting, name) {
  7497. * return greeting + ' ' + name;
  7498. * };
  7499. *
  7500. * var greetFred = _.partialRight(greet, 'fred');
  7501. * greetFred('hi');
  7502. * // => 'hi fred'
  7503. *
  7504. * // using placeholders
  7505. * var sayHelloTo = _.partialRight(greet, 'hello', _);
  7506. * sayHelloTo('fred');
  7507. * // => 'hello fred'
  7508. */
  7509. var partialRight = createPartial(PARTIAL_RIGHT_FLAG);
  7510. /**
  7511. * Creates a function that invokes `func` with arguments arranged according
  7512. * to the specified indexes where the argument value at the first index is
  7513. * provided as the first argument, the argument value at the second index is
  7514. * provided as the second argument, and so on.
  7515. *
  7516. * @static
  7517. * @memberOf _
  7518. * @category Function
  7519. * @param {Function} func The function to rearrange arguments for.
  7520. * @param {...(number|number[])} indexes The arranged argument indexes,
  7521. * specified as individual indexes or arrays of indexes.
  7522. * @returns {Function} Returns the new function.
  7523. * @example
  7524. *
  7525. * var rearged = _.rearg(function(a, b, c) {
  7526. * return [a, b, c];
  7527. * }, 2, 0, 1);
  7528. *
  7529. * rearged('b', 'c', 'a')
  7530. * // => ['a', 'b', 'c']
  7531. *
  7532. * var map = _.rearg(_.map, [1, 0]);
  7533. * map(function(n) {
  7534. * return n * 3;
  7535. * }, [1, 2, 3]);
  7536. * // => [3, 6, 9]
  7537. */
  7538. var rearg = restParam(function(func, indexes) {
  7539. return createWrapper(func, REARG_FLAG, null, null, null, baseFlatten(indexes));
  7540. });
  7541. /**
  7542. * Creates a function that invokes `func` with the `this` binding of the
  7543. * created function and arguments from `start` and beyond provided as an array.
  7544. *
  7545. * **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).
  7546. *
  7547. * @static
  7548. * @memberOf _
  7549. * @category Function
  7550. * @param {Function} func The function to apply a rest parameter to.
  7551. * @param {number} [start=func.length-1] The start position of the rest parameter.
  7552. * @returns {Function} Returns the new function.
  7553. * @example
  7554. *
  7555. * var say = _.restParam(function(what, names) {
  7556. * return what + ' ' + _.initial(names).join(', ') +
  7557. * (_.size(names) > 1 ? ', & ' : '') + _.last(names);
  7558. * });
  7559. *
  7560. * say('hello', 'fred', 'barney', 'pebbles');
  7561. * // => 'hello fred, barney, & pebbles'
  7562. */
  7563. function restParam(func, start) {
  7564. if (typeof func != 'function') {
  7565. throw new TypeError(FUNC_ERROR_TEXT);
  7566. }
  7567. start = nativeMax(start === undefined ? (func.length - 1) : (+start || 0), 0);
  7568. return function() {
  7569. var args = arguments,
  7570. index = -1,
  7571. length = nativeMax(args.length - start, 0),
  7572. rest = Array(length);
  7573. while (++index < length) {
  7574. rest[index] = args[start + index];
  7575. }
  7576. switch (start) {
  7577. case 0: return func.call(this, rest);
  7578. case 1: return func.call(this, args[0], rest);
  7579. case 2: return func.call(this, args[0], args[1], rest);
  7580. }
  7581. var otherArgs = Array(start + 1);
  7582. index = -1;
  7583. while (++index < start) {
  7584. otherArgs[index] = args[index];
  7585. }
  7586. otherArgs[start] = rest;
  7587. return func.apply(this, otherArgs);
  7588. };
  7589. }
  7590. /**
  7591. * Creates a function that invokes `func` with the `this` binding of the created
  7592. * function and an array of arguments much like [`Function#apply`](https://es5.github.io/#x15.3.4.3).
  7593. *
  7594. * **Note:** This method is based on the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator).
  7595. *
  7596. * @static
  7597. * @memberOf _
  7598. * @category Function
  7599. * @param {Function} func The function to spread arguments over.
  7600. * @returns {Function} Returns the new function.
  7601. * @example
  7602. *
  7603. * var say = _.spread(function(who, what) {
  7604. * return who + ' says ' + what;
  7605. * });
  7606. *
  7607. * say(['fred', 'hello']);
  7608. * // => 'fred says hello'
  7609. *
  7610. * // with a Promise
  7611. * var numbers = Promise.all([
  7612. * Promise.resolve(40),
  7613. * Promise.resolve(36)
  7614. * ]);
  7615. *
  7616. * numbers.then(_.spread(function(x, y) {
  7617. * return x + y;
  7618. * }));
  7619. * // => a Promise of 76
  7620. */
  7621. function spread(func) {
  7622. if (typeof func != 'function') {
  7623. throw new TypeError(FUNC_ERROR_TEXT);
  7624. }
  7625. return function(array) {
  7626. return func.apply(this, array);
  7627. };
  7628. }
  7629. /**
  7630. * Creates a throttled function that only invokes `func` at most once per
  7631. * every `wait` milliseconds. The throttled function comes with a `cancel`
  7632. * method to cancel delayed invocations. Provide an options object to indicate
  7633. * that `func` should be invoked on the leading and/or trailing edge of the
  7634. * `wait` timeout. Subsequent calls to the throttled function return the
  7635. * result of the last `func` call.
  7636. *
  7637. * **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
  7638. * on the trailing edge of the timeout only if the the throttled function is
  7639. * invoked more than once during the `wait` timeout.
  7640. *
  7641. * See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation)
  7642. * for details over the differences between `_.throttle` and `_.debounce`.
  7643. *
  7644. * @static
  7645. * @memberOf _
  7646. * @category Function
  7647. * @param {Function} func The function to throttle.
  7648. * @param {number} [wait=0] The number of milliseconds to throttle invocations to.
  7649. * @param {Object} [options] The options object.
  7650. * @param {boolean} [options.leading=true] Specify invoking on the leading
  7651. * edge of the timeout.
  7652. * @param {boolean} [options.trailing=true] Specify invoking on the trailing
  7653. * edge of the timeout.
  7654. * @returns {Function} Returns the new throttled function.
  7655. * @example
  7656. *
  7657. * // avoid excessively updating the position while scrolling
  7658. * jQuery(window).on('scroll', _.throttle(updatePosition, 100));
  7659. *
  7660. * // invoke `renewToken` when the click event is fired, but not more than once every 5 minutes
  7661. * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {
  7662. * 'trailing': false
  7663. * }));
  7664. *
  7665. * // cancel a trailing throttled call
  7666. * jQuery(window).on('popstate', throttled.cancel);
  7667. */
  7668. function throttle(func, wait, options) {
  7669. var leading = true,
  7670. trailing = true;
  7671. if (typeof func != 'function') {
  7672. throw new TypeError(FUNC_ERROR_TEXT);
  7673. }
  7674. if (options === false) {
  7675. leading = false;
  7676. } else if (isObject(options)) {
  7677. leading = 'leading' in options ? !!options.leading : leading;
  7678. trailing = 'trailing' in options ? !!options.trailing : trailing;
  7679. }
  7680. debounceOptions.leading = leading;
  7681. debounceOptions.maxWait = +wait;
  7682. debounceOptions.trailing = trailing;
  7683. return debounce(func, wait, debounceOptions);
  7684. }
  7685. /**
  7686. * Creates a function that provides `value` to the wrapper function as its
  7687. * first argument. Any additional arguments provided to the function are
  7688. * appended to those provided to the wrapper function. The wrapper is invoked
  7689. * with the `this` binding of the created function.
  7690. *
  7691. * @static
  7692. * @memberOf _
  7693. * @category Function
  7694. * @param {*} value The value to wrap.
  7695. * @param {Function} wrapper The wrapper function.
  7696. * @returns {Function} Returns the new function.
  7697. * @example
  7698. *
  7699. * var p = _.wrap(_.escape, function(func, text) {
  7700. * return '<p>' + func(text) + '</p>';
  7701. * });
  7702. *
  7703. * p('fred, barney, & pebbles');
  7704. * // => '<p>fred, barney, &amp; pebbles</p>'
  7705. */
  7706. function wrap(value, wrapper) {
  7707. wrapper = wrapper == null ? identity : wrapper;
  7708. return createWrapper(wrapper, PARTIAL_FLAG, null, [value], []);
  7709. }
  7710. /*------------------------------------------------------------------------*/
  7711. /**
  7712. * Creates a clone of `value`. If `isDeep` is `true` nested objects are cloned,
  7713. * otherwise they are assigned by reference. If `customizer` is provided it is
  7714. * invoked to produce the cloned values. If `customizer` returns `undefined`
  7715. * cloning is handled by the method instead. The `customizer` is bound to
  7716. * `thisArg` and invoked with two argument; (value [, index|key, object]).
  7717. *
  7718. * **Note:** This method is loosely based on the
  7719. * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).
  7720. * The enumerable properties of `arguments` objects and objects created by
  7721. * constructors other than `Object` are cloned to plain `Object` objects. An
  7722. * empty object is returned for uncloneable values such as functions, DOM nodes,
  7723. * Maps, Sets, and WeakMaps.
  7724. *
  7725. * @static
  7726. * @memberOf _
  7727. * @category Lang
  7728. * @param {*} value The value to clone.
  7729. * @param {boolean} [isDeep] Specify a deep clone.
  7730. * @param {Function} [customizer] The function to customize cloning values.
  7731. * @param {*} [thisArg] The `this` binding of `customizer`.
  7732. * @returns {*} Returns the cloned value.
  7733. * @example
  7734. *
  7735. * var users = [
  7736. * { 'user': 'barney' },
  7737. * { 'user': 'fred' }
  7738. * ];
  7739. *
  7740. * var shallow = _.clone(users);
  7741. * shallow[0] === users[0];
  7742. * // => true
  7743. *
  7744. * var deep = _.clone(users, true);
  7745. * deep[0] === users[0];
  7746. * // => false
  7747. *
  7748. * // using a customizer callback
  7749. * var el = _.clone(document.body, function(value) {
  7750. * if (_.isElement(value)) {
  7751. * return value.cloneNode(false);
  7752. * }
  7753. * });
  7754. *
  7755. * el === document.body
  7756. * // => false
  7757. * el.nodeName
  7758. * // => BODY
  7759. * el.childNodes.length;
  7760. * // => 0
  7761. */
  7762. function clone(value, isDeep, customizer, thisArg) {
  7763. if (isDeep && typeof isDeep != 'boolean' && isIterateeCall(value, isDeep, customizer)) {
  7764. isDeep = false;
  7765. }
  7766. else if (typeof isDeep == 'function') {
  7767. thisArg = customizer;
  7768. customizer = isDeep;
  7769. isDeep = false;
  7770. }
  7771. return typeof customizer == 'function'
  7772. ? baseClone(value, isDeep, bindCallback(customizer, thisArg, 1))
  7773. : baseClone(value, isDeep);
  7774. }
  7775. /**
  7776. * Creates a deep clone of `value`. If `customizer` is provided it is invoked
  7777. * to produce the cloned values. If `customizer` returns `undefined` cloning
  7778. * is handled by the method instead. The `customizer` is bound to `thisArg`
  7779. * and invoked with two argument; (value [, index|key, object]).
  7780. *
  7781. * **Note:** This method is loosely based on the
  7782. * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).
  7783. * The enumerable properties of `arguments` objects and objects created by
  7784. * constructors other than `Object` are cloned to plain `Object` objects. An
  7785. * empty object is returned for uncloneable values such as functions, DOM nodes,
  7786. * Maps, Sets, and WeakMaps.
  7787. *
  7788. * @static
  7789. * @memberOf _
  7790. * @category Lang
  7791. * @param {*} value The value to deep clone.
  7792. * @param {Function} [customizer] The function to customize cloning values.
  7793. * @param {*} [thisArg] The `this` binding of `customizer`.
  7794. * @returns {*} Returns the deep cloned value.
  7795. * @example
  7796. *
  7797. * var users = [
  7798. * { 'user': 'barney' },
  7799. * { 'user': 'fred' }
  7800. * ];
  7801. *
  7802. * var deep = _.cloneDeep(users);
  7803. * deep[0] === users[0];
  7804. * // => false
  7805. *
  7806. * // using a customizer callback
  7807. * var el = _.cloneDeep(document.body, function(value) {
  7808. * if (_.isElement(value)) {
  7809. * return value.cloneNode(true);
  7810. * }
  7811. * });
  7812. *
  7813. * el === document.body
  7814. * // => false
  7815. * el.nodeName
  7816. * // => BODY
  7817. * el.childNodes.length;
  7818. * // => 20
  7819. */
  7820. function cloneDeep(value, customizer, thisArg) {
  7821. return typeof customizer == 'function'
  7822. ? baseClone(value, true, bindCallback(customizer, thisArg, 1))
  7823. : baseClone(value, true);
  7824. }
  7825. /**
  7826. * Checks if `value` is greater than `other`.
  7827. *
  7828. * @static
  7829. * @memberOf _
  7830. * @category Lang
  7831. * @param {*} value The value to compare.
  7832. * @param {*} other The other value to compare.
  7833. * @returns {boolean} Returns `true` if `value` is greater than `other`, else `false`.
  7834. * @example
  7835. *
  7836. * _.gt(3, 1);
  7837. * // => true
  7838. *
  7839. * _.gt(3, 3);
  7840. * // => false
  7841. *
  7842. * _.gt(1, 3);
  7843. * // => false
  7844. */
  7845. function gt(value, other) {
  7846. return value > other;
  7847. }
  7848. /**
  7849. * Checks if `value` is greater than or equal to `other`.
  7850. *
  7851. * @static
  7852. * @memberOf _
  7853. * @category Lang
  7854. * @param {*} value The value to compare.
  7855. * @param {*} other The other value to compare.
  7856. * @returns {boolean} Returns `true` if `value` is greater than or equal to `other`, else `false`.
  7857. * @example
  7858. *
  7859. * _.gte(3, 1);
  7860. * // => true
  7861. *
  7862. * _.gte(3, 3);
  7863. * // => true
  7864. *
  7865. * _.gte(1, 3);
  7866. * // => false
  7867. */
  7868. function gte(value, other) {
  7869. return value >= other;
  7870. }
  7871. /**
  7872. * Checks if `value` is classified as an `arguments` object.
  7873. *
  7874. * @static
  7875. * @memberOf _
  7876. * @category Lang
  7877. * @param {*} value The value to check.
  7878. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  7879. * @example
  7880. *
  7881. * _.isArguments(function() { return arguments; }());
  7882. * // => true
  7883. *
  7884. * _.isArguments([1, 2, 3]);
  7885. * // => false
  7886. */
  7887. function isArguments(value) {
  7888. return isObjectLike(value) && isArrayLike(value) && objToString.call(value) == argsTag;
  7889. }
  7890. /**
  7891. * Checks if `value` is classified as an `Array` object.
  7892. *
  7893. * @static
  7894. * @memberOf _
  7895. * @category Lang
  7896. * @param {*} value The value to check.
  7897. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  7898. * @example
  7899. *
  7900. * _.isArray([1, 2, 3]);
  7901. * // => true
  7902. *
  7903. * _.isArray(function() { return arguments; }());
  7904. * // => false
  7905. */
  7906. var isArray = nativeIsArray || function(value) {
  7907. return isObjectLike(value) && isLength(value.length) && objToString.call(value) == arrayTag;
  7908. };
  7909. /**
  7910. * Checks if `value` is classified as a boolean primitive or object.
  7911. *
  7912. * @static
  7913. * @memberOf _
  7914. * @category Lang
  7915. * @param {*} value The value to check.
  7916. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  7917. * @example
  7918. *
  7919. * _.isBoolean(false);
  7920. * // => true
  7921. *
  7922. * _.isBoolean(null);
  7923. * // => false
  7924. */
  7925. function isBoolean(value) {
  7926. return value === true || value === false || (isObjectLike(value) && objToString.call(value) == boolTag);
  7927. }
  7928. /**
  7929. * Checks if `value` is classified as a `Date` object.
  7930. *
  7931. * @static
  7932. * @memberOf _
  7933. * @category Lang
  7934. * @param {*} value The value to check.
  7935. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  7936. * @example
  7937. *
  7938. * _.isDate(new Date);
  7939. * // => true
  7940. *
  7941. * _.isDate('Mon April 23 2012');
  7942. * // => false
  7943. */
  7944. function isDate(value) {
  7945. return isObjectLike(value) && objToString.call(value) == dateTag;
  7946. }
  7947. /**
  7948. * Checks if `value` is a DOM element.
  7949. *
  7950. * @static
  7951. * @memberOf _
  7952. * @category Lang
  7953. * @param {*} value The value to check.
  7954. * @returns {boolean} Returns `true` if `value` is a DOM element, else `false`.
  7955. * @example
  7956. *
  7957. * _.isElement(document.body);
  7958. * // => true
  7959. *
  7960. * _.isElement('<body>');
  7961. * // => false
  7962. */
  7963. function isElement(value) {
  7964. return !!value && value.nodeType === 1 && isObjectLike(value) &&
  7965. (objToString.call(value).indexOf('Element') > -1);
  7966. }
  7967. // Fallback for environments without DOM support.
  7968. if (!support.dom) {
  7969. isElement = function(value) {
  7970. return !!value && value.nodeType === 1 && isObjectLike(value) && !isPlainObject(value);
  7971. };
  7972. }
  7973. /**
  7974. * Checks if `value` is empty. A value is considered empty unless it is an
  7975. * `arguments` object, array, string, or jQuery-like collection with a length
  7976. * greater than `0` or an object with own enumerable properties.
  7977. *
  7978. * @static
  7979. * @memberOf _
  7980. * @category Lang
  7981. * @param {Array|Object|string} value The value to inspect.
  7982. * @returns {boolean} Returns `true` if `value` is empty, else `false`.
  7983. * @example
  7984. *
  7985. * _.isEmpty(null);
  7986. * // => true
  7987. *
  7988. * _.isEmpty(true);
  7989. * // => true
  7990. *
  7991. * _.isEmpty(1);
  7992. * // => true
  7993. *
  7994. * _.isEmpty([1, 2, 3]);
  7995. * // => false
  7996. *
  7997. * _.isEmpty({ 'a': 1 });
  7998. * // => false
  7999. */
  8000. function isEmpty(value) {
  8001. if (value == null) {
  8002. return true;
  8003. }
  8004. if (isArrayLike(value) && (isArray(value) || isString(value) || isArguments(value) ||
  8005. (isObjectLike(value) && isFunction(value.splice)))) {
  8006. return !value.length;
  8007. }
  8008. return !keys(value).length;
  8009. }
  8010. /**
  8011. * Performs a deep comparison between two values to determine if they are
  8012. * equivalent. If `customizer` is provided it is invoked to compare values.
  8013. * If `customizer` returns `undefined` comparisons are handled by the method
  8014. * instead. The `customizer` is bound to `thisArg` and invoked with three
  8015. * arguments: (value, other [, index|key]).
  8016. *
  8017. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  8018. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  8019. * their own, not inherited, enumerable properties. Functions and DOM nodes
  8020. * are **not** supported. Provide a customizer function to extend support
  8021. * for comparing other values.
  8022. *
  8023. * @static
  8024. * @memberOf _
  8025. * @alias eq
  8026. * @category Lang
  8027. * @param {*} value The value to compare.
  8028. * @param {*} other The other value to compare.
  8029. * @param {Function} [customizer] The function to customize value comparisons.
  8030. * @param {*} [thisArg] The `this` binding of `customizer`.
  8031. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  8032. * @example
  8033. *
  8034. * var object = { 'user': 'fred' };
  8035. * var other = { 'user': 'fred' };
  8036. *
  8037. * object == other;
  8038. * // => false
  8039. *
  8040. * _.isEqual(object, other);
  8041. * // => true
  8042. *
  8043. * // using a customizer callback
  8044. * var array = ['hello', 'goodbye'];
  8045. * var other = ['hi', 'goodbye'];
  8046. *
  8047. * _.isEqual(array, other, function(value, other) {
  8048. * if (_.every([value, other], RegExp.prototype.test, /^h(?:i|ello)$/)) {
  8049. * return true;
  8050. * }
  8051. * });
  8052. * // => true
  8053. */
  8054. function isEqual(value, other, customizer, thisArg) {
  8055. customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined;
  8056. var result = customizer ? customizer(value, other) : undefined;
  8057. return result === undefined ? baseIsEqual(value, other, customizer) : !!result;
  8058. }
  8059. /**
  8060. * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`,
  8061. * `SyntaxError`, `TypeError`, or `URIError` object.
  8062. *
  8063. * @static
  8064. * @memberOf _
  8065. * @category Lang
  8066. * @param {*} value The value to check.
  8067. * @returns {boolean} Returns `true` if `value` is an error object, else `false`.
  8068. * @example
  8069. *
  8070. * _.isError(new Error);
  8071. * // => true
  8072. *
  8073. * _.isError(Error);
  8074. * // => false
  8075. */
  8076. function isError(value) {
  8077. return isObjectLike(value) && typeof value.message == 'string' && objToString.call(value) == errorTag;
  8078. }
  8079. /**
  8080. * Checks if `value` is a finite primitive number.
  8081. *
  8082. * **Note:** This method is based on [`Number.isFinite`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isfinite).
  8083. *
  8084. * @static
  8085. * @memberOf _
  8086. * @category Lang
  8087. * @param {*} value The value to check.
  8088. * @returns {boolean} Returns `true` if `value` is a finite number, else `false`.
  8089. * @example
  8090. *
  8091. * _.isFinite(10);
  8092. * // => true
  8093. *
  8094. * _.isFinite('10');
  8095. * // => false
  8096. *
  8097. * _.isFinite(true);
  8098. * // => false
  8099. *
  8100. * _.isFinite(Object(10));
  8101. * // => false
  8102. *
  8103. * _.isFinite(Infinity);
  8104. * // => false
  8105. */
  8106. var isFinite = nativeNumIsFinite || function(value) {
  8107. return typeof value == 'number' && nativeIsFinite(value);
  8108. };
  8109. /**
  8110. * Checks if `value` is classified as a `Function` object.
  8111. *
  8112. * @static
  8113. * @memberOf _
  8114. * @category Lang
  8115. * @param {*} value The value to check.
  8116. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  8117. * @example
  8118. *
  8119. * _.isFunction(_);
  8120. * // => true
  8121. *
  8122. * _.isFunction(/abc/);
  8123. * // => false
  8124. */
  8125. var isFunction = !(baseIsFunction(/x/) || (Uint8Array && !baseIsFunction(Uint8Array))) ? baseIsFunction : function(value) {
  8126. // The use of `Object#toString` avoids issues with the `typeof` operator
  8127. // in older versions of Chrome and Safari which return 'function' for regexes
  8128. // and Safari 8 equivalents which return 'object' for typed array constructors.
  8129. return objToString.call(value) == funcTag;
  8130. };
  8131. /**
  8132. * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
  8133. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  8134. *
  8135. * @static
  8136. * @memberOf _
  8137. * @category Lang
  8138. * @param {*} value The value to check.
  8139. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  8140. * @example
  8141. *
  8142. * _.isObject({});
  8143. * // => true
  8144. *
  8145. * _.isObject([1, 2, 3]);
  8146. * // => true
  8147. *
  8148. * _.isObject(1);
  8149. * // => false
  8150. */
  8151. function isObject(value) {
  8152. // Avoid a V8 JIT bug in Chrome 19-20.
  8153. // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
  8154. var type = typeof value;
  8155. return !!value && (type == 'object' || type == 'function');
  8156. }
  8157. /**
  8158. * Performs a deep comparison between `object` and `source` to determine if
  8159. * `object` contains equivalent property values. If `customizer` is provided
  8160. * it is invoked to compare values. If `customizer` returns `undefined`
  8161. * comparisons are handled by the method instead. The `customizer` is bound
  8162. * to `thisArg` and invoked with three arguments: (value, other, index|key).
  8163. *
  8164. * **Note:** This method supports comparing properties of arrays, booleans,
  8165. * `Date` objects, numbers, `Object` objects, regexes, and strings. Functions
  8166. * and DOM nodes are **not** supported. Provide a customizer function to extend
  8167. * support for comparing other values.
  8168. *
  8169. * @static
  8170. * @memberOf _
  8171. * @category Lang
  8172. * @param {Object} object The object to inspect.
  8173. * @param {Object} source The object of property values to match.
  8174. * @param {Function} [customizer] The function to customize value comparisons.
  8175. * @param {*} [thisArg] The `this` binding of `customizer`.
  8176. * @returns {boolean} Returns `true` if `object` is a match, else `false`.
  8177. * @example
  8178. *
  8179. * var object = { 'user': 'fred', 'age': 40 };
  8180. *
  8181. * _.isMatch(object, { 'age': 40 });
  8182. * // => true
  8183. *
  8184. * _.isMatch(object, { 'age': 36 });
  8185. * // => false
  8186. *
  8187. * // using a customizer callback
  8188. * var object = { 'greeting': 'hello' };
  8189. * var source = { 'greeting': 'hi' };
  8190. *
  8191. * _.isMatch(object, source, function(value, other) {
  8192. * return _.every([value, other], RegExp.prototype.test, /^h(?:i|ello)$/) || undefined;
  8193. * });
  8194. * // => true
  8195. */
  8196. function isMatch(object, source, customizer, thisArg) {
  8197. customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined;
  8198. return baseIsMatch(object, getMatchData(source), customizer);
  8199. }
  8200. /**
  8201. * Checks if `value` is `NaN`.
  8202. *
  8203. * **Note:** This method is not the same as [`isNaN`](https://es5.github.io/#x15.1.2.4)
  8204. * which returns `true` for `undefined` and other non-numeric values.
  8205. *
  8206. * @static
  8207. * @memberOf _
  8208. * @category Lang
  8209. * @param {*} value The value to check.
  8210. * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`.
  8211. * @example
  8212. *
  8213. * _.isNaN(NaN);
  8214. * // => true
  8215. *
  8216. * _.isNaN(new Number(NaN));
  8217. * // => true
  8218. *
  8219. * isNaN(undefined);
  8220. * // => true
  8221. *
  8222. * _.isNaN(undefined);
  8223. * // => false
  8224. */
  8225. function isNaN(value) {
  8226. // An `NaN` primitive is the only value that is not equal to itself.
  8227. // Perform the `toStringTag` check first to avoid errors with some host objects in IE.
  8228. return isNumber(value) && value != +value;
  8229. }
  8230. /**
  8231. * Checks if `value` is a native function.
  8232. *
  8233. * @static
  8234. * @memberOf _
  8235. * @category Lang
  8236. * @param {*} value The value to check.
  8237. * @returns {boolean} Returns `true` if `value` is a native function, else `false`.
  8238. * @example
  8239. *
  8240. * _.isNative(Array.prototype.push);
  8241. * // => true
  8242. *
  8243. * _.isNative(_);
  8244. * // => false
  8245. */
  8246. function isNative(value) {
  8247. if (value == null) {
  8248. return false;
  8249. }
  8250. if (objToString.call(value) == funcTag) {
  8251. return reIsNative.test(fnToString.call(value));
  8252. }
  8253. return isObjectLike(value) && reIsHostCtor.test(value);
  8254. }
  8255. /**
  8256. * Checks if `value` is `null`.
  8257. *
  8258. * @static
  8259. * @memberOf _
  8260. * @category Lang
  8261. * @param {*} value The value to check.
  8262. * @returns {boolean} Returns `true` if `value` is `null`, else `false`.
  8263. * @example
  8264. *
  8265. * _.isNull(null);
  8266. * // => true
  8267. *
  8268. * _.isNull(void 0);
  8269. * // => false
  8270. */
  8271. function isNull(value) {
  8272. return value === null;
  8273. }
  8274. /**
  8275. * Checks if `value` is classified as a `Number` primitive or object.
  8276. *
  8277. * **Note:** To exclude `Infinity`, `-Infinity`, and `NaN`, which are classified
  8278. * as numbers, use the `_.isFinite` method.
  8279. *
  8280. * @static
  8281. * @memberOf _
  8282. * @category Lang
  8283. * @param {*} value The value to check.
  8284. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  8285. * @example
  8286. *
  8287. * _.isNumber(8.4);
  8288. * // => true
  8289. *
  8290. * _.isNumber(NaN);
  8291. * // => true
  8292. *
  8293. * _.isNumber('8.4');
  8294. * // => false
  8295. */
  8296. function isNumber(value) {
  8297. return typeof value == 'number' || (isObjectLike(value) && objToString.call(value) == numberTag);
  8298. }
  8299. /**
  8300. * Checks if `value` is a plain object, that is, an object created by the
  8301. * `Object` constructor or one with a `[[Prototype]]` of `null`.
  8302. *
  8303. * **Note:** This method assumes objects created by the `Object` constructor
  8304. * have no inherited enumerable properties.
  8305. *
  8306. * @static
  8307. * @memberOf _
  8308. * @category Lang
  8309. * @param {*} value The value to check.
  8310. * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
  8311. * @example
  8312. *
  8313. * function Foo() {
  8314. * this.a = 1;
  8315. * }
  8316. *
  8317. * _.isPlainObject(new Foo);
  8318. * // => false
  8319. *
  8320. * _.isPlainObject([1, 2, 3]);
  8321. * // => false
  8322. *
  8323. * _.isPlainObject({ 'x': 0, 'y': 0 });
  8324. * // => true
  8325. *
  8326. * _.isPlainObject(Object.create(null));
  8327. * // => true
  8328. */
  8329. var isPlainObject = !getPrototypeOf ? shimIsPlainObject : function(value) {
  8330. if (!(value && objToString.call(value) == objectTag)) {
  8331. return false;
  8332. }
  8333. var valueOf = getNative(value, 'valueOf'),
  8334. objProto = valueOf && (objProto = getPrototypeOf(valueOf)) && getPrototypeOf(objProto);
  8335. return objProto
  8336. ? (value == objProto || getPrototypeOf(value) == objProto)
  8337. : shimIsPlainObject(value);
  8338. };
  8339. /**
  8340. * Checks if `value` is classified as a `RegExp` object.
  8341. *
  8342. * @static
  8343. * @memberOf _
  8344. * @category Lang
  8345. * @param {*} value The value to check.
  8346. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  8347. * @example
  8348. *
  8349. * _.isRegExp(/abc/);
  8350. * // => true
  8351. *
  8352. * _.isRegExp('/abc/');
  8353. * // => false
  8354. */
  8355. function isRegExp(value) {
  8356. return isObjectLike(value) && objToString.call(value) == regexpTag;
  8357. }
  8358. /**
  8359. * Checks if `value` is classified as a `String` primitive or object.
  8360. *
  8361. * @static
  8362. * @memberOf _
  8363. * @category Lang
  8364. * @param {*} value The value to check.
  8365. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  8366. * @example
  8367. *
  8368. * _.isString('abc');
  8369. * // => true
  8370. *
  8371. * _.isString(1);
  8372. * // => false
  8373. */
  8374. function isString(value) {
  8375. return typeof value == 'string' || (isObjectLike(value) && objToString.call(value) == stringTag);
  8376. }
  8377. /**
  8378. * Checks if `value` is classified as a typed array.
  8379. *
  8380. * @static
  8381. * @memberOf _
  8382. * @category Lang
  8383. * @param {*} value The value to check.
  8384. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  8385. * @example
  8386. *
  8387. * _.isTypedArray(new Uint8Array);
  8388. * // => true
  8389. *
  8390. * _.isTypedArray([]);
  8391. * // => false
  8392. */
  8393. function isTypedArray(value) {
  8394. return isObjectLike(value) && isLength(value.length) && !!typedArrayTags[objToString.call(value)];
  8395. }
  8396. /**
  8397. * Checks if `value` is `undefined`.
  8398. *
  8399. * @static
  8400. * @memberOf _
  8401. * @category Lang
  8402. * @param {*} value The value to check.
  8403. * @returns {boolean} Returns `true` if `value` is `undefined`, else `false`.
  8404. * @example
  8405. *
  8406. * _.isUndefined(void 0);
  8407. * // => true
  8408. *
  8409. * _.isUndefined(null);
  8410. * // => false
  8411. */
  8412. function isUndefined(value) {
  8413. return value === undefined;
  8414. }
  8415. /**
  8416. * Checks if `value` is less than `other`.
  8417. *
  8418. * @static
  8419. * @memberOf _
  8420. * @category Lang
  8421. * @param {*} value The value to compare.
  8422. * @param {*} other The other value to compare.
  8423. * @returns {boolean} Returns `true` if `value` is less than `other`, else `false`.
  8424. * @example
  8425. *
  8426. * _.lt(1, 3);
  8427. * // => true
  8428. *
  8429. * _.lt(3, 3);
  8430. * // => false
  8431. *
  8432. * _.lt(3, 1);
  8433. * // => false
  8434. */
  8435. function lt(value, other) {
  8436. return value < other;
  8437. }
  8438. /**
  8439. * Checks if `value` is less than or equal to `other`.
  8440. *
  8441. * @static
  8442. * @memberOf _
  8443. * @category Lang
  8444. * @param {*} value The value to compare.
  8445. * @param {*} other The other value to compare.
  8446. * @returns {boolean} Returns `true` if `value` is less than or equal to `other`, else `false`.
  8447. * @example
  8448. *
  8449. * _.lte(1, 3);
  8450. * // => true
  8451. *
  8452. * _.lte(3, 3);
  8453. * // => true
  8454. *
  8455. * _.lte(3, 1);
  8456. * // => false
  8457. */
  8458. function lte(value, other) {
  8459. return value <= other;
  8460. }
  8461. /**
  8462. * Converts `value` to an array.
  8463. *
  8464. * @static
  8465. * @memberOf _
  8466. * @category Lang
  8467. * @param {*} value The value to convert.
  8468. * @returns {Array} Returns the converted array.
  8469. * @example
  8470. *
  8471. * (function() {
  8472. * return _.toArray(arguments).slice(1);
  8473. * }(1, 2, 3));
  8474. * // => [2, 3]
  8475. */
  8476. function toArray(value) {
  8477. var length = value ? getLength(value) : 0;
  8478. if (!isLength(length)) {
  8479. return values(value);
  8480. }
  8481. if (!length) {
  8482. return [];
  8483. }
  8484. return arrayCopy(value);
  8485. }
  8486. /**
  8487. * Converts `value` to a plain object flattening inherited enumerable
  8488. * properties of `value` to own properties of the plain object.
  8489. *
  8490. * @static
  8491. * @memberOf _
  8492. * @category Lang
  8493. * @param {*} value The value to convert.
  8494. * @returns {Object} Returns the converted plain object.
  8495. * @example
  8496. *
  8497. * function Foo() {
  8498. * this.b = 2;
  8499. * }
  8500. *
  8501. * Foo.prototype.c = 3;
  8502. *
  8503. * _.assign({ 'a': 1 }, new Foo);
  8504. * // => { 'a': 1, 'b': 2 }
  8505. *
  8506. * _.assign({ 'a': 1 }, _.toPlainObject(new Foo));
  8507. * // => { 'a': 1, 'b': 2, 'c': 3 }
  8508. */
  8509. function toPlainObject(value) {
  8510. return baseCopy(value, keysIn(value));
  8511. }
  8512. /*------------------------------------------------------------------------*/
  8513. /**
  8514. * Assigns own enumerable properties of source object(s) to the destination
  8515. * object. Subsequent sources overwrite property assignments of previous sources.
  8516. * If `customizer` is provided it is invoked to produce the assigned values.
  8517. * The `customizer` is bound to `thisArg` and invoked with five arguments:
  8518. * (objectValue, sourceValue, key, object, source).
  8519. *
  8520. * **Note:** This method mutates `object` and is based on
  8521. * [`Object.assign`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign).
  8522. *
  8523. * @static
  8524. * @memberOf _
  8525. * @alias extend
  8526. * @category Object
  8527. * @param {Object} object The destination object.
  8528. * @param {...Object} [sources] The source objects.
  8529. * @param {Function} [customizer] The function to customize assigned values.
  8530. * @param {*} [thisArg] The `this` binding of `customizer`.
  8531. * @returns {Object} Returns `object`.
  8532. * @example
  8533. *
  8534. * _.assign({ 'user': 'barney' }, { 'age': 40 }, { 'user': 'fred' });
  8535. * // => { 'user': 'fred', 'age': 40 }
  8536. *
  8537. * // using a customizer callback
  8538. * var defaults = _.partialRight(_.assign, function(value, other) {
  8539. * return _.isUndefined(value) ? other : value;
  8540. * });
  8541. *
  8542. * defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
  8543. * // => { 'user': 'barney', 'age': 36 }
  8544. */
  8545. var assign = createAssigner(function(object, source, customizer) {
  8546. return customizer
  8547. ? assignWith(object, source, customizer)
  8548. : baseAssign(object, source);
  8549. });
  8550. /**
  8551. * Creates an object that inherits from the given `prototype` object. If a
  8552. * `properties` object is provided its own enumerable properties are assigned
  8553. * to the created object.
  8554. *
  8555. * @static
  8556. * @memberOf _
  8557. * @category Object
  8558. * @param {Object} prototype The object to inherit from.
  8559. * @param {Object} [properties] The properties to assign to the object.
  8560. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  8561. * @returns {Object} Returns the new object.
  8562. * @example
  8563. *
  8564. * function Shape() {
  8565. * this.x = 0;
  8566. * this.y = 0;
  8567. * }
  8568. *
  8569. * function Circle() {
  8570. * Shape.call(this);
  8571. * }
  8572. *
  8573. * Circle.prototype = _.create(Shape.prototype, {
  8574. * 'constructor': Circle
  8575. * });
  8576. *
  8577. * var circle = new Circle;
  8578. * circle instanceof Circle;
  8579. * // => true
  8580. *
  8581. * circle instanceof Shape;
  8582. * // => true
  8583. */
  8584. function create(prototype, properties, guard) {
  8585. var result = baseCreate(prototype);
  8586. if (guard && isIterateeCall(prototype, properties, guard)) {
  8587. properties = null;
  8588. }
  8589. return properties ? baseAssign(result, properties) : result;
  8590. }
  8591. /**
  8592. * Assigns own enumerable properties of source object(s) to the destination
  8593. * object for all destination properties that resolve to `undefined`. Once a
  8594. * property is set, additional values of the same property are ignored.
  8595. *
  8596. * **Note:** This method mutates `object`.
  8597. *
  8598. * @static
  8599. * @memberOf _
  8600. * @category Object
  8601. * @param {Object} object The destination object.
  8602. * @param {...Object} [sources] The source objects.
  8603. * @returns {Object} Returns `object`.
  8604. * @example
  8605. *
  8606. * _.defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
  8607. * // => { 'user': 'barney', 'age': 36 }
  8608. */
  8609. var defaults = restParam(function(args) {
  8610. var object = args[0];
  8611. if (object == null) {
  8612. return object;
  8613. }
  8614. args.push(assignDefaults);
  8615. return assign.apply(undefined, args);
  8616. });
  8617. /**
  8618. * This method is like `_.find` except that it returns the key of the first
  8619. * element `predicate` returns truthy for instead of the element itself.
  8620. *
  8621. * If a property name is provided for `predicate` the created `_.property`
  8622. * style callback returns the property value of the given element.
  8623. *
  8624. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  8625. * style callback returns `true` for elements that have a matching property
  8626. * value, else `false`.
  8627. *
  8628. * If an object is provided for `predicate` the created `_.matches` style
  8629. * callback returns `true` for elements that have the properties of the given
  8630. * object, else `false`.
  8631. *
  8632. * @static
  8633. * @memberOf _
  8634. * @category Object
  8635. * @param {Object} object The object to search.
  8636. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  8637. * per iteration.
  8638. * @param {*} [thisArg] The `this` binding of `predicate`.
  8639. * @returns {string|undefined} Returns the key of the matched element, else `undefined`.
  8640. * @example
  8641. *
  8642. * var users = {
  8643. * 'barney': { 'age': 36, 'active': true },
  8644. * 'fred': { 'age': 40, 'active': false },
  8645. * 'pebbles': { 'age': 1, 'active': true }
  8646. * };
  8647. *
  8648. * _.findKey(users, function(chr) {
  8649. * return chr.age < 40;
  8650. * });
  8651. * // => 'barney' (iteration order is not guaranteed)
  8652. *
  8653. * // using the `_.matches` callback shorthand
  8654. * _.findKey(users, { 'age': 1, 'active': true });
  8655. * // => 'pebbles'
  8656. *
  8657. * // using the `_.matchesProperty` callback shorthand
  8658. * _.findKey(users, 'active', false);
  8659. * // => 'fred'
  8660. *
  8661. * // using the `_.property` callback shorthand
  8662. * _.findKey(users, 'active');
  8663. * // => 'barney'
  8664. */
  8665. var findKey = createFindKey(baseForOwn);
  8666. /**
  8667. * This method is like `_.findKey` except that it iterates over elements of
  8668. * a collection in the opposite order.
  8669. *
  8670. * If a property name is provided for `predicate` the created `_.property`
  8671. * style callback returns the property value of the given element.
  8672. *
  8673. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  8674. * style callback returns `true` for elements that have a matching property
  8675. * value, else `false`.
  8676. *
  8677. * If an object is provided for `predicate` the created `_.matches` style
  8678. * callback returns `true` for elements that have the properties of the given
  8679. * object, else `false`.
  8680. *
  8681. * @static
  8682. * @memberOf _
  8683. * @category Object
  8684. * @param {Object} object The object to search.
  8685. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  8686. * per iteration.
  8687. * @param {*} [thisArg] The `this` binding of `predicate`.
  8688. * @returns {string|undefined} Returns the key of the matched element, else `undefined`.
  8689. * @example
  8690. *
  8691. * var users = {
  8692. * 'barney': { 'age': 36, 'active': true },
  8693. * 'fred': { 'age': 40, 'active': false },
  8694. * 'pebbles': { 'age': 1, 'active': true }
  8695. * };
  8696. *
  8697. * _.findLastKey(users, function(chr) {
  8698. * return chr.age < 40;
  8699. * });
  8700. * // => returns `pebbles` assuming `_.findKey` returns `barney`
  8701. *
  8702. * // using the `_.matches` callback shorthand
  8703. * _.findLastKey(users, { 'age': 36, 'active': true });
  8704. * // => 'barney'
  8705. *
  8706. * // using the `_.matchesProperty` callback shorthand
  8707. * _.findLastKey(users, 'active', false);
  8708. * // => 'fred'
  8709. *
  8710. * // using the `_.property` callback shorthand
  8711. * _.findLastKey(users, 'active');
  8712. * // => 'pebbles'
  8713. */
  8714. var findLastKey = createFindKey(baseForOwnRight);
  8715. /**
  8716. * Iterates over own and inherited enumerable properties of an object invoking
  8717. * `iteratee` for each property. The `iteratee` is bound to `thisArg` and invoked
  8718. * with three arguments: (value, key, object). Iteratee functions may exit
  8719. * iteration early by explicitly returning `false`.
  8720. *
  8721. * @static
  8722. * @memberOf _
  8723. * @category Object
  8724. * @param {Object} object The object to iterate over.
  8725. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8726. * @param {*} [thisArg] The `this` binding of `iteratee`.
  8727. * @returns {Object} Returns `object`.
  8728. * @example
  8729. *
  8730. * function Foo() {
  8731. * this.a = 1;
  8732. * this.b = 2;
  8733. * }
  8734. *
  8735. * Foo.prototype.c = 3;
  8736. *
  8737. * _.forIn(new Foo, function(value, key) {
  8738. * console.log(key);
  8739. * });
  8740. * // => logs 'a', 'b', and 'c' (iteration order is not guaranteed)
  8741. */
  8742. var forIn = createForIn(baseFor);
  8743. /**
  8744. * This method is like `_.forIn` except that it iterates over properties of
  8745. * `object` in the opposite order.
  8746. *
  8747. * @static
  8748. * @memberOf _
  8749. * @category Object
  8750. * @param {Object} object The object to iterate over.
  8751. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8752. * @param {*} [thisArg] The `this` binding of `iteratee`.
  8753. * @returns {Object} Returns `object`.
  8754. * @example
  8755. *
  8756. * function Foo() {
  8757. * this.a = 1;
  8758. * this.b = 2;
  8759. * }
  8760. *
  8761. * Foo.prototype.c = 3;
  8762. *
  8763. * _.forInRight(new Foo, function(value, key) {
  8764. * console.log(key);
  8765. * });
  8766. * // => logs 'c', 'b', and 'a' assuming `_.forIn ` logs 'a', 'b', and 'c'
  8767. */
  8768. var forInRight = createForIn(baseForRight);
  8769. /**
  8770. * Iterates over own enumerable properties of an object invoking `iteratee`
  8771. * for each property. The `iteratee` is bound to `thisArg` and invoked with
  8772. * three arguments: (value, key, object). Iteratee functions may exit iteration
  8773. * early by explicitly returning `false`.
  8774. *
  8775. * @static
  8776. * @memberOf _
  8777. * @category Object
  8778. * @param {Object} object The object to iterate over.
  8779. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8780. * @param {*} [thisArg] The `this` binding of `iteratee`.
  8781. * @returns {Object} Returns `object`.
  8782. * @example
  8783. *
  8784. * function Foo() {
  8785. * this.a = 1;
  8786. * this.b = 2;
  8787. * }
  8788. *
  8789. * Foo.prototype.c = 3;
  8790. *
  8791. * _.forOwn(new Foo, function(value, key) {
  8792. * console.log(key);
  8793. * });
  8794. * // => logs 'a' and 'b' (iteration order is not guaranteed)
  8795. */
  8796. var forOwn = createForOwn(baseForOwn);
  8797. /**
  8798. * This method is like `_.forOwn` except that it iterates over properties of
  8799. * `object` in the opposite order.
  8800. *
  8801. * @static
  8802. * @memberOf _
  8803. * @category Object
  8804. * @param {Object} object The object to iterate over.
  8805. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8806. * @param {*} [thisArg] The `this` binding of `iteratee`.
  8807. * @returns {Object} Returns `object`.
  8808. * @example
  8809. *
  8810. * function Foo() {
  8811. * this.a = 1;
  8812. * this.b = 2;
  8813. * }
  8814. *
  8815. * Foo.prototype.c = 3;
  8816. *
  8817. * _.forOwnRight(new Foo, function(value, key) {
  8818. * console.log(key);
  8819. * });
  8820. * // => logs 'b' and 'a' assuming `_.forOwn` logs 'a' and 'b'
  8821. */
  8822. var forOwnRight = createForOwn(baseForOwnRight);
  8823. /**
  8824. * Creates an array of function property names from all enumerable properties,
  8825. * own and inherited, of `object`.
  8826. *
  8827. * @static
  8828. * @memberOf _
  8829. * @alias methods
  8830. * @category Object
  8831. * @param {Object} object The object to inspect.
  8832. * @returns {Array} Returns the new array of property names.
  8833. * @example
  8834. *
  8835. * _.functions(_);
  8836. * // => ['after', 'ary', 'assign', ...]
  8837. */
  8838. function functions(object) {
  8839. return baseFunctions(object, keysIn(object));
  8840. }
  8841. /**
  8842. * Gets the property value at `path` of `object`. If the resolved value is
  8843. * `undefined` the `defaultValue` is used in its place.
  8844. *
  8845. * @static
  8846. * @memberOf _
  8847. * @category Object
  8848. * @param {Object} object The object to query.
  8849. * @param {Array|string} path The path of the property to get.
  8850. * @param {*} [defaultValue] The value returned if the resolved value is `undefined`.
  8851. * @returns {*} Returns the resolved value.
  8852. * @example
  8853. *
  8854. * var object = { 'a': [{ 'b': { 'c': 3 } }] };
  8855. *
  8856. * _.get(object, 'a[0].b.c');
  8857. * // => 3
  8858. *
  8859. * _.get(object, ['a', '0', 'b', 'c']);
  8860. * // => 3
  8861. *
  8862. * _.get(object, 'a.b.c', 'default');
  8863. * // => 'default'
  8864. */
  8865. function get(object, path, defaultValue) {
  8866. var result = object == null ? undefined : baseGet(object, toPath(path), path + '');
  8867. return result === undefined ? defaultValue : result;
  8868. }
  8869. /**
  8870. * Checks if `path` is a direct property.
  8871. *
  8872. * @static
  8873. * @memberOf _
  8874. * @category Object
  8875. * @param {Object} object The object to query.
  8876. * @param {Array|string} path The path to check.
  8877. * @returns {boolean} Returns `true` if `path` is a direct property, else `false`.
  8878. * @example
  8879. *
  8880. * var object = { 'a': { 'b': { 'c': 3 } } };
  8881. *
  8882. * _.has(object, 'a');
  8883. * // => true
  8884. *
  8885. * _.has(object, 'a.b.c');
  8886. * // => true
  8887. *
  8888. * _.has(object, ['a', 'b', 'c']);
  8889. * // => true
  8890. */
  8891. function has(object, path) {
  8892. if (object == null) {
  8893. return false;
  8894. }
  8895. var result = hasOwnProperty.call(object, path);
  8896. if (!result && !isKey(path)) {
  8897. path = toPath(path);
  8898. object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  8899. if (object == null) {
  8900. return false;
  8901. }
  8902. path = last(path);
  8903. result = hasOwnProperty.call(object, path);
  8904. }
  8905. return result || (isLength(object.length) && isIndex(path, object.length) &&
  8906. (isArray(object) || isArguments(object)));
  8907. }
  8908. /**
  8909. * Creates an object composed of the inverted keys and values of `object`.
  8910. * If `object` contains duplicate values, subsequent values overwrite property
  8911. * assignments of previous values unless `multiValue` is `true`.
  8912. *
  8913. * @static
  8914. * @memberOf _
  8915. * @category Object
  8916. * @param {Object} object The object to invert.
  8917. * @param {boolean} [multiValue] Allow multiple values per key.
  8918. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  8919. * @returns {Object} Returns the new inverted object.
  8920. * @example
  8921. *
  8922. * var object = { 'a': 1, 'b': 2, 'c': 1 };
  8923. *
  8924. * _.invert(object);
  8925. * // => { '1': 'c', '2': 'b' }
  8926. *
  8927. * // with `multiValue`
  8928. * _.invert(object, true);
  8929. * // => { '1': ['a', 'c'], '2': ['b'] }
  8930. */
  8931. function invert(object, multiValue, guard) {
  8932. if (guard && isIterateeCall(object, multiValue, guard)) {
  8933. multiValue = null;
  8934. }
  8935. var index = -1,
  8936. props = keys(object),
  8937. length = props.length,
  8938. result = {};
  8939. while (++index < length) {
  8940. var key = props[index],
  8941. value = object[key];
  8942. if (multiValue) {
  8943. if (hasOwnProperty.call(result, value)) {
  8944. result[value].push(key);
  8945. } else {
  8946. result[value] = [key];
  8947. }
  8948. }
  8949. else {
  8950. result[value] = key;
  8951. }
  8952. }
  8953. return result;
  8954. }
  8955. /**
  8956. * Creates an array of the own enumerable property names of `object`.
  8957. *
  8958. * **Note:** Non-object values are coerced to objects. See the
  8959. * [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.keys)
  8960. * for more details.
  8961. *
  8962. * @static
  8963. * @memberOf _
  8964. * @category Object
  8965. * @param {Object} object The object to query.
  8966. * @returns {Array} Returns the array of property names.
  8967. * @example
  8968. *
  8969. * function Foo() {
  8970. * this.a = 1;
  8971. * this.b = 2;
  8972. * }
  8973. *
  8974. * Foo.prototype.c = 3;
  8975. *
  8976. * _.keys(new Foo);
  8977. * // => ['a', 'b'] (iteration order is not guaranteed)
  8978. *
  8979. * _.keys('hi');
  8980. * // => ['0', '1']
  8981. */
  8982. var keys = !nativeKeys ? shimKeys : function(object) {
  8983. var Ctor = object == null ? null : object.constructor;
  8984. if ((typeof Ctor == 'function' && Ctor.prototype === object) ||
  8985. (typeof object != 'function' && isArrayLike(object))) {
  8986. return shimKeys(object);
  8987. }
  8988. return isObject(object) ? nativeKeys(object) : [];
  8989. };
  8990. /**
  8991. * Creates an array of the own and inherited enumerable property names of `object`.
  8992. *
  8993. * **Note:** Non-object values are coerced to objects.
  8994. *
  8995. * @static
  8996. * @memberOf _
  8997. * @category Object
  8998. * @param {Object} object The object to query.
  8999. * @returns {Array} Returns the array of property names.
  9000. * @example
  9001. *
  9002. * function Foo() {
  9003. * this.a = 1;
  9004. * this.b = 2;
  9005. * }
  9006. *
  9007. * Foo.prototype.c = 3;
  9008. *
  9009. * _.keysIn(new Foo);
  9010. * // => ['a', 'b', 'c'] (iteration order is not guaranteed)
  9011. */
  9012. function keysIn(object) {
  9013. if (object == null) {
  9014. return [];
  9015. }
  9016. if (!isObject(object)) {
  9017. object = Object(object);
  9018. }
  9019. var length = object.length;
  9020. length = (length && isLength(length) &&
  9021. (isArray(object) || isArguments(object)) && length) || 0;
  9022. var Ctor = object.constructor,
  9023. index = -1,
  9024. isProto = typeof Ctor == 'function' && Ctor.prototype === object,
  9025. result = Array(length),
  9026. skipIndexes = length > 0;
  9027. while (++index < length) {
  9028. result[index] = (index + '');
  9029. }
  9030. for (var key in object) {
  9031. if (!(skipIndexes && isIndex(key, length)) &&
  9032. !(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) {
  9033. result.push(key);
  9034. }
  9035. }
  9036. return result;
  9037. }
  9038. /**
  9039. * The opposite of `_.mapValues`; this method creates an object with the
  9040. * same values as `object` and keys generated by running each own enumerable
  9041. * property of `object` through `iteratee`.
  9042. *
  9043. * @static
  9044. * @memberOf _
  9045. * @category Object
  9046. * @param {Object} object The object to iterate over.
  9047. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  9048. * per iteration.
  9049. * @param {*} [thisArg] The `this` binding of `iteratee`.
  9050. * @returns {Object} Returns the new mapped object.
  9051. * @example
  9052. *
  9053. * _.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) {
  9054. * return key + value;
  9055. * });
  9056. * // => { 'a1': 1, 'b2': 2 }
  9057. */
  9058. var mapKeys = createObjectMapper(true);
  9059. /**
  9060. * Creates an object with the same keys as `object` and values generated by
  9061. * running each own enumerable property of `object` through `iteratee`. The
  9062. * iteratee function is bound to `thisArg` and invoked with three arguments:
  9063. * (value, key, object).
  9064. *
  9065. * If a property name is provided for `iteratee` the created `_.property`
  9066. * style callback returns the property value of the given element.
  9067. *
  9068. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  9069. * style callback returns `true` for elements that have a matching property
  9070. * value, else `false`.
  9071. *
  9072. * If an object is provided for `iteratee` the created `_.matches` style
  9073. * callback returns `true` for elements that have the properties of the given
  9074. * object, else `false`.
  9075. *
  9076. * @static
  9077. * @memberOf _
  9078. * @category Object
  9079. * @param {Object} object The object to iterate over.
  9080. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  9081. * per iteration.
  9082. * @param {*} [thisArg] The `this` binding of `iteratee`.
  9083. * @returns {Object} Returns the new mapped object.
  9084. * @example
  9085. *
  9086. * _.mapValues({ 'a': 1, 'b': 2 }, function(n) {
  9087. * return n * 3;
  9088. * });
  9089. * // => { 'a': 3, 'b': 6 }
  9090. *
  9091. * var users = {
  9092. * 'fred': { 'user': 'fred', 'age': 40 },
  9093. * 'pebbles': { 'user': 'pebbles', 'age': 1 }
  9094. * };
  9095. *
  9096. * // using the `_.property` callback shorthand
  9097. * _.mapValues(users, 'age');
  9098. * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
  9099. */
  9100. var mapValues = createObjectMapper();
  9101. /**
  9102. * Recursively merges own enumerable properties of the source object(s), that
  9103. * don't resolve to `undefined` into the destination object. Subsequent sources
  9104. * overwrite property assignments of previous sources. If `customizer` is
  9105. * provided it is invoked to produce the merged values of the destination and
  9106. * source properties. If `customizer` returns `undefined` merging is handled
  9107. * by the method instead. The `customizer` is bound to `thisArg` and invoked
  9108. * with five arguments: (objectValue, sourceValue, key, object, source).
  9109. *
  9110. * @static
  9111. * @memberOf _
  9112. * @category Object
  9113. * @param {Object} object The destination object.
  9114. * @param {...Object} [sources] The source objects.
  9115. * @param {Function} [customizer] The function to customize assigned values.
  9116. * @param {*} [thisArg] The `this` binding of `customizer`.
  9117. * @returns {Object} Returns `object`.
  9118. * @example
  9119. *
  9120. * var users = {
  9121. * 'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
  9122. * };
  9123. *
  9124. * var ages = {
  9125. * 'data': [{ 'age': 36 }, { 'age': 40 }]
  9126. * };
  9127. *
  9128. * _.merge(users, ages);
  9129. * // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }
  9130. *
  9131. * // using a customizer callback
  9132. * var object = {
  9133. * 'fruits': ['apple'],
  9134. * 'vegetables': ['beet']
  9135. * };
  9136. *
  9137. * var other = {
  9138. * 'fruits': ['banana'],
  9139. * 'vegetables': ['carrot']
  9140. * };
  9141. *
  9142. * _.merge(object, other, function(a, b) {
  9143. * if (_.isArray(a)) {
  9144. * return a.concat(b);
  9145. * }
  9146. * });
  9147. * // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] }
  9148. */
  9149. var merge = createAssigner(baseMerge);
  9150. /**
  9151. * The opposite of `_.pick`; this method creates an object composed of the
  9152. * own and inherited enumerable properties of `object` that are not omitted.
  9153. *
  9154. * @static
  9155. * @memberOf _
  9156. * @category Object
  9157. * @param {Object} object The source object.
  9158. * @param {Function|...(string|string[])} [predicate] The function invoked per
  9159. * iteration or property names to omit, specified as individual property
  9160. * names or arrays of property names.
  9161. * @param {*} [thisArg] The `this` binding of `predicate`.
  9162. * @returns {Object} Returns the new object.
  9163. * @example
  9164. *
  9165. * var object = { 'user': 'fred', 'age': 40 };
  9166. *
  9167. * _.omit(object, 'age');
  9168. * // => { 'user': 'fred' }
  9169. *
  9170. * _.omit(object, _.isNumber);
  9171. * // => { 'user': 'fred' }
  9172. */
  9173. var omit = restParam(function(object, props) {
  9174. if (object == null) {
  9175. return {};
  9176. }
  9177. if (typeof props[0] != 'function') {
  9178. var props = arrayMap(baseFlatten(props), String);
  9179. return pickByArray(object, baseDifference(keysIn(object), props));
  9180. }
  9181. var predicate = bindCallback(props[0], props[1], 3);
  9182. return pickByCallback(object, function(value, key, object) {
  9183. return !predicate(value, key, object);
  9184. });
  9185. });
  9186. /**
  9187. * Creates a two dimensional array of the key-value pairs for `object`,
  9188. * e.g. `[[key1, value1], [key2, value2]]`.
  9189. *
  9190. * @static
  9191. * @memberOf _
  9192. * @category Object
  9193. * @param {Object} object The object to query.
  9194. * @returns {Array} Returns the new array of key-value pairs.
  9195. * @example
  9196. *
  9197. * _.pairs({ 'barney': 36, 'fred': 40 });
  9198. * // => [['barney', 36], ['fred', 40]] (iteration order is not guaranteed)
  9199. */
  9200. function pairs(object) {
  9201. object = toObject(object);
  9202. var index = -1,
  9203. props = keys(object),
  9204. length = props.length,
  9205. result = Array(length);
  9206. while (++index < length) {
  9207. var key = props[index];
  9208. result[index] = [key, object[key]];
  9209. }
  9210. return result;
  9211. }
  9212. /**
  9213. * Creates an object composed of the picked `object` properties. Property
  9214. * names may be specified as individual arguments or as arrays of property
  9215. * names. If `predicate` is provided it is invoked for each property of `object`
  9216. * picking the properties `predicate` returns truthy for. The predicate is
  9217. * bound to `thisArg` and invoked with three arguments: (value, key, object).
  9218. *
  9219. * @static
  9220. * @memberOf _
  9221. * @category Object
  9222. * @param {Object} object The source object.
  9223. * @param {Function|...(string|string[])} [predicate] The function invoked per
  9224. * iteration or property names to pick, specified as individual property
  9225. * names or arrays of property names.
  9226. * @param {*} [thisArg] The `this` binding of `predicate`.
  9227. * @returns {Object} Returns the new object.
  9228. * @example
  9229. *
  9230. * var object = { 'user': 'fred', 'age': 40 };
  9231. *
  9232. * _.pick(object, 'user');
  9233. * // => { 'user': 'fred' }
  9234. *
  9235. * _.pick(object, _.isString);
  9236. * // => { 'user': 'fred' }
  9237. */
  9238. var pick = restParam(function(object, props) {
  9239. if (object == null) {
  9240. return {};
  9241. }
  9242. return typeof props[0] == 'function'
  9243. ? pickByCallback(object, bindCallback(props[0], props[1], 3))
  9244. : pickByArray(object, baseFlatten(props));
  9245. });
  9246. /**
  9247. * This method is like `_.get` except that if the resolved value is a function
  9248. * it is invoked with the `this` binding of its parent object and its result
  9249. * is returned.
  9250. *
  9251. * @static
  9252. * @memberOf _
  9253. * @category Object
  9254. * @param {Object} object The object to query.
  9255. * @param {Array|string} path The path of the property to resolve.
  9256. * @param {*} [defaultValue] The value returned if the resolved value is `undefined`.
  9257. * @returns {*} Returns the resolved value.
  9258. * @example
  9259. *
  9260. * var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
  9261. *
  9262. * _.result(object, 'a[0].b.c1');
  9263. * // => 3
  9264. *
  9265. * _.result(object, 'a[0].b.c2');
  9266. * // => 4
  9267. *
  9268. * _.result(object, 'a.b.c', 'default');
  9269. * // => 'default'
  9270. *
  9271. * _.result(object, 'a.b.c', _.constant('default'));
  9272. * // => 'default'
  9273. */
  9274. function result(object, path, defaultValue) {
  9275. var result = object == null ? undefined : object[path];
  9276. if (result === undefined) {
  9277. if (object != null && !isKey(path, object)) {
  9278. path = toPath(path);
  9279. object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  9280. result = object == null ? undefined : object[last(path)];
  9281. }
  9282. result = result === undefined ? defaultValue : result;
  9283. }
  9284. return isFunction(result) ? result.call(object) : result;
  9285. }
  9286. /**
  9287. * Sets the property value of `path` on `object`. If a portion of `path`
  9288. * does not exist it is created.
  9289. *
  9290. * @static
  9291. * @memberOf _
  9292. * @category Object
  9293. * @param {Object} object The object to augment.
  9294. * @param {Array|string} path The path of the property to set.
  9295. * @param {*} value The value to set.
  9296. * @returns {Object} Returns `object`.
  9297. * @example
  9298. *
  9299. * var object = { 'a': [{ 'b': { 'c': 3 } }] };
  9300. *
  9301. * _.set(object, 'a[0].b.c', 4);
  9302. * console.log(object.a[0].b.c);
  9303. * // => 4
  9304. *
  9305. * _.set(object, 'x[0].y.z', 5);
  9306. * console.log(object.x[0].y.z);
  9307. * // => 5
  9308. */
  9309. function set(object, path, value) {
  9310. if (object == null) {
  9311. return object;
  9312. }
  9313. var pathKey = (path + '');
  9314. path = (object[pathKey] != null || isKey(path, object)) ? [pathKey] : toPath(path);
  9315. var index = -1,
  9316. length = path.length,
  9317. lastIndex = length - 1,
  9318. nested = object;
  9319. while (nested != null && ++index < length) {
  9320. var key = path[index];
  9321. if (isObject(nested)) {
  9322. if (index == lastIndex) {
  9323. nested[key] = value;
  9324. } else if (nested[key] == null) {
  9325. nested[key] = isIndex(path[index + 1]) ? [] : {};
  9326. }
  9327. }
  9328. nested = nested[key];
  9329. }
  9330. return object;
  9331. }
  9332. /**
  9333. * An alternative to `_.reduce`; this method transforms `object` to a new
  9334. * `accumulator` object which is the result of running each of its own enumerable
  9335. * properties through `iteratee`, with each invocation potentially mutating
  9336. * the `accumulator` object. The `iteratee` is bound to `thisArg` and invoked
  9337. * with four arguments: (accumulator, value, key, object). Iteratee functions
  9338. * may exit iteration early by explicitly returning `false`.
  9339. *
  9340. * @static
  9341. * @memberOf _
  9342. * @category Object
  9343. * @param {Array|Object} object The object to iterate over.
  9344. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  9345. * @param {*} [accumulator] The custom accumulator value.
  9346. * @param {*} [thisArg] The `this` binding of `iteratee`.
  9347. * @returns {*} Returns the accumulated value.
  9348. * @example
  9349. *
  9350. * _.transform([2, 3, 4], function(result, n) {
  9351. * result.push(n *= n);
  9352. * return n % 2 == 0;
  9353. * });
  9354. * // => [4, 9]
  9355. *
  9356. * _.transform({ 'a': 1, 'b': 2 }, function(result, n, key) {
  9357. * result[key] = n * 3;
  9358. * });
  9359. * // => { 'a': 3, 'b': 6 }
  9360. */
  9361. function transform(object, iteratee, accumulator, thisArg) {
  9362. var isArr = isArray(object) || isTypedArray(object);
  9363. iteratee = getCallback(iteratee, thisArg, 4);
  9364. if (accumulator == null) {
  9365. if (isArr || isObject(object)) {
  9366. var Ctor = object.constructor;
  9367. if (isArr) {
  9368. accumulator = isArray(object) ? new Ctor : [];
  9369. } else {
  9370. accumulator = baseCreate(isFunction(Ctor) ? Ctor.prototype : null);
  9371. }
  9372. } else {
  9373. accumulator = {};
  9374. }
  9375. }
  9376. (isArr ? arrayEach : baseForOwn)(object, function(value, index, object) {
  9377. return iteratee(accumulator, value, index, object);
  9378. });
  9379. return accumulator;
  9380. }
  9381. /**
  9382. * Creates an array of the own enumerable property values of `object`.
  9383. *
  9384. * **Note:** Non-object values are coerced to objects.
  9385. *
  9386. * @static
  9387. * @memberOf _
  9388. * @category Object
  9389. * @param {Object} object The object to query.
  9390. * @returns {Array} Returns the array of property values.
  9391. * @example
  9392. *
  9393. * function Foo() {
  9394. * this.a = 1;
  9395. * this.b = 2;
  9396. * }
  9397. *
  9398. * Foo.prototype.c = 3;
  9399. *
  9400. * _.values(new Foo);
  9401. * // => [1, 2] (iteration order is not guaranteed)
  9402. *
  9403. * _.values('hi');
  9404. * // => ['h', 'i']
  9405. */
  9406. function values(object) {
  9407. return baseValues(object, keys(object));
  9408. }
  9409. /**
  9410. * Creates an array of the own and inherited enumerable property values
  9411. * of `object`.
  9412. *
  9413. * **Note:** Non-object values are coerced to objects.
  9414. *
  9415. * @static
  9416. * @memberOf _
  9417. * @category Object
  9418. * @param {Object} object The object to query.
  9419. * @returns {Array} Returns the array of property values.
  9420. * @example
  9421. *
  9422. * function Foo() {
  9423. * this.a = 1;
  9424. * this.b = 2;
  9425. * }
  9426. *
  9427. * Foo.prototype.c = 3;
  9428. *
  9429. * _.valuesIn(new Foo);
  9430. * // => [1, 2, 3] (iteration order is not guaranteed)
  9431. */
  9432. function valuesIn(object) {
  9433. return baseValues(object, keysIn(object));
  9434. }
  9435. /*------------------------------------------------------------------------*/
  9436. /**
  9437. * Checks if `n` is between `start` and up to but not including, `end`. If
  9438. * `end` is not specified it is set to `start` with `start` then set to `0`.
  9439. *
  9440. * @static
  9441. * @memberOf _
  9442. * @category Number
  9443. * @param {number} n The number to check.
  9444. * @param {number} [start=0] The start of the range.
  9445. * @param {number} end The end of the range.
  9446. * @returns {boolean} Returns `true` if `n` is in the range, else `false`.
  9447. * @example
  9448. *
  9449. * _.inRange(3, 2, 4);
  9450. * // => true
  9451. *
  9452. * _.inRange(4, 8);
  9453. * // => true
  9454. *
  9455. * _.inRange(4, 2);
  9456. * // => false
  9457. *
  9458. * _.inRange(2, 2);
  9459. * // => false
  9460. *
  9461. * _.inRange(1.2, 2);
  9462. * // => true
  9463. *
  9464. * _.inRange(5.2, 4);
  9465. * // => false
  9466. */
  9467. function inRange(value, start, end) {
  9468. start = +start || 0;
  9469. if (typeof end === 'undefined') {
  9470. end = start;
  9471. start = 0;
  9472. } else {
  9473. end = +end || 0;
  9474. }
  9475. return value >= nativeMin(start, end) && value < nativeMax(start, end);
  9476. }
  9477. /**
  9478. * Produces a random number between `min` and `max` (inclusive). If only one
  9479. * argument is provided a number between `0` and the given number is returned.
  9480. * If `floating` is `true`, or either `min` or `max` are floats, a floating-point
  9481. * number is returned instead of an integer.
  9482. *
  9483. * @static
  9484. * @memberOf _
  9485. * @category Number
  9486. * @param {number} [min=0] The minimum possible value.
  9487. * @param {number} [max=1] The maximum possible value.
  9488. * @param {boolean} [floating] Specify returning a floating-point number.
  9489. * @returns {number} Returns the random number.
  9490. * @example
  9491. *
  9492. * _.random(0, 5);
  9493. * // => an integer between 0 and 5
  9494. *
  9495. * _.random(5);
  9496. * // => also an integer between 0 and 5
  9497. *
  9498. * _.random(5, true);
  9499. * // => a floating-point number between 0 and 5
  9500. *
  9501. * _.random(1.2, 5.2);
  9502. * // => a floating-point number between 1.2 and 5.2
  9503. */
  9504. function random(min, max, floating) {
  9505. if (floating && isIterateeCall(min, max, floating)) {
  9506. max = floating = null;
  9507. }
  9508. var noMin = min == null,
  9509. noMax = max == null;
  9510. if (floating == null) {
  9511. if (noMax && typeof min == 'boolean') {
  9512. floating = min;
  9513. min = 1;
  9514. }
  9515. else if (typeof max == 'boolean') {
  9516. floating = max;
  9517. noMax = true;
  9518. }
  9519. }
  9520. if (noMin && noMax) {
  9521. max = 1;
  9522. noMax = false;
  9523. }
  9524. min = +min || 0;
  9525. if (noMax) {
  9526. max = min;
  9527. min = 0;
  9528. } else {
  9529. max = +max || 0;
  9530. }
  9531. if (floating || min % 1 || max % 1) {
  9532. var rand = nativeRandom();
  9533. return nativeMin(min + (rand * (max - min + parseFloat('1e-' + ((rand + '').length - 1)))), max);
  9534. }
  9535. return baseRandom(min, max);
  9536. }
  9537. /*------------------------------------------------------------------------*/
  9538. /**
  9539. * Converts `string` to [camel case](https://en.wikipedia.org/wiki/CamelCase).
  9540. *
  9541. * @static
  9542. * @memberOf _
  9543. * @category String
  9544. * @param {string} [string=''] The string to convert.
  9545. * @returns {string} Returns the camel cased string.
  9546. * @example
  9547. *
  9548. * _.camelCase('Foo Bar');
  9549. * // => 'fooBar'
  9550. *
  9551. * _.camelCase('--foo-bar');
  9552. * // => 'fooBar'
  9553. *
  9554. * _.camelCase('__foo_bar__');
  9555. * // => 'fooBar'
  9556. */
  9557. var camelCase = createCompounder(function(result, word, index) {
  9558. word = word.toLowerCase();
  9559. return result + (index ? (word.charAt(0).toUpperCase() + word.slice(1)) : word);
  9560. });
  9561. /**
  9562. * Capitalizes the first character of `string`.
  9563. *
  9564. * @static
  9565. * @memberOf _
  9566. * @category String
  9567. * @param {string} [string=''] The string to capitalize.
  9568. * @returns {string} Returns the capitalized string.
  9569. * @example
  9570. *
  9571. * _.capitalize('fred');
  9572. * // => 'Fred'
  9573. */
  9574. function capitalize(string) {
  9575. string = baseToString(string);
  9576. return string && (string.charAt(0).toUpperCase() + string.slice(1));
  9577. }
  9578. /**
  9579. * Deburrs `string` by converting [latin-1 supplementary letters](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)#Character_table)
  9580. * to basic latin letters and removing [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks).
  9581. *
  9582. * @static
  9583. * @memberOf _
  9584. * @category String
  9585. * @param {string} [string=''] The string to deburr.
  9586. * @returns {string} Returns the deburred string.
  9587. * @example
  9588. *
  9589. * _.deburr('déjà vu');
  9590. * // => 'deja vu'
  9591. */
  9592. function deburr(string) {
  9593. string = baseToString(string);
  9594. return string && string.replace(reLatin1, deburrLetter).replace(reComboMark, '');
  9595. }
  9596. /**
  9597. * Checks if `string` ends with the given target string.
  9598. *
  9599. * @static
  9600. * @memberOf _
  9601. * @category String
  9602. * @param {string} [string=''] The string to search.
  9603. * @param {string} [target] The string to search for.
  9604. * @param {number} [position=string.length] The position to search from.
  9605. * @returns {boolean} Returns `true` if `string` ends with `target`, else `false`.
  9606. * @example
  9607. *
  9608. * _.endsWith('abc', 'c');
  9609. * // => true
  9610. *
  9611. * _.endsWith('abc', 'b');
  9612. * // => false
  9613. *
  9614. * _.endsWith('abc', 'b', 2);
  9615. * // => true
  9616. */
  9617. function endsWith(string, target, position) {
  9618. string = baseToString(string);
  9619. target = (target + '');
  9620. var length = string.length;
  9621. position = position === undefined
  9622. ? length
  9623. : nativeMin(position < 0 ? 0 : (+position || 0), length);
  9624. position -= target.length;
  9625. return position >= 0 && string.indexOf(target, position) == position;
  9626. }
  9627. /**
  9628. * Converts the characters "&", "<", ">", '"', "'", and "\`", in `string` to
  9629. * their corresponding HTML entities.
  9630. *
  9631. * **Note:** No other characters are escaped. To escape additional characters
  9632. * use a third-party library like [_he_](https://mths.be/he).
  9633. *
  9634. * Though the ">" character is escaped for symmetry, characters like
  9635. * ">" and "/" don't need escaping in HTML and have no special meaning
  9636. * unless they're part of a tag or unquoted attribute value.
  9637. * See [Mathias Bynens's article](https://mathiasbynens.be/notes/ambiguous-ampersands)
  9638. * (under "semi-related fun fact") for more details.
  9639. *
  9640. * Backticks are escaped because in Internet Explorer < 9, they can break out
  9641. * of attribute values or HTML comments. See [#59](https://html5sec.org/#59),
  9642. * [#102](https://html5sec.org/#102), [#108](https://html5sec.org/#108), and
  9643. * [#133](https://html5sec.org/#133) of the [HTML5 Security Cheatsheet](https://html5sec.org/)
  9644. * for more details.
  9645. *
  9646. * When working with HTML you should always [quote attribute values](http://wonko.com/post/html-escaping)
  9647. * to reduce XSS vectors.
  9648. *
  9649. * @static
  9650. * @memberOf _
  9651. * @category String
  9652. * @param {string} [string=''] The string to escape.
  9653. * @returns {string} Returns the escaped string.
  9654. * @example
  9655. *
  9656. * _.escape('fred, barney, & pebbles');
  9657. * // => 'fred, barney, &amp; pebbles'
  9658. */
  9659. function escape(string) {
  9660. // Reset `lastIndex` because in IE < 9 `String#replace` does not.
  9661. string = baseToString(string);
  9662. return (string && reHasUnescapedHtml.test(string))
  9663. ? string.replace(reUnescapedHtml, escapeHtmlChar)
  9664. : string;
  9665. }
  9666. /**
  9667. * Escapes the `RegExp` special characters "\", "/", "^", "$", ".", "|", "?",
  9668. * "*", "+", "(", ")", "[", "]", "{" and "}" in `string`.
  9669. *
  9670. * @static
  9671. * @memberOf _
  9672. * @category String
  9673. * @param {string} [string=''] The string to escape.
  9674. * @returns {string} Returns the escaped string.
  9675. * @example
  9676. *
  9677. * _.escapeRegExp('[lodash](https://lodash.com/)');
  9678. * // => '\[lodash\]\(https:\/\/lodash\.com\/\)'
  9679. */
  9680. function escapeRegExp(string) {
  9681. string = baseToString(string);
  9682. return (string && reHasRegExpChars.test(string))
  9683. ? string.replace(reRegExpChars, '\\$&')
  9684. : string;
  9685. }
  9686. /**
  9687. * Converts `string` to [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles).
  9688. *
  9689. * @static
  9690. * @memberOf _
  9691. * @category String
  9692. * @param {string} [string=''] The string to convert.
  9693. * @returns {string} Returns the kebab cased string.
  9694. * @example
  9695. *
  9696. * _.kebabCase('Foo Bar');
  9697. * // => 'foo-bar'
  9698. *
  9699. * _.kebabCase('fooBar');
  9700. * // => 'foo-bar'
  9701. *
  9702. * _.kebabCase('__foo_bar__');
  9703. * // => 'foo-bar'
  9704. */
  9705. var kebabCase = createCompounder(function(result, word, index) {
  9706. return result + (index ? '-' : '') + word.toLowerCase();
  9707. });
  9708. /**
  9709. * Pads `string` on the left and right sides if it's shorter than `length`.
  9710. * Padding characters are truncated if they can't be evenly divided by `length`.
  9711. *
  9712. * @static
  9713. * @memberOf _
  9714. * @category String
  9715. * @param {string} [string=''] The string to pad.
  9716. * @param {number} [length=0] The padding length.
  9717. * @param {string} [chars=' '] The string used as padding.
  9718. * @returns {string} Returns the padded string.
  9719. * @example
  9720. *
  9721. * _.pad('abc', 8);
  9722. * // => ' abc '
  9723. *
  9724. * _.pad('abc', 8, '_-');
  9725. * // => '_-abc_-_'
  9726. *
  9727. * _.pad('abc', 3);
  9728. * // => 'abc'
  9729. */
  9730. function pad(string, length, chars) {
  9731. string = baseToString(string);
  9732. length = +length;
  9733. var strLength = string.length;
  9734. if (strLength >= length || !nativeIsFinite(length)) {
  9735. return string;
  9736. }
  9737. var mid = (length - strLength) / 2,
  9738. leftLength = floor(mid),
  9739. rightLength = ceil(mid);
  9740. chars = createPadding('', rightLength, chars);
  9741. return chars.slice(0, leftLength) + string + chars;
  9742. }
  9743. /**
  9744. * Pads `string` on the left side if it's shorter than `length`. Padding
  9745. * characters are truncated if they exceed `length`.
  9746. *
  9747. * @static
  9748. * @memberOf _
  9749. * @category String
  9750. * @param {string} [string=''] The string to pad.
  9751. * @param {number} [length=0] The padding length.
  9752. * @param {string} [chars=' '] The string used as padding.
  9753. * @returns {string} Returns the padded string.
  9754. * @example
  9755. *
  9756. * _.padLeft('abc', 6);
  9757. * // => ' abc'
  9758. *
  9759. * _.padLeft('abc', 6, '_-');
  9760. * // => '_-_abc'
  9761. *
  9762. * _.padLeft('abc', 3);
  9763. * // => 'abc'
  9764. */
  9765. var padLeft = createPadDir();
  9766. /**
  9767. * Pads `string` on the right side if it's shorter than `length`. Padding
  9768. * characters are truncated if they exceed `length`.
  9769. *
  9770. * @static
  9771. * @memberOf _
  9772. * @category String
  9773. * @param {string} [string=''] The string to pad.
  9774. * @param {number} [length=0] The padding length.
  9775. * @param {string} [chars=' '] The string used as padding.
  9776. * @returns {string} Returns the padded string.
  9777. * @example
  9778. *
  9779. * _.padRight('abc', 6);
  9780. * // => 'abc '
  9781. *
  9782. * _.padRight('abc', 6, '_-');
  9783. * // => 'abc_-_'
  9784. *
  9785. * _.padRight('abc', 3);
  9786. * // => 'abc'
  9787. */
  9788. var padRight = createPadDir(true);
  9789. /**
  9790. * Converts `string` to an integer of the specified radix. If `radix` is
  9791. * `undefined` or `0`, a `radix` of `10` is used unless `value` is a hexadecimal,
  9792. * in which case a `radix` of `16` is used.
  9793. *
  9794. * **Note:** This method aligns with the [ES5 implementation](https://es5.github.io/#E)
  9795. * of `parseInt`.
  9796. *
  9797. * @static
  9798. * @memberOf _
  9799. * @category String
  9800. * @param {string} string The string to convert.
  9801. * @param {number} [radix] The radix to interpret `value` by.
  9802. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  9803. * @returns {number} Returns the converted integer.
  9804. * @example
  9805. *
  9806. * _.parseInt('08');
  9807. * // => 8
  9808. *
  9809. * _.map(['6', '08', '10'], _.parseInt);
  9810. * // => [6, 8, 10]
  9811. */
  9812. function parseInt(string, radix, guard) {
  9813. if (guard && isIterateeCall(string, radix, guard)) {
  9814. radix = 0;
  9815. }
  9816. return nativeParseInt(string, radix);
  9817. }
  9818. // Fallback for environments with pre-ES5 implementations.
  9819. if (nativeParseInt(whitespace + '08') != 8) {
  9820. parseInt = function(string, radix, guard) {
  9821. // Firefox < 21 and Opera < 15 follow ES3 for `parseInt`.
  9822. // Chrome fails to trim leading <BOM> whitespace characters.
  9823. // See https://code.google.com/p/v8/issues/detail?id=3109 for more details.
  9824. if (guard ? isIterateeCall(string, radix, guard) : radix == null) {
  9825. radix = 0;
  9826. } else if (radix) {
  9827. radix = +radix;
  9828. }
  9829. string = trim(string);
  9830. return nativeParseInt(string, radix || (reHasHexPrefix.test(string) ? 16 : 10));
  9831. };
  9832. }
  9833. /**
  9834. * Repeats the given string `n` times.
  9835. *
  9836. * @static
  9837. * @memberOf _
  9838. * @category String
  9839. * @param {string} [string=''] The string to repeat.
  9840. * @param {number} [n=0] The number of times to repeat the string.
  9841. * @returns {string} Returns the repeated string.
  9842. * @example
  9843. *
  9844. * _.repeat('*', 3);
  9845. * // => '***'
  9846. *
  9847. * _.repeat('abc', 2);
  9848. * // => 'abcabc'
  9849. *
  9850. * _.repeat('abc', 0);
  9851. * // => ''
  9852. */
  9853. function repeat(string, n) {
  9854. var result = '';
  9855. string = baseToString(string);
  9856. n = +n;
  9857. if (n < 1 || !string || !nativeIsFinite(n)) {
  9858. return result;
  9859. }
  9860. // Leverage the exponentiation by squaring algorithm for a faster repeat.
  9861. // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details.
  9862. do {
  9863. if (n % 2) {
  9864. result += string;
  9865. }
  9866. n = floor(n / 2);
  9867. string += string;
  9868. } while (n);
  9869. return result;
  9870. }
  9871. /**
  9872. * Converts `string` to [snake case](https://en.wikipedia.org/wiki/Snake_case).
  9873. *
  9874. * @static
  9875. * @memberOf _
  9876. * @category String
  9877. * @param {string} [string=''] The string to convert.
  9878. * @returns {string} Returns the snake cased string.
  9879. * @example
  9880. *
  9881. * _.snakeCase('Foo Bar');
  9882. * // => 'foo_bar'
  9883. *
  9884. * _.snakeCase('fooBar');
  9885. * // => 'foo_bar'
  9886. *
  9887. * _.snakeCase('--foo-bar');
  9888. * // => 'foo_bar'
  9889. */
  9890. var snakeCase = createCompounder(function(result, word, index) {
  9891. return result + (index ? '_' : '') + word.toLowerCase();
  9892. });
  9893. /**
  9894. * Converts `string` to [start case](https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage).
  9895. *
  9896. * @static
  9897. * @memberOf _
  9898. * @category String
  9899. * @param {string} [string=''] The string to convert.
  9900. * @returns {string} Returns the start cased string.
  9901. * @example
  9902. *
  9903. * _.startCase('--foo-bar');
  9904. * // => 'Foo Bar'
  9905. *
  9906. * _.startCase('fooBar');
  9907. * // => 'Foo Bar'
  9908. *
  9909. * _.startCase('__foo_bar__');
  9910. * // => 'Foo Bar'
  9911. */
  9912. var startCase = createCompounder(function(result, word, index) {
  9913. return result + (index ? ' ' : '') + (word.charAt(0).toUpperCase() + word.slice(1));
  9914. });
  9915. /**
  9916. * Checks if `string` starts with the given target string.
  9917. *
  9918. * @static
  9919. * @memberOf _
  9920. * @category String
  9921. * @param {string} [string=''] The string to search.
  9922. * @param {string} [target] The string to search for.
  9923. * @param {number} [position=0] The position to search from.
  9924. * @returns {boolean} Returns `true` if `string` starts with `target`, else `false`.
  9925. * @example
  9926. *
  9927. * _.startsWith('abc', 'a');
  9928. * // => true
  9929. *
  9930. * _.startsWith('abc', 'b');
  9931. * // => false
  9932. *
  9933. * _.startsWith('abc', 'b', 1);
  9934. * // => true
  9935. */
  9936. function startsWith(string, target, position) {
  9937. string = baseToString(string);
  9938. position = position == null
  9939. ? 0
  9940. : nativeMin(position < 0 ? 0 : (+position || 0), string.length);
  9941. return string.lastIndexOf(target, position) == position;
  9942. }
  9943. /**
  9944. * Creates a compiled template function that can interpolate data properties
  9945. * in "interpolate" delimiters, HTML-escape interpolated data properties in
  9946. * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data
  9947. * properties may be accessed as free variables in the template. If a setting
  9948. * object is provided it takes precedence over `_.templateSettings` values.
  9949. *
  9950. * **Note:** In the development build `_.template` utilizes
  9951. * [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl)
  9952. * for easier debugging.
  9953. *
  9954. * For more information on precompiling templates see
  9955. * [lodash's custom builds documentation](https://lodash.com/custom-builds).
  9956. *
  9957. * For more information on Chrome extension sandboxes see
  9958. * [Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval).
  9959. *
  9960. * @static
  9961. * @memberOf _
  9962. * @category String
  9963. * @param {string} [string=''] The template string.
  9964. * @param {Object} [options] The options object.
  9965. * @param {RegExp} [options.escape] The HTML "escape" delimiter.
  9966. * @param {RegExp} [options.evaluate] The "evaluate" delimiter.
  9967. * @param {Object} [options.imports] An object to import into the template as free variables.
  9968. * @param {RegExp} [options.interpolate] The "interpolate" delimiter.
  9969. * @param {string} [options.sourceURL] The sourceURL of the template's compiled source.
  9970. * @param {string} [options.variable] The data object variable name.
  9971. * @param- {Object} [otherOptions] Enables the legacy `options` param signature.
  9972. * @returns {Function} Returns the compiled template function.
  9973. * @example
  9974. *
  9975. * // using the "interpolate" delimiter to create a compiled template
  9976. * var compiled = _.template('hello <%= user %>!');
  9977. * compiled({ 'user': 'fred' });
  9978. * // => 'hello fred!'
  9979. *
  9980. * // using the HTML "escape" delimiter to escape data property values
  9981. * var compiled = _.template('<b><%- value %></b>');
  9982. * compiled({ 'value': '<script>' });
  9983. * // => '<b>&lt;script&gt;</b>'
  9984. *
  9985. * // using the "evaluate" delimiter to execute JavaScript and generate HTML
  9986. * var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
  9987. * compiled({ 'users': ['fred', 'barney'] });
  9988. * // => '<li>fred</li><li>barney</li>'
  9989. *
  9990. * // using the internal `print` function in "evaluate" delimiters
  9991. * var compiled = _.template('<% print("hello " + user); %>!');
  9992. * compiled({ 'user': 'barney' });
  9993. * // => 'hello barney!'
  9994. *
  9995. * // using the ES delimiter as an alternative to the default "interpolate" delimiter
  9996. * var compiled = _.template('hello ${ user }!');
  9997. * compiled({ 'user': 'pebbles' });
  9998. * // => 'hello pebbles!'
  9999. *
  10000. * // using custom template delimiters
  10001. * _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
  10002. * var compiled = _.template('hello {{ user }}!');
  10003. * compiled({ 'user': 'mustache' });
  10004. * // => 'hello mustache!'
  10005. *
  10006. * // using backslashes to treat delimiters as plain text
  10007. * var compiled = _.template('<%= "\\<%- value %\\>" %>');
  10008. * compiled({ 'value': 'ignored' });
  10009. * // => '<%- value %>'
  10010. *
  10011. * // using the `imports` option to import `jQuery` as `jq`
  10012. * var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
  10013. * var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
  10014. * compiled({ 'users': ['fred', 'barney'] });
  10015. * // => '<li>fred</li><li>barney</li>'
  10016. *
  10017. * // using the `sourceURL` option to specify a custom sourceURL for the template
  10018. * var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
  10019. * compiled(data);
  10020. * // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector
  10021. *
  10022. * // using the `variable` option to ensure a with-statement isn't used in the compiled template
  10023. * var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
  10024. * compiled.source;
  10025. * // => function(data) {
  10026. * // var __t, __p = '';
  10027. * // __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
  10028. * // return __p;
  10029. * // }
  10030. *
  10031. * // using the `source` property to inline compiled templates for meaningful
  10032. * // line numbers in error messages and a stack trace
  10033. * fs.writeFileSync(path.join(cwd, 'jst.js'), '\
  10034. * var JST = {\
  10035. * "main": ' + _.template(mainText).source + '\
  10036. * };\
  10037. * ');
  10038. */
  10039. function template(string, options, otherOptions) {
  10040. // Based on John Resig's `tmpl` implementation (http://ejohn.org/blog/javascript-micro-templating/)
  10041. // and Laura Doktorova's doT.js (https://github.com/olado/doT).
  10042. var settings = lodash.templateSettings;
  10043. if (otherOptions && isIterateeCall(string, options, otherOptions)) {
  10044. options = otherOptions = null;
  10045. }
  10046. string = baseToString(string);
  10047. options = assignWith(baseAssign({}, otherOptions || options), settings, assignOwnDefaults);
  10048. var imports = assignWith(baseAssign({}, options.imports), settings.imports, assignOwnDefaults),
  10049. importsKeys = keys(imports),
  10050. importsValues = baseValues(imports, importsKeys);
  10051. var isEscaping,
  10052. isEvaluating,
  10053. index = 0,
  10054. interpolate = options.interpolate || reNoMatch,
  10055. source = "__p += '";
  10056. // Compile the regexp to match each delimiter.
  10057. var reDelimiters = RegExp(
  10058. (options.escape || reNoMatch).source + '|' +
  10059. interpolate.source + '|' +
  10060. (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
  10061. (options.evaluate || reNoMatch).source + '|$'
  10062. , 'g');
  10063. // Use a sourceURL for easier debugging.
  10064. var sourceURL = '//# sourceURL=' +
  10065. ('sourceURL' in options
  10066. ? options.sourceURL
  10067. : ('lodash.templateSources[' + (++templateCounter) + ']')
  10068. ) + '\n';
  10069. string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
  10070. interpolateValue || (interpolateValue = esTemplateValue);
  10071. // Escape characters that can't be included in string literals.
  10072. source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar);
  10073. // Replace delimiters with snippets.
  10074. if (escapeValue) {
  10075. isEscaping = true;
  10076. source += "' +\n__e(" + escapeValue + ") +\n'";
  10077. }
  10078. if (evaluateValue) {
  10079. isEvaluating = true;
  10080. source += "';\n" + evaluateValue + ";\n__p += '";
  10081. }
  10082. if (interpolateValue) {
  10083. source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
  10084. }
  10085. index = offset + match.length;
  10086. // The JS engine embedded in Adobe products requires returning the `match`
  10087. // string in order to produce the correct `offset` value.
  10088. return match;
  10089. });
  10090. source += "';\n";
  10091. // If `variable` is not specified wrap a with-statement around the generated
  10092. // code to add the data object to the top of the scope chain.
  10093. var variable = options.variable;
  10094. if (!variable) {
  10095. source = 'with (obj) {\n' + source + '\n}\n';
  10096. }
  10097. // Cleanup code by stripping empty strings.
  10098. source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
  10099. .replace(reEmptyStringMiddle, '$1')
  10100. .replace(reEmptyStringTrailing, '$1;');
  10101. // Frame code as the function body.
  10102. source = 'function(' + (variable || 'obj') + ') {\n' +
  10103. (variable
  10104. ? ''
  10105. : 'obj || (obj = {});\n'
  10106. ) +
  10107. "var __t, __p = ''" +
  10108. (isEscaping
  10109. ? ', __e = _.escape'
  10110. : ''
  10111. ) +
  10112. (isEvaluating
  10113. ? ', __j = Array.prototype.join;\n' +
  10114. "function print() { __p += __j.call(arguments, '') }\n"
  10115. : ';\n'
  10116. ) +
  10117. source +
  10118. 'return __p\n}';
  10119. var result = attempt(function() {
  10120. return Function(importsKeys, sourceURL + 'return ' + source).apply(undefined, importsValues);
  10121. });
  10122. // Provide the compiled function's source by its `toString` method or
  10123. // the `source` property as a convenience for inlining compiled templates.
  10124. result.source = source;
  10125. if (isError(result)) {
  10126. throw result;
  10127. }
  10128. return result;
  10129. }
  10130. /**
  10131. * Removes leading and trailing whitespace or specified characters from `string`.
  10132. *
  10133. * @static
  10134. * @memberOf _
  10135. * @category String
  10136. * @param {string} [string=''] The string to trim.
  10137. * @param {string} [chars=whitespace] The characters to trim.
  10138. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10139. * @returns {string} Returns the trimmed string.
  10140. * @example
  10141. *
  10142. * _.trim(' abc ');
  10143. * // => 'abc'
  10144. *
  10145. * _.trim('-_-abc-_-', '_-');
  10146. * // => 'abc'
  10147. *
  10148. * _.map([' foo ', ' bar '], _.trim);
  10149. * // => ['foo', 'bar']
  10150. */
  10151. function trim(string, chars, guard) {
  10152. var value = string;
  10153. string = baseToString(string);
  10154. if (!string) {
  10155. return string;
  10156. }
  10157. if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
  10158. return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1);
  10159. }
  10160. chars = (chars + '');
  10161. return string.slice(charsLeftIndex(string, chars), charsRightIndex(string, chars) + 1);
  10162. }
  10163. /**
  10164. * Removes leading whitespace or specified characters from `string`.
  10165. *
  10166. * @static
  10167. * @memberOf _
  10168. * @category String
  10169. * @param {string} [string=''] The string to trim.
  10170. * @param {string} [chars=whitespace] The characters to trim.
  10171. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10172. * @returns {string} Returns the trimmed string.
  10173. * @example
  10174. *
  10175. * _.trimLeft(' abc ');
  10176. * // => 'abc '
  10177. *
  10178. * _.trimLeft('-_-abc-_-', '_-');
  10179. * // => 'abc-_-'
  10180. */
  10181. function trimLeft(string, chars, guard) {
  10182. var value = string;
  10183. string = baseToString(string);
  10184. if (!string) {
  10185. return string;
  10186. }
  10187. if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
  10188. return string.slice(trimmedLeftIndex(string));
  10189. }
  10190. return string.slice(charsLeftIndex(string, (chars + '')));
  10191. }
  10192. /**
  10193. * Removes trailing whitespace or specified characters from `string`.
  10194. *
  10195. * @static
  10196. * @memberOf _
  10197. * @category String
  10198. * @param {string} [string=''] The string to trim.
  10199. * @param {string} [chars=whitespace] The characters to trim.
  10200. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10201. * @returns {string} Returns the trimmed string.
  10202. * @example
  10203. *
  10204. * _.trimRight(' abc ');
  10205. * // => ' abc'
  10206. *
  10207. * _.trimRight('-_-abc-_-', '_-');
  10208. * // => '-_-abc'
  10209. */
  10210. function trimRight(string, chars, guard) {
  10211. var value = string;
  10212. string = baseToString(string);
  10213. if (!string) {
  10214. return string;
  10215. }
  10216. if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
  10217. return string.slice(0, trimmedRightIndex(string) + 1);
  10218. }
  10219. return string.slice(0, charsRightIndex(string, (chars + '')) + 1);
  10220. }
  10221. /**
  10222. * Truncates `string` if it's longer than the given maximum string length.
  10223. * The last characters of the truncated string are replaced with the omission
  10224. * string which defaults to "...".
  10225. *
  10226. * @static
  10227. * @memberOf _
  10228. * @category String
  10229. * @param {string} [string=''] The string to truncate.
  10230. * @param {Object|number} [options] The options object or maximum string length.
  10231. * @param {number} [options.length=30] The maximum string length.
  10232. * @param {string} [options.omission='...'] The string to indicate text is omitted.
  10233. * @param {RegExp|string} [options.separator] The separator pattern to truncate to.
  10234. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10235. * @returns {string} Returns the truncated string.
  10236. * @example
  10237. *
  10238. * _.trunc('hi-diddly-ho there, neighborino');
  10239. * // => 'hi-diddly-ho there, neighbo...'
  10240. *
  10241. * _.trunc('hi-diddly-ho there, neighborino', 24);
  10242. * // => 'hi-diddly-ho there, n...'
  10243. *
  10244. * _.trunc('hi-diddly-ho there, neighborino', {
  10245. * 'length': 24,
  10246. * 'separator': ' '
  10247. * });
  10248. * // => 'hi-diddly-ho there,...'
  10249. *
  10250. * _.trunc('hi-diddly-ho there, neighborino', {
  10251. * 'length': 24,
  10252. * 'separator': /,? +/
  10253. * });
  10254. * // => 'hi-diddly-ho there...'
  10255. *
  10256. * _.trunc('hi-diddly-ho there, neighborino', {
  10257. * 'omission': ' [...]'
  10258. * });
  10259. * // => 'hi-diddly-ho there, neig [...]'
  10260. */
  10261. function trunc(string, options, guard) {
  10262. if (guard && isIterateeCall(string, options, guard)) {
  10263. options = null;
  10264. }
  10265. var length = DEFAULT_TRUNC_LENGTH,
  10266. omission = DEFAULT_TRUNC_OMISSION;
  10267. if (options != null) {
  10268. if (isObject(options)) {
  10269. var separator = 'separator' in options ? options.separator : separator;
  10270. length = 'length' in options ? (+options.length || 0) : length;
  10271. omission = 'omission' in options ? baseToString(options.omission) : omission;
  10272. } else {
  10273. length = +options || 0;
  10274. }
  10275. }
  10276. string = baseToString(string);
  10277. if (length >= string.length) {
  10278. return string;
  10279. }
  10280. var end = length - omission.length;
  10281. if (end < 1) {
  10282. return omission;
  10283. }
  10284. var result = string.slice(0, end);
  10285. if (separator == null) {
  10286. return result + omission;
  10287. }
  10288. if (isRegExp(separator)) {
  10289. if (string.slice(end).search(separator)) {
  10290. var match,
  10291. newEnd,
  10292. substring = string.slice(0, end);
  10293. if (!separator.global) {
  10294. separator = RegExp(separator.source, (reFlags.exec(separator) || '') + 'g');
  10295. }
  10296. separator.lastIndex = 0;
  10297. while ((match = separator.exec(substring))) {
  10298. newEnd = match.index;
  10299. }
  10300. result = result.slice(0, newEnd == null ? end : newEnd);
  10301. }
  10302. } else if (string.indexOf(separator, end) != end) {
  10303. var index = result.lastIndexOf(separator);
  10304. if (index > -1) {
  10305. result = result.slice(0, index);
  10306. }
  10307. }
  10308. return result + omission;
  10309. }
  10310. /**
  10311. * The inverse of `_.escape`; this method converts the HTML entities
  10312. * `&amp;`, `&lt;`, `&gt;`, `&quot;`, `&#39;`, and `&#96;` in `string` to their
  10313. * corresponding characters.
  10314. *
  10315. * **Note:** No other HTML entities are unescaped. To unescape additional HTML
  10316. * entities use a third-party library like [_he_](https://mths.be/he).
  10317. *
  10318. * @static
  10319. * @memberOf _
  10320. * @category String
  10321. * @param {string} [string=''] The string to unescape.
  10322. * @returns {string} Returns the unescaped string.
  10323. * @example
  10324. *
  10325. * _.unescape('fred, barney, &amp; pebbles');
  10326. * // => 'fred, barney, & pebbles'
  10327. */
  10328. function unescape(string) {
  10329. string = baseToString(string);
  10330. return (string && reHasEscapedHtml.test(string))
  10331. ? string.replace(reEscapedHtml, unescapeHtmlChar)
  10332. : string;
  10333. }
  10334. /**
  10335. * Splits `string` into an array of its words.
  10336. *
  10337. * @static
  10338. * @memberOf _
  10339. * @category String
  10340. * @param {string} [string=''] The string to inspect.
  10341. * @param {RegExp|string} [pattern] The pattern to match words.
  10342. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10343. * @returns {Array} Returns the words of `string`.
  10344. * @example
  10345. *
  10346. * _.words('fred, barney, & pebbles');
  10347. * // => ['fred', 'barney', 'pebbles']
  10348. *
  10349. * _.words('fred, barney, & pebbles', /[^, ]+/g);
  10350. * // => ['fred', 'barney', '&', 'pebbles']
  10351. */
  10352. function words(string, pattern, guard) {
  10353. if (guard && isIterateeCall(string, pattern, guard)) {
  10354. pattern = null;
  10355. }
  10356. string = baseToString(string);
  10357. return string.match(pattern || reWords) || [];
  10358. }
  10359. /*------------------------------------------------------------------------*/
  10360. /**
  10361. * Attempts to invoke `func`, returning either the result or the caught error
  10362. * object. Any additional arguments are provided to `func` when it is invoked.
  10363. *
  10364. * @static
  10365. * @memberOf _
  10366. * @category Utility
  10367. * @param {Function} func The function to attempt.
  10368. * @returns {*} Returns the `func` result or error object.
  10369. * @example
  10370. *
  10371. * // avoid throwing errors for invalid selectors
  10372. * var elements = _.attempt(function(selector) {
  10373. * return document.querySelectorAll(selector);
  10374. * }, '>_>');
  10375. *
  10376. * if (_.isError(elements)) {
  10377. * elements = [];
  10378. * }
  10379. */
  10380. var attempt = restParam(function(func, args) {
  10381. try {
  10382. return func.apply(undefined, args);
  10383. } catch(e) {
  10384. return isError(e) ? e : new Error(e);
  10385. }
  10386. });
  10387. /**
  10388. * Creates a function that invokes `func` with the `this` binding of `thisArg`
  10389. * and arguments of the created function. If `func` is a property name the
  10390. * created callback returns the property value for a given element. If `func`
  10391. * is an object the created callback returns `true` for elements that contain
  10392. * the equivalent object properties, otherwise it returns `false`.
  10393. *
  10394. * @static
  10395. * @memberOf _
  10396. * @alias iteratee
  10397. * @category Utility
  10398. * @param {*} [func=_.identity] The value to convert to a callback.
  10399. * @param {*} [thisArg] The `this` binding of `func`.
  10400. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  10401. * @returns {Function} Returns the callback.
  10402. * @example
  10403. *
  10404. * var users = [
  10405. * { 'user': 'barney', 'age': 36 },
  10406. * { 'user': 'fred', 'age': 40 }
  10407. * ];
  10408. *
  10409. * // wrap to create custom callback shorthands
  10410. * _.callback = _.wrap(_.callback, function(callback, func, thisArg) {
  10411. * var match = /^(.+?)__([gl]t)(.+)$/.exec(func);
  10412. * if (!match) {
  10413. * return callback(func, thisArg);
  10414. * }
  10415. * return function(object) {
  10416. * return match[2] == 'gt'
  10417. * ? object[match[1]] > match[3]
  10418. * : object[match[1]] < match[3];
  10419. * };
  10420. * });
  10421. *
  10422. * _.filter(users, 'age__gt36');
  10423. * // => [{ 'user': 'fred', 'age': 40 }]
  10424. */
  10425. function callback(func, thisArg, guard) {
  10426. if (guard && isIterateeCall(func, thisArg, guard)) {
  10427. thisArg = null;
  10428. }
  10429. return isObjectLike(func)
  10430. ? matches(func)
  10431. : baseCallback(func, thisArg);
  10432. }
  10433. /**
  10434. * Creates a function that returns `value`.
  10435. *
  10436. * @static
  10437. * @memberOf _
  10438. * @category Utility
  10439. * @param {*} value The value to return from the new function.
  10440. * @returns {Function} Returns the new function.
  10441. * @example
  10442. *
  10443. * var object = { 'user': 'fred' };
  10444. * var getter = _.constant(object);
  10445. *
  10446. * getter() === object;
  10447. * // => true
  10448. */
  10449. function constant(value) {
  10450. return function() {
  10451. return value;
  10452. };
  10453. }
  10454. /**
  10455. * This method returns the first argument provided to it.
  10456. *
  10457. * @static
  10458. * @memberOf _
  10459. * @category Utility
  10460. * @param {*} value Any value.
  10461. * @returns {*} Returns `value`.
  10462. * @example
  10463. *
  10464. * var object = { 'user': 'fred' };
  10465. *
  10466. * _.identity(object) === object;
  10467. * // => true
  10468. */
  10469. function identity(value) {
  10470. return value;
  10471. }
  10472. /**
  10473. * Creates a function that performs a deep comparison between a given object
  10474. * and `source`, returning `true` if the given object has equivalent property
  10475. * values, else `false`.
  10476. *
  10477. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  10478. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  10479. * their own, not inherited, enumerable properties. For comparing a single
  10480. * own or inherited property value see `_.matchesProperty`.
  10481. *
  10482. * @static
  10483. * @memberOf _
  10484. * @category Utility
  10485. * @param {Object} source The object of property values to match.
  10486. * @returns {Function} Returns the new function.
  10487. * @example
  10488. *
  10489. * var users = [
  10490. * { 'user': 'barney', 'age': 36, 'active': true },
  10491. * { 'user': 'fred', 'age': 40, 'active': false }
  10492. * ];
  10493. *
  10494. * _.filter(users, _.matches({ 'age': 40, 'active': false }));
  10495. * // => [{ 'user': 'fred', 'age': 40, 'active': false }]
  10496. */
  10497. function matches(source) {
  10498. return baseMatches(baseClone(source, true));
  10499. }
  10500. /**
  10501. * Creates a function that compares the property value of `path` on a given
  10502. * object to `value`.
  10503. *
  10504. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  10505. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  10506. * their own, not inherited, enumerable properties.
  10507. *
  10508. * @static
  10509. * @memberOf _
  10510. * @category Utility
  10511. * @param {Array|string} path The path of the property to get.
  10512. * @param {*} srcValue The value to match.
  10513. * @returns {Function} Returns the new function.
  10514. * @example
  10515. *
  10516. * var users = [
  10517. * { 'user': 'barney' },
  10518. * { 'user': 'fred' }
  10519. * ];
  10520. *
  10521. * _.find(users, _.matchesProperty('user', 'fred'));
  10522. * // => { 'user': 'fred' }
  10523. */
  10524. function matchesProperty(path, srcValue) {
  10525. return baseMatchesProperty(path, baseClone(srcValue, true));
  10526. }
  10527. /**
  10528. * Creates a function that invokes the method at `path` on a given object.
  10529. * Any additional arguments are provided to the invoked method.
  10530. *
  10531. * @static
  10532. * @memberOf _
  10533. * @category Utility
  10534. * @param {Array|string} path The path of the method to invoke.
  10535. * @param {...*} [args] The arguments to invoke the method with.
  10536. * @returns {Function} Returns the new function.
  10537. * @example
  10538. *
  10539. * var objects = [
  10540. * { 'a': { 'b': { 'c': _.constant(2) } } },
  10541. * { 'a': { 'b': { 'c': _.constant(1) } } }
  10542. * ];
  10543. *
  10544. * _.map(objects, _.method('a.b.c'));
  10545. * // => [2, 1]
  10546. *
  10547. * _.invoke(_.sortBy(objects, _.method(['a', 'b', 'c'])), 'a.b.c');
  10548. * // => [1, 2]
  10549. */
  10550. var method = restParam(function(path, args) {
  10551. return function(object) {
  10552. return invokePath(object, path, args);
  10553. };
  10554. });
  10555. /**
  10556. * The opposite of `_.method`; this method creates a function that invokes
  10557. * the method at a given path on `object`. Any additional arguments are
  10558. * provided to the invoked method.
  10559. *
  10560. * @static
  10561. * @memberOf _
  10562. * @category Utility
  10563. * @param {Object} object The object to query.
  10564. * @param {...*} [args] The arguments to invoke the method with.
  10565. * @returns {Function} Returns the new function.
  10566. * @example
  10567. *
  10568. * var array = _.times(3, _.constant),
  10569. * object = { 'a': array, 'b': array, 'c': array };
  10570. *
  10571. * _.map(['a[2]', 'c[0]'], _.methodOf(object));
  10572. * // => [2, 0]
  10573. *
  10574. * _.map([['a', '2'], ['c', '0']], _.methodOf(object));
  10575. * // => [2, 0]
  10576. */
  10577. var methodOf = restParam(function(object, args) {
  10578. return function(path) {
  10579. return invokePath(object, path, args);
  10580. };
  10581. });
  10582. /**
  10583. * Adds all own enumerable function properties of a source object to the
  10584. * destination object. If `object` is a function then methods are added to
  10585. * its prototype as well.
  10586. *
  10587. * **Note:** Use `_.runInContext` to create a pristine `lodash` function to
  10588. * avoid conflicts caused by modifying the original.
  10589. *
  10590. * @static
  10591. * @memberOf _
  10592. * @category Utility
  10593. * @param {Function|Object} [object=lodash] The destination object.
  10594. * @param {Object} source The object of functions to add.
  10595. * @param {Object} [options] The options object.
  10596. * @param {boolean} [options.chain=true] Specify whether the functions added
  10597. * are chainable.
  10598. * @returns {Function|Object} Returns `object`.
  10599. * @example
  10600. *
  10601. * function vowels(string) {
  10602. * return _.filter(string, function(v) {
  10603. * return /[aeiou]/i.test(v);
  10604. * });
  10605. * }
  10606. *
  10607. * _.mixin({ 'vowels': vowels });
  10608. * _.vowels('fred');
  10609. * // => ['e']
  10610. *
  10611. * _('fred').vowels().value();
  10612. * // => ['e']
  10613. *
  10614. * _.mixin({ 'vowels': vowels }, { 'chain': false });
  10615. * _('fred').vowels();
  10616. * // => ['e']
  10617. */
  10618. function mixin(object, source, options) {
  10619. if (options == null) {
  10620. var isObj = isObject(source),
  10621. props = isObj ? keys(source) : null,
  10622. methodNames = (props && props.length) ? baseFunctions(source, props) : null;
  10623. if (!(methodNames ? methodNames.length : isObj)) {
  10624. methodNames = false;
  10625. options = source;
  10626. source = object;
  10627. object = this;
  10628. }
  10629. }
  10630. if (!methodNames) {
  10631. methodNames = baseFunctions(source, keys(source));
  10632. }
  10633. var chain = true,
  10634. index = -1,
  10635. isFunc = isFunction(object),
  10636. length = methodNames.length;
  10637. if (options === false) {
  10638. chain = false;
  10639. } else if (isObject(options) && 'chain' in options) {
  10640. chain = options.chain;
  10641. }
  10642. while (++index < length) {
  10643. var methodName = methodNames[index],
  10644. func = source[methodName];
  10645. object[methodName] = func;
  10646. if (isFunc) {
  10647. object.prototype[methodName] = (function(func) {
  10648. return function() {
  10649. var chainAll = this.__chain__;
  10650. if (chain || chainAll) {
  10651. var result = object(this.__wrapped__),
  10652. actions = result.__actions__ = arrayCopy(this.__actions__);
  10653. actions.push({ 'func': func, 'args': arguments, 'thisArg': object });
  10654. result.__chain__ = chainAll;
  10655. return result;
  10656. }
  10657. var args = [this.value()];
  10658. push.apply(args, arguments);
  10659. return func.apply(object, args);
  10660. };
  10661. }(func));
  10662. }
  10663. }
  10664. return object;
  10665. }
  10666. /**
  10667. * Reverts the `_` variable to its previous value and returns a reference to
  10668. * the `lodash` function.
  10669. *
  10670. * @static
  10671. * @memberOf _
  10672. * @category Utility
  10673. * @returns {Function} Returns the `lodash` function.
  10674. * @example
  10675. *
  10676. * var lodash = _.noConflict();
  10677. */
  10678. function noConflict() {
  10679. context._ = oldDash;
  10680. return this;
  10681. }
  10682. /**
  10683. * A no-operation function that returns `undefined` regardless of the
  10684. * arguments it receives.
  10685. *
  10686. * @static
  10687. * @memberOf _
  10688. * @category Utility
  10689. * @example
  10690. *
  10691. * var object = { 'user': 'fred' };
  10692. *
  10693. * _.noop(object) === undefined;
  10694. * // => true
  10695. */
  10696. function noop() {
  10697. // No operation performed.
  10698. }
  10699. /**
  10700. * Creates a function that returns the property value at `path` on a
  10701. * given object.
  10702. *
  10703. * @static
  10704. * @memberOf _
  10705. * @category Utility
  10706. * @param {Array|string} path The path of the property to get.
  10707. * @returns {Function} Returns the new function.
  10708. * @example
  10709. *
  10710. * var objects = [
  10711. * { 'a': { 'b': { 'c': 2 } } },
  10712. * { 'a': { 'b': { 'c': 1 } } }
  10713. * ];
  10714. *
  10715. * _.map(objects, _.property('a.b.c'));
  10716. * // => [2, 1]
  10717. *
  10718. * _.pluck(_.sortBy(objects, _.property(['a', 'b', 'c'])), 'a.b.c');
  10719. * // => [1, 2]
  10720. */
  10721. function property(path) {
  10722. return isKey(path) ? baseProperty(path) : basePropertyDeep(path);
  10723. }
  10724. /**
  10725. * The opposite of `_.property`; this method creates a function that returns
  10726. * the property value at a given path on `object`.
  10727. *
  10728. * @static
  10729. * @memberOf _
  10730. * @category Utility
  10731. * @param {Object} object The object to query.
  10732. * @returns {Function} Returns the new function.
  10733. * @example
  10734. *
  10735. * var array = [0, 1, 2],
  10736. * object = { 'a': array, 'b': array, 'c': array };
  10737. *
  10738. * _.map(['a[2]', 'c[0]'], _.propertyOf(object));
  10739. * // => [2, 0]
  10740. *
  10741. * _.map([['a', '2'], ['c', '0']], _.propertyOf(object));
  10742. * // => [2, 0]
  10743. */
  10744. function propertyOf(object) {
  10745. return function(path) {
  10746. return baseGet(object, toPath(path), path + '');
  10747. };
  10748. }
  10749. /**
  10750. * Creates an array of numbers (positive and/or negative) progressing from
  10751. * `start` up to, but not including, `end`. If `end` is not specified it is
  10752. * set to `start` with `start` then set to `0`. If `end` is less than `start`
  10753. * a zero-length range is created unless a negative `step` is specified.
  10754. *
  10755. * @static
  10756. * @memberOf _
  10757. * @category Utility
  10758. * @param {number} [start=0] The start of the range.
  10759. * @param {number} end The end of the range.
  10760. * @param {number} [step=1] The value to increment or decrement by.
  10761. * @returns {Array} Returns the new array of numbers.
  10762. * @example
  10763. *
  10764. * _.range(4);
  10765. * // => [0, 1, 2, 3]
  10766. *
  10767. * _.range(1, 5);
  10768. * // => [1, 2, 3, 4]
  10769. *
  10770. * _.range(0, 20, 5);
  10771. * // => [0, 5, 10, 15]
  10772. *
  10773. * _.range(0, -4, -1);
  10774. * // => [0, -1, -2, -3]
  10775. *
  10776. * _.range(1, 4, 0);
  10777. * // => [1, 1, 1]
  10778. *
  10779. * _.range(0);
  10780. * // => []
  10781. */
  10782. function range(start, end, step) {
  10783. if (step && isIterateeCall(start, end, step)) {
  10784. end = step = null;
  10785. }
  10786. start = +start || 0;
  10787. step = step == null ? 1 : (+step || 0);
  10788. if (end == null) {
  10789. end = start;
  10790. start = 0;
  10791. } else {
  10792. end = +end || 0;
  10793. }
  10794. // Use `Array(length)` so engines like Chakra and V8 avoid slower modes.
  10795. // See https://youtu.be/XAqIpGU8ZZk#t=17m25s for more details.
  10796. var index = -1,
  10797. length = nativeMax(ceil((end - start) / (step || 1)), 0),
  10798. result = Array(length);
  10799. while (++index < length) {
  10800. result[index] = start;
  10801. start += step;
  10802. }
  10803. return result;
  10804. }
  10805. /**
  10806. * Invokes the iteratee function `n` times, returning an array of the results
  10807. * of each invocation. The `iteratee` is bound to `thisArg` and invoked with
  10808. * one argument; (index).
  10809. *
  10810. * @static
  10811. * @memberOf _
  10812. * @category Utility
  10813. * @param {number} n The number of times to invoke `iteratee`.
  10814. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  10815. * @param {*} [thisArg] The `this` binding of `iteratee`.
  10816. * @returns {Array} Returns the array of results.
  10817. * @example
  10818. *
  10819. * var diceRolls = _.times(3, _.partial(_.random, 1, 6, false));
  10820. * // => [3, 6, 4]
  10821. *
  10822. * _.times(3, function(n) {
  10823. * mage.castSpell(n);
  10824. * });
  10825. * // => invokes `mage.castSpell(n)` three times with `n` of `0`, `1`, and `2`
  10826. *
  10827. * _.times(3, function(n) {
  10828. * this.cast(n);
  10829. * }, mage);
  10830. * // => also invokes `mage.castSpell(n)` three times
  10831. */
  10832. function times(n, iteratee, thisArg) {
  10833. n = floor(n);
  10834. // Exit early to avoid a JSC JIT bug in Safari 8
  10835. // where `Array(0)` is treated as `Array(1)`.
  10836. if (n < 1 || !nativeIsFinite(n)) {
  10837. return [];
  10838. }
  10839. var index = -1,
  10840. result = Array(nativeMin(n, MAX_ARRAY_LENGTH));
  10841. iteratee = bindCallback(iteratee, thisArg, 1);
  10842. while (++index < n) {
  10843. if (index < MAX_ARRAY_LENGTH) {
  10844. result[index] = iteratee(index);
  10845. } else {
  10846. iteratee(index);
  10847. }
  10848. }
  10849. return result;
  10850. }
  10851. /**
  10852. * Generates a unique ID. If `prefix` is provided the ID is appended to it.
  10853. *
  10854. * @static
  10855. * @memberOf _
  10856. * @category Utility
  10857. * @param {string} [prefix] The value to prefix the ID with.
  10858. * @returns {string} Returns the unique ID.
  10859. * @example
  10860. *
  10861. * _.uniqueId('contact_');
  10862. * // => 'contact_104'
  10863. *
  10864. * _.uniqueId();
  10865. * // => '105'
  10866. */
  10867. function uniqueId(prefix) {
  10868. var id = ++idCounter;
  10869. return baseToString(prefix) + id;
  10870. }
  10871. /*------------------------------------------------------------------------*/
  10872. /**
  10873. * Adds two numbers.
  10874. *
  10875. * @static
  10876. * @memberOf _
  10877. * @category Math
  10878. * @param {number} augend The first number to add.
  10879. * @param {number} addend The second number to add.
  10880. * @returns {number} Returns the sum.
  10881. * @example
  10882. *
  10883. * _.add(6, 4);
  10884. * // => 10
  10885. */
  10886. function add(augend, addend) {
  10887. return (+augend || 0) + (+addend || 0);
  10888. }
  10889. /**
  10890. * Gets the maximum value of `collection`. If `collection` is empty or falsey
  10891. * `-Infinity` is returned. If an iteratee function is provided it is invoked
  10892. * for each value in `collection` to generate the criterion by which the value
  10893. * is ranked. The `iteratee` is bound to `thisArg` and invoked with three
  10894. * arguments: (value, index, collection).
  10895. *
  10896. * If a property name is provided for `iteratee` the created `_.property`
  10897. * style callback returns the property value of the given element.
  10898. *
  10899. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  10900. * style callback returns `true` for elements that have a matching property
  10901. * value, else `false`.
  10902. *
  10903. * If an object is provided for `iteratee` the created `_.matches` style
  10904. * callback returns `true` for elements that have the properties of the given
  10905. * object, else `false`.
  10906. *
  10907. * @static
  10908. * @memberOf _
  10909. * @category Math
  10910. * @param {Array|Object|string} collection The collection to iterate over.
  10911. * @param {Function|Object|string} [iteratee] The function invoked per iteration.
  10912. * @param {*} [thisArg] The `this` binding of `iteratee`.
  10913. * @returns {*} Returns the maximum value.
  10914. * @example
  10915. *
  10916. * _.max([4, 2, 8, 6]);
  10917. * // => 8
  10918. *
  10919. * _.max([]);
  10920. * // => -Infinity
  10921. *
  10922. * var users = [
  10923. * { 'user': 'barney', 'age': 36 },
  10924. * { 'user': 'fred', 'age': 40 }
  10925. * ];
  10926. *
  10927. * _.max(users, function(chr) {
  10928. * return chr.age;
  10929. * });
  10930. * // => { 'user': 'fred', 'age': 40 }
  10931. *
  10932. * // using the `_.property` callback shorthand
  10933. * _.max(users, 'age');
  10934. * // => { 'user': 'fred', 'age': 40 }
  10935. */
  10936. var max = createExtremum(gt, NEGATIVE_INFINITY);
  10937. /**
  10938. * Gets the minimum value of `collection`. If `collection` is empty or falsey
  10939. * `Infinity` is returned. If an iteratee function is provided it is invoked
  10940. * for each value in `collection` to generate the criterion by which the value
  10941. * is ranked. The `iteratee` is bound to `thisArg` and invoked with three
  10942. * arguments: (value, index, collection).
  10943. *
  10944. * If a property name is provided for `iteratee` the created `_.property`
  10945. * style callback returns the property value of the given element.
  10946. *
  10947. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  10948. * style callback returns `true` for elements that have a matching property
  10949. * value, else `false`.
  10950. *
  10951. * If an object is provided for `iteratee` the created `_.matches` style
  10952. * callback returns `true` for elements that have the properties of the given
  10953. * object, else `false`.
  10954. *
  10955. * @static
  10956. * @memberOf _
  10957. * @category Math
  10958. * @param {Array|Object|string} collection The collection to iterate over.
  10959. * @param {Function|Object|string} [iteratee] The function invoked per iteration.
  10960. * @param {*} [thisArg] The `this` binding of `iteratee`.
  10961. * @returns {*} Returns the minimum value.
  10962. * @example
  10963. *
  10964. * _.min([4, 2, 8, 6]);
  10965. * // => 2
  10966. *
  10967. * _.min([]);
  10968. * // => Infinity
  10969. *
  10970. * var users = [
  10971. * { 'user': 'barney', 'age': 36 },
  10972. * { 'user': 'fred', 'age': 40 }
  10973. * ];
  10974. *
  10975. * _.min(users, function(chr) {
  10976. * return chr.age;
  10977. * });
  10978. * // => { 'user': 'barney', 'age': 36 }
  10979. *
  10980. * // using the `_.property` callback shorthand
  10981. * _.min(users, 'age');
  10982. * // => { 'user': 'barney', 'age': 36 }
  10983. */
  10984. var min = createExtremum(lt, POSITIVE_INFINITY);
  10985. /**
  10986. * Gets the sum of the values in `collection`.
  10987. *
  10988. * @static
  10989. * @memberOf _
  10990. * @category Math
  10991. * @param {Array|Object|string} collection The collection to iterate over.
  10992. * @param {Function|Object|string} [iteratee] The function invoked per iteration.
  10993. * @param {*} [thisArg] The `this` binding of `iteratee`.
  10994. * @returns {number} Returns the sum.
  10995. * @example
  10996. *
  10997. * _.sum([4, 6]);
  10998. * // => 10
  10999. *
  11000. * _.sum({ 'a': 4, 'b': 6 });
  11001. * // => 10
  11002. *
  11003. * var objects = [
  11004. * { 'n': 4 },
  11005. * { 'n': 6 }
  11006. * ];
  11007. *
  11008. * _.sum(objects, function(object) {
  11009. * return object.n;
  11010. * });
  11011. * // => 10
  11012. *
  11013. * // using the `_.property` callback shorthand
  11014. * _.sum(objects, 'n');
  11015. * // => 10
  11016. */
  11017. function sum(collection, iteratee, thisArg) {
  11018. if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
  11019. iteratee = null;
  11020. }
  11021. var callback = getCallback(),
  11022. noIteratee = iteratee == null;
  11023. if (!(noIteratee && callback === baseCallback)) {
  11024. noIteratee = false;
  11025. iteratee = callback(iteratee, thisArg, 3);
  11026. }
  11027. return noIteratee
  11028. ? arraySum(isArray(collection) ? collection : toIterable(collection))
  11029. : baseSum(collection, iteratee);
  11030. }
  11031. /*------------------------------------------------------------------------*/
  11032. // Ensure wrappers are instances of `baseLodash`.
  11033. lodash.prototype = baseLodash.prototype;
  11034. LodashWrapper.prototype = baseCreate(baseLodash.prototype);
  11035. LodashWrapper.prototype.constructor = LodashWrapper;
  11036. LazyWrapper.prototype = baseCreate(baseLodash.prototype);
  11037. LazyWrapper.prototype.constructor = LazyWrapper;
  11038. // Add functions to the `Map` cache.
  11039. MapCache.prototype['delete'] = mapDelete;
  11040. MapCache.prototype.get = mapGet;
  11041. MapCache.prototype.has = mapHas;
  11042. MapCache.prototype.set = mapSet;
  11043. // Add functions to the `Set` cache.
  11044. SetCache.prototype.push = cachePush;
  11045. // Assign cache to `_.memoize`.
  11046. memoize.Cache = MapCache;
  11047. // Add functions that return wrapped values when chaining.
  11048. lodash.after = after;
  11049. lodash.ary = ary;
  11050. lodash.assign = assign;
  11051. lodash.at = at;
  11052. lodash.before = before;
  11053. lodash.bind = bind;
  11054. lodash.bindAll = bindAll;
  11055. lodash.bindKey = bindKey;
  11056. lodash.callback = callback;
  11057. lodash.chain = chain;
  11058. lodash.chunk = chunk;
  11059. lodash.compact = compact;
  11060. lodash.constant = constant;
  11061. lodash.countBy = countBy;
  11062. lodash.create = create;
  11063. lodash.curry = curry;
  11064. lodash.curryRight = curryRight;
  11065. lodash.debounce = debounce;
  11066. lodash.defaults = defaults;
  11067. lodash.defer = defer;
  11068. lodash.delay = delay;
  11069. lodash.difference = difference;
  11070. lodash.drop = drop;
  11071. lodash.dropRight = dropRight;
  11072. lodash.dropRightWhile = dropRightWhile;
  11073. lodash.dropWhile = dropWhile;
  11074. lodash.fill = fill;
  11075. lodash.filter = filter;
  11076. lodash.flatten = flatten;
  11077. lodash.flattenDeep = flattenDeep;
  11078. lodash.flow = flow;
  11079. lodash.flowRight = flowRight;
  11080. lodash.forEach = forEach;
  11081. lodash.forEachRight = forEachRight;
  11082. lodash.forIn = forIn;
  11083. lodash.forInRight = forInRight;
  11084. lodash.forOwn = forOwn;
  11085. lodash.forOwnRight = forOwnRight;
  11086. lodash.functions = functions;
  11087. lodash.groupBy = groupBy;
  11088. lodash.indexBy = indexBy;
  11089. lodash.initial = initial;
  11090. lodash.intersection = intersection;
  11091. lodash.invert = invert;
  11092. lodash.invoke = invoke;
  11093. lodash.keys = keys;
  11094. lodash.keysIn = keysIn;
  11095. lodash.map = map;
  11096. lodash.mapKeys = mapKeys;
  11097. lodash.mapValues = mapValues;
  11098. lodash.matches = matches;
  11099. lodash.matchesProperty = matchesProperty;
  11100. lodash.memoize = memoize;
  11101. lodash.merge = merge;
  11102. lodash.method = method;
  11103. lodash.methodOf = methodOf;
  11104. lodash.mixin = mixin;
  11105. lodash.negate = negate;
  11106. lodash.omit = omit;
  11107. lodash.once = once;
  11108. lodash.pairs = pairs;
  11109. lodash.partial = partial;
  11110. lodash.partialRight = partialRight;
  11111. lodash.partition = partition;
  11112. lodash.pick = pick;
  11113. lodash.pluck = pluck;
  11114. lodash.property = property;
  11115. lodash.propertyOf = propertyOf;
  11116. lodash.pull = pull;
  11117. lodash.pullAt = pullAt;
  11118. lodash.range = range;
  11119. lodash.rearg = rearg;
  11120. lodash.reject = reject;
  11121. lodash.remove = remove;
  11122. lodash.rest = rest;
  11123. lodash.restParam = restParam;
  11124. lodash.set = set;
  11125. lodash.shuffle = shuffle;
  11126. lodash.slice = slice;
  11127. lodash.sortBy = sortBy;
  11128. lodash.sortByAll = sortByAll;
  11129. lodash.sortByOrder = sortByOrder;
  11130. lodash.spread = spread;
  11131. lodash.take = take;
  11132. lodash.takeRight = takeRight;
  11133. lodash.takeRightWhile = takeRightWhile;
  11134. lodash.takeWhile = takeWhile;
  11135. lodash.tap = tap;
  11136. lodash.throttle = throttle;
  11137. lodash.thru = thru;
  11138. lodash.times = times;
  11139. lodash.toArray = toArray;
  11140. lodash.toPlainObject = toPlainObject;
  11141. lodash.transform = transform;
  11142. lodash.union = union;
  11143. lodash.uniq = uniq;
  11144. lodash.unzip = unzip;
  11145. lodash.unzipWith = unzipWith;
  11146. lodash.values = values;
  11147. lodash.valuesIn = valuesIn;
  11148. lodash.where = where;
  11149. lodash.without = without;
  11150. lodash.wrap = wrap;
  11151. lodash.xor = xor;
  11152. lodash.zip = zip;
  11153. lodash.zipObject = zipObject;
  11154. lodash.zipWith = zipWith;
  11155. // Add aliases.
  11156. lodash.backflow = flowRight;
  11157. lodash.collect = map;
  11158. lodash.compose = flowRight;
  11159. lodash.each = forEach;
  11160. lodash.eachRight = forEachRight;
  11161. lodash.extend = assign;
  11162. lodash.iteratee = callback;
  11163. lodash.methods = functions;
  11164. lodash.object = zipObject;
  11165. lodash.select = filter;
  11166. lodash.tail = rest;
  11167. lodash.unique = uniq;
  11168. // Add functions to `lodash.prototype`.
  11169. mixin(lodash, lodash);
  11170. /*------------------------------------------------------------------------*/
  11171. // Add functions that return unwrapped values when chaining.
  11172. lodash.add = add;
  11173. lodash.attempt = attempt;
  11174. lodash.camelCase = camelCase;
  11175. lodash.capitalize = capitalize;
  11176. lodash.clone = clone;
  11177. lodash.cloneDeep = cloneDeep;
  11178. lodash.deburr = deburr;
  11179. lodash.endsWith = endsWith;
  11180. lodash.escape = escape;
  11181. lodash.escapeRegExp = escapeRegExp;
  11182. lodash.every = every;
  11183. lodash.find = find;
  11184. lodash.findIndex = findIndex;
  11185. lodash.findKey = findKey;
  11186. lodash.findLast = findLast;
  11187. lodash.findLastIndex = findLastIndex;
  11188. lodash.findLastKey = findLastKey;
  11189. lodash.findWhere = findWhere;
  11190. lodash.first = first;
  11191. lodash.get = get;
  11192. lodash.gt = gt;
  11193. lodash.gte = gte;
  11194. lodash.has = has;
  11195. lodash.identity = identity;
  11196. lodash.includes = includes;
  11197. lodash.indexOf = indexOf;
  11198. lodash.inRange = inRange;
  11199. lodash.isArguments = isArguments;
  11200. lodash.isArray = isArray;
  11201. lodash.isBoolean = isBoolean;
  11202. lodash.isDate = isDate;
  11203. lodash.isElement = isElement;
  11204. lodash.isEmpty = isEmpty;
  11205. lodash.isEqual = isEqual;
  11206. lodash.isError = isError;
  11207. lodash.isFinite = isFinite;
  11208. lodash.isFunction = isFunction;
  11209. lodash.isMatch = isMatch;
  11210. lodash.isNaN = isNaN;
  11211. lodash.isNative = isNative;
  11212. lodash.isNull = isNull;
  11213. lodash.isNumber = isNumber;
  11214. lodash.isObject = isObject;
  11215. lodash.isPlainObject = isPlainObject;
  11216. lodash.isRegExp = isRegExp;
  11217. lodash.isString = isString;
  11218. lodash.isTypedArray = isTypedArray;
  11219. lodash.isUndefined = isUndefined;
  11220. lodash.kebabCase = kebabCase;
  11221. lodash.last = last;
  11222. lodash.lastIndexOf = lastIndexOf;
  11223. lodash.lt = lt;
  11224. lodash.lte = lte;
  11225. lodash.max = max;
  11226. lodash.min = min;
  11227. lodash.noConflict = noConflict;
  11228. lodash.noop = noop;
  11229. lodash.now = now;
  11230. lodash.pad = pad;
  11231. lodash.padLeft = padLeft;
  11232. lodash.padRight = padRight;
  11233. lodash.parseInt = parseInt;
  11234. lodash.random = random;
  11235. lodash.reduce = reduce;
  11236. lodash.reduceRight = reduceRight;
  11237. lodash.repeat = repeat;
  11238. lodash.result = result;
  11239. lodash.runInContext = runInContext;
  11240. lodash.size = size;
  11241. lodash.snakeCase = snakeCase;
  11242. lodash.some = some;
  11243. lodash.sortedIndex = sortedIndex;
  11244. lodash.sortedLastIndex = sortedLastIndex;
  11245. lodash.startCase = startCase;
  11246. lodash.startsWith = startsWith;
  11247. lodash.sum = sum;
  11248. lodash.template = template;
  11249. lodash.trim = trim;
  11250. lodash.trimLeft = trimLeft;
  11251. lodash.trimRight = trimRight;
  11252. lodash.trunc = trunc;
  11253. lodash.unescape = unescape;
  11254. lodash.uniqueId = uniqueId;
  11255. lodash.words = words;
  11256. // Add aliases.
  11257. lodash.all = every;
  11258. lodash.any = some;
  11259. lodash.contains = includes;
  11260. lodash.eq = isEqual;
  11261. lodash.detect = find;
  11262. lodash.foldl = reduce;
  11263. lodash.foldr = reduceRight;
  11264. lodash.head = first;
  11265. lodash.include = includes;
  11266. lodash.inject = reduce;
  11267. mixin(lodash, (function() {
  11268. var source = {};
  11269. baseForOwn(lodash, function(func, methodName) {
  11270. if (!lodash.prototype[methodName]) {
  11271. source[methodName] = func;
  11272. }
  11273. });
  11274. return source;
  11275. }()), false);
  11276. /*------------------------------------------------------------------------*/
  11277. // Add functions capable of returning wrapped and unwrapped values when chaining.
  11278. lodash.sample = sample;
  11279. lodash.prototype.sample = function(n) {
  11280. if (!this.__chain__ && n == null) {
  11281. return sample(this.value());
  11282. }
  11283. return this.thru(function(value) {
  11284. return sample(value, n);
  11285. });
  11286. };
  11287. /*------------------------------------------------------------------------*/
  11288. /**
  11289. * The semantic version number.
  11290. *
  11291. * @static
  11292. * @memberOf _
  11293. * @type string
  11294. */
  11295. lodash.VERSION = VERSION;
  11296. // Assign default placeholders.
  11297. arrayEach(['bind', 'bindKey', 'curry', 'curryRight', 'partial', 'partialRight'], function(methodName) {
  11298. lodash[methodName].placeholder = lodash;
  11299. });
  11300. // Add `LazyWrapper` methods that accept an `iteratee` value.
  11301. arrayEach(['dropWhile', 'filter', 'map', 'takeWhile'], function(methodName, type) {
  11302. var isFilter = type != LAZY_MAP_FLAG,
  11303. isDropWhile = type == LAZY_DROP_WHILE_FLAG;
  11304. LazyWrapper.prototype[methodName] = function(iteratee, thisArg) {
  11305. var filtered = this.__filtered__,
  11306. result = (filtered && isDropWhile) ? new LazyWrapper(this) : this.clone(),
  11307. iteratees = result.__iteratees__ || (result.__iteratees__ = []);
  11308. iteratees.push({
  11309. 'done': false,
  11310. 'count': 0,
  11311. 'index': 0,
  11312. 'iteratee': getCallback(iteratee, thisArg, 1),
  11313. 'limit': -1,
  11314. 'type': type
  11315. });
  11316. result.__filtered__ = filtered || isFilter;
  11317. return result;
  11318. };
  11319. });
  11320. // Add `LazyWrapper` methods for `_.drop` and `_.take` variants.
  11321. arrayEach(['drop', 'take'], function(methodName, index) {
  11322. var whileName = methodName + 'While';
  11323. LazyWrapper.prototype[methodName] = function(n) {
  11324. var filtered = this.__filtered__,
  11325. result = (filtered && !index) ? this.dropWhile() : this.clone();
  11326. n = n == null ? 1 : nativeMax(floor(n) || 0, 0);
  11327. if (filtered) {
  11328. if (index) {
  11329. result.__takeCount__ = nativeMin(result.__takeCount__, n);
  11330. } else {
  11331. last(result.__iteratees__).limit = n;
  11332. }
  11333. } else {
  11334. var views = result.__views__ || (result.__views__ = []);
  11335. views.push({ 'size': n, 'type': methodName + (result.__dir__ < 0 ? 'Right' : '') });
  11336. }
  11337. return result;
  11338. };
  11339. LazyWrapper.prototype[methodName + 'Right'] = function(n) {
  11340. return this.reverse()[methodName](n).reverse();
  11341. };
  11342. LazyWrapper.prototype[methodName + 'RightWhile'] = function(predicate, thisArg) {
  11343. return this.reverse()[whileName](predicate, thisArg).reverse();
  11344. };
  11345. });
  11346. // Add `LazyWrapper` methods for `_.first` and `_.last`.
  11347. arrayEach(['first', 'last'], function(methodName, index) {
  11348. var takeName = 'take' + (index ? 'Right' : '');
  11349. LazyWrapper.prototype[methodName] = function() {
  11350. return this[takeName](1).value()[0];
  11351. };
  11352. });
  11353. // Add `LazyWrapper` methods for `_.initial` and `_.rest`.
  11354. arrayEach(['initial', 'rest'], function(methodName, index) {
  11355. var dropName = 'drop' + (index ? '' : 'Right');
  11356. LazyWrapper.prototype[methodName] = function() {
  11357. return this[dropName](1);
  11358. };
  11359. });
  11360. // Add `LazyWrapper` methods for `_.pluck` and `_.where`.
  11361. arrayEach(['pluck', 'where'], function(methodName, index) {
  11362. var operationName = index ? 'filter' : 'map',
  11363. createCallback = index ? baseMatches : property;
  11364. LazyWrapper.prototype[methodName] = function(value) {
  11365. return this[operationName](createCallback(value));
  11366. };
  11367. });
  11368. LazyWrapper.prototype.compact = function() {
  11369. return this.filter(identity);
  11370. };
  11371. LazyWrapper.prototype.reject = function(predicate, thisArg) {
  11372. predicate = getCallback(predicate, thisArg, 1);
  11373. return this.filter(function(value) {
  11374. return !predicate(value);
  11375. });
  11376. };
  11377. LazyWrapper.prototype.slice = function(start, end) {
  11378. start = start == null ? 0 : (+start || 0);
  11379. var result = this;
  11380. if (start < 0) {
  11381. result = this.takeRight(-start);
  11382. } else if (start) {
  11383. result = this.drop(start);
  11384. }
  11385. if (end !== undefined) {
  11386. end = (+end || 0);
  11387. result = end < 0 ? result.dropRight(-end) : result.take(end - start);
  11388. }
  11389. return result;
  11390. };
  11391. LazyWrapper.prototype.toArray = function() {
  11392. return this.drop(0);
  11393. };
  11394. // Add `LazyWrapper` methods to `lodash.prototype`.
  11395. baseForOwn(LazyWrapper.prototype, function(func, methodName) {
  11396. var lodashFunc = lodash[methodName];
  11397. if (!lodashFunc) {
  11398. return;
  11399. }
  11400. var checkIteratee = /^(?:filter|map|reject)|While$/.test(methodName),
  11401. retUnwrapped = /^(?:first|last)$/.test(methodName);
  11402. lodash.prototype[methodName] = function() {
  11403. var args = arguments,
  11404. chainAll = this.__chain__,
  11405. value = this.__wrapped__,
  11406. isHybrid = !!this.__actions__.length,
  11407. isLazy = value instanceof LazyWrapper,
  11408. iteratee = args[0],
  11409. useLazy = isLazy || isArray(value);
  11410. if (useLazy && checkIteratee && typeof iteratee == 'function' && iteratee.length != 1) {
  11411. // avoid lazy use if the iteratee has a "length" value other than `1`
  11412. isLazy = useLazy = false;
  11413. }
  11414. var onlyLazy = isLazy && !isHybrid;
  11415. if (retUnwrapped && !chainAll) {
  11416. return onlyLazy
  11417. ? func.call(value)
  11418. : lodashFunc.call(lodash, this.value());
  11419. }
  11420. var interceptor = function(value) {
  11421. var otherArgs = [value];
  11422. push.apply(otherArgs, args);
  11423. return lodashFunc.apply(lodash, otherArgs);
  11424. };
  11425. if (useLazy) {
  11426. var wrapper = onlyLazy ? value : new LazyWrapper(this),
  11427. result = func.apply(wrapper, args);
  11428. if (!retUnwrapped && (isHybrid || result.__actions__)) {
  11429. var actions = result.__actions__ || (result.__actions__ = []);
  11430. actions.push({ 'func': thru, 'args': [interceptor], 'thisArg': lodash });
  11431. }
  11432. return new LodashWrapper(result, chainAll);
  11433. }
  11434. return this.thru(interceptor);
  11435. };
  11436. });
  11437. // Add `Array` and `String` methods to `lodash.prototype`.
  11438. arrayEach(['concat', 'join', 'pop', 'push', 'replace', 'shift', 'sort', 'splice', 'split', 'unshift'], function(methodName) {
  11439. var func = (/^(?:replace|split)$/.test(methodName) ? stringProto : arrayProto)[methodName],
  11440. chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru',
  11441. retUnwrapped = /^(?:join|pop|replace|shift)$/.test(methodName);
  11442. lodash.prototype[methodName] = function() {
  11443. var args = arguments;
  11444. if (retUnwrapped && !this.__chain__) {
  11445. return func.apply(this.value(), args);
  11446. }
  11447. return this[chainName](function(value) {
  11448. return func.apply(value, args);
  11449. });
  11450. };
  11451. });
  11452. // Map minified function names to their real names.
  11453. baseForOwn(LazyWrapper.prototype, function(func, methodName) {
  11454. var lodashFunc = lodash[methodName];
  11455. if (lodashFunc) {
  11456. var key = lodashFunc.name,
  11457. names = realNames[key] || (realNames[key] = []);
  11458. names.push({ 'name': methodName, 'func': lodashFunc });
  11459. }
  11460. });
  11461. realNames[createHybridWrapper(null, BIND_KEY_FLAG).name] = [{ 'name': 'wrapper', 'func': null }];
  11462. // Add functions to the lazy wrapper.
  11463. LazyWrapper.prototype.clone = lazyClone;
  11464. LazyWrapper.prototype.reverse = lazyReverse;
  11465. LazyWrapper.prototype.value = lazyValue;
  11466. // Add chaining functions to the `lodash` wrapper.
  11467. lodash.prototype.chain = wrapperChain;
  11468. lodash.prototype.commit = wrapperCommit;
  11469. lodash.prototype.plant = wrapperPlant;
  11470. lodash.prototype.reverse = wrapperReverse;
  11471. lodash.prototype.toString = wrapperToString;
  11472. lodash.prototype.run = lodash.prototype.toJSON = lodash.prototype.valueOf = lodash.prototype.value = wrapperValue;
  11473. // Add function aliases to the `lodash` wrapper.
  11474. lodash.prototype.collect = lodash.prototype.map;
  11475. lodash.prototype.head = lodash.prototype.first;
  11476. lodash.prototype.select = lodash.prototype.filter;
  11477. lodash.prototype.tail = lodash.prototype.rest;
  11478. return lodash;
  11479. }
  11480. /*--------------------------------------------------------------------------*/
  11481. // Export lodash.
  11482. var _ = runInContext();
  11483. // Some AMD build optimizers like r.js check for condition patterns like the following:
  11484. if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  11485. // Expose lodash to the global object when an AMD loader is present to avoid
  11486. // errors in cases where lodash is loaded by a script tag and not intended
  11487. // as an AMD module. See http://requirejs.org/docs/errors.html#mismatch for
  11488. // more details.
  11489. root._ = _;
  11490. // Define as an anonymous module so, through path mapping, it can be
  11491. // referenced as the "underscore" module.
  11492. define(function() {
  11493. return _;
  11494. });
  11495. }
  11496. // Check for `exports` after `define` in case a build optimizer adds an `exports` object.
  11497. else if (freeExports && freeModule) {
  11498. // Export for Node.js or RingoJS.
  11499. if (moduleExports) {
  11500. (freeModule.exports = _)._ = _;
  11501. }
  11502. // Export for Rhino with CommonJS support.
  11503. else {
  11504. freeExports._ = _;
  11505. }
  11506. }
  11507. else {
  11508. // Export for a browser or Rhino.
  11509. root._ = _;
  11510. }
  11511. }.call(this));