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

lodash.underscore.js 156KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979
  1. /**
  2. * @license
  3. * Lo-Dash 2.4.2 (Custom Build) <https://lodash.com/>
  4. * Build: `lodash underscore exports="amd,commonjs,global,node" -o ./dist/lodash.underscore.js`
  5. * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
  6. * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
  7. * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. * Available under MIT license <https://lodash.com/license>
  9. */
  10. ;(function() {
  11. /** Used as a safe reference for `undefined` in pre ES5 environments */
  12. var undefined;
  13. /** Used to generate unique IDs */
  14. var idCounter = 0;
  15. /** Used internally to indicate various things */
  16. var indicatorObject = {};
  17. /** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */
  18. var keyPrefix = +new Date + '';
  19. /** Used to match "interpolate" template delimiters */
  20. var reInterpolate = /<%=([\s\S]+?)%>/g;
  21. /** Used to ensure capturing order of template delimiters */
  22. var reNoMatch = /($^)/;
  23. /** Used to match unescaped characters in compiled string literals */
  24. var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
  25. /** `Object#toString` result shortcuts */
  26. var argsClass = '[object Arguments]',
  27. arrayClass = '[object Array]',
  28. boolClass = '[object Boolean]',
  29. dateClass = '[object Date]',
  30. funcClass = '[object Function]',
  31. numberClass = '[object Number]',
  32. objectClass = '[object Object]',
  33. regexpClass = '[object RegExp]',
  34. stringClass = '[object String]';
  35. /** Used to determine if values are of the language type Object */
  36. var objectTypes = {
  37. 'boolean': false,
  38. 'function': true,
  39. 'object': true,
  40. 'number': false,
  41. 'string': false,
  42. 'undefined': false
  43. };
  44. /** Used to escape characters for inclusion in compiled string literals */
  45. var stringEscapes = {
  46. '\\': '\\',
  47. "'": "'",
  48. '\n': 'n',
  49. '\r': 'r',
  50. '\t': 't',
  51. '\u2028': 'u2028',
  52. '\u2029': 'u2029'
  53. };
  54. /** Used as a reference to the global object */
  55. var root = (objectTypes[typeof window] && window) || this;
  56. /** Detect free variable `exports` */
  57. var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
  58. /** Detect free variable `module` */
  59. var freeModule = objectTypes[typeof module] && module && !module.nodeType && module;
  60. /** Detect the popular CommonJS extension `module.exports` */
  61. var moduleExports = freeModule && freeModule.exports === freeExports && freeExports;
  62. /** Detect free variable `global` from Node.js or Browserified code and use it as `root` */
  63. var freeGlobal = objectTypes[typeof global] && global;
  64. if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
  65. root = freeGlobal;
  66. }
  67. /*--------------------------------------------------------------------------*/
  68. /**
  69. * The base implementation of `_.indexOf` without support for binary searches
  70. * or `fromIndex` constraints.
  71. *
  72. * @private
  73. * @param {Array} array The array to search.
  74. * @param {*} value The value to search for.
  75. * @param {number} [fromIndex=0] The index to search from.
  76. * @returns {number} Returns the index of the matched value or `-1`.
  77. */
  78. function baseIndexOf(array, value, fromIndex) {
  79. var index = (fromIndex || 0) - 1,
  80. length = array ? array.length : 0;
  81. while (++index < length) {
  82. if (array[index] === value) {
  83. return index;
  84. }
  85. }
  86. return -1;
  87. }
  88. /**
  89. * Used by `sortBy` to compare transformed `collection` elements, stable sorting
  90. * them in ascending order.
  91. *
  92. * @private
  93. * @param {Object} a The object to compare to `b`.
  94. * @param {Object} b The object to compare to `a`.
  95. * @returns {number} Returns the sort order indicator of `1` or `-1`.
  96. */
  97. function compareAscending(a, b) {
  98. var ac = a.criteria,
  99. bc = b.criteria,
  100. index = -1,
  101. length = ac.length;
  102. while (++index < length) {
  103. var value = ac[index],
  104. other = bc[index];
  105. if (value !== other) {
  106. if (value > other || typeof value == 'undefined') {
  107. return 1;
  108. }
  109. if (value < other || typeof other == 'undefined') {
  110. return -1;
  111. }
  112. }
  113. }
  114. // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
  115. // that causes it, under certain circumstances, to return the same value for
  116. // `a` and `b`. See https://github.com/jashkenas/underscore/pull/1247
  117. //
  118. // This also ensures a stable sort in V8 and other engines.
  119. // See http://code.google.com/p/v8/issues/detail?id=90
  120. return a.index - b.index;
  121. }
  122. /**
  123. * Used by `template` to escape characters for inclusion in compiled
  124. * string literals.
  125. *
  126. * @private
  127. * @param {string} match The matched character to escape.
  128. * @returns {string} Returns the escaped character.
  129. */
  130. function escapeStringChar(match) {
  131. return '\\' + stringEscapes[match];
  132. }
  133. /**
  134. * Slices the `collection` from the `start` index up to, but not including,
  135. * the `end` index.
  136. *
  137. * Note: This function is used instead of `Array#slice` to support node lists
  138. * in IE < 9 and to ensure dense arrays are returned.
  139. *
  140. * @private
  141. * @param {Array|Object|string} collection The collection to slice.
  142. * @param {number} start The start index.
  143. * @param {number} end The end index.
  144. * @returns {Array} Returns the new array.
  145. */
  146. function slice(array, start, end) {
  147. start || (start = 0);
  148. if (typeof end == 'undefined') {
  149. end = array ? array.length : 0;
  150. }
  151. var index = -1,
  152. length = end - start || 0,
  153. result = Array(length < 0 ? 0 : length);
  154. while (++index < length) {
  155. result[index] = array[start + index];
  156. }
  157. return result;
  158. }
  159. /*--------------------------------------------------------------------------*/
  160. /**
  161. * Used for `Array` method references.
  162. *
  163. * Normally `Array.prototype` would suffice, however, using an array literal
  164. * avoids issues in Narwhal.
  165. */
  166. var arrayRef = [];
  167. /** Used for native method references */
  168. var objectProto = Object.prototype;
  169. /** Used to restore the original `_` reference in `noConflict` */
  170. var oldDash = root._;
  171. /** Used to resolve the internal [[Class]] of values */
  172. var toString = objectProto.toString;
  173. /** Used to detect if a method is native */
  174. var reNative = RegExp('^' +
  175. String(toString)
  176. .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  177. .replace(/toString| for [^\]]+/g, '.*?') + '$'
  178. );
  179. /** Native method shortcuts */
  180. var ceil = Math.ceil,
  181. floor = Math.floor,
  182. hasOwnProperty = objectProto.hasOwnProperty,
  183. push = arrayRef.push,
  184. propertyIsEnumerable = objectProto.propertyIsEnumerable;
  185. /* Native method shortcuts for methods with the same name as other `lodash` methods */
  186. var nativeCreate = isNative(nativeCreate = Object.create) && nativeCreate,
  187. nativeIsArray = isNative(nativeIsArray = Array.isArray) && nativeIsArray,
  188. nativeIsFinite = root.isFinite,
  189. nativeIsNaN = root.isNaN,
  190. nativeKeys = isNative(nativeKeys = Object.keys) && nativeKeys,
  191. nativeMax = Math.max,
  192. nativeMin = Math.min,
  193. nativeRandom = Math.random;
  194. /*--------------------------------------------------------------------------*/
  195. /**
  196. * Creates a `lodash` object which wraps the given value to enable intuitive
  197. * method chaining.
  198. *
  199. * In addition to Lo-Dash methods, wrappers also have the following `Array` methods:
  200. * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
  201. * and `unshift`
  202. *
  203. * Chaining is supported in custom builds as long as the `value` method is
  204. * implicitly or explicitly included in the build.
  205. *
  206. * The chainable wrapper functions are:
  207. * `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`,
  208. * `compose`, `concat`, `countBy`, `create`, `createCallback`, `curry`,
  209. * `debounce`, `defaults`, `defer`, `delay`, `difference`, `filter`, `flatten`,
  210. * `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`,
  211. * `functions`, `groupBy`, `indexBy`, `initial`, `intersection`, `invert`,
  212. * `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
  213. * `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `pull`, `push`,
  214. * `range`, `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`,
  215. * `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`, `transform`,
  216. * `union`, `uniq`, `unshift`, `unzip`, `values`, `where`, `without`, `wrap`,
  217. * and `zip`
  218. *
  219. * The non-chainable wrapper functions are:
  220. * `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `findIndex`,
  221. * `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `has`, `identity`,
  222. * `indexOf`, `isArguments`, `isArray`, `isBoolean`, `isDate`, `isElement`,
  223. * `isEmpty`, `isEqual`, `isFinite`, `isFunction`, `isNaN`, `isNull`, `isNumber`,
  224. * `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`, `join`,
  225. * `lastIndexOf`, `mixin`, `noConflict`, `parseInt`, `pop`, `random`, `reduce`,
  226. * `reduceRight`, `result`, `shift`, `size`, `some`, `sortedIndex`, `runInContext`,
  227. * `template`, `unescape`, `uniqueId`, and `value`
  228. *
  229. * The wrapper functions `first` and `last` return wrapped values when `n` is
  230. * provided, otherwise they return unwrapped values.
  231. *
  232. * Explicit chaining can be enabled by using the `_.chain` method.
  233. *
  234. * @name _
  235. * @constructor
  236. * @category Chaining
  237. * @param {*} value The value to wrap in a `lodash` instance.
  238. * @returns {Object} Returns a `lodash` instance.
  239. * @example
  240. *
  241. * var wrapped = _([1, 2, 3]);
  242. *
  243. * // returns an unwrapped value
  244. * wrapped.reduce(function(sum, num) {
  245. * return sum + num;
  246. * });
  247. * // => 6
  248. *
  249. * // returns a wrapped value
  250. * var squares = wrapped.map(function(num) {
  251. * return num * num;
  252. * });
  253. *
  254. * _.isArray(squares);
  255. * // => false
  256. *
  257. * _.isArray(squares.value());
  258. * // => true
  259. */
  260. function lodash(value) {
  261. return (value instanceof lodash)
  262. ? value
  263. : new lodashWrapper(value);
  264. }
  265. /**
  266. * A fast path for creating `lodash` wrapper objects.
  267. *
  268. * @private
  269. * @param {*} value The value to wrap in a `lodash` instance.
  270. * @param {boolean} chainAll A flag to enable chaining for all methods
  271. * @returns {Object} Returns a `lodash` instance.
  272. */
  273. function lodashWrapper(value, chainAll) {
  274. this.__chain__ = !!chainAll;
  275. this.__wrapped__ = value;
  276. }
  277. // ensure `new lodashWrapper` is an instance of `lodash`
  278. lodashWrapper.prototype = lodash.prototype;
  279. /**
  280. * An object used to flag environments features.
  281. *
  282. * @static
  283. * @memberOf _
  284. * @type Object
  285. */
  286. var support = {};
  287. (function() {
  288. var object = { '0': 1, 'length': 1 };
  289. /**
  290. * Detect if `Array#shift` and `Array#splice` augment array-like objects correctly.
  291. *
  292. * Firefox < 10, IE compatibility mode, and IE < 9 have buggy Array `shift()`
  293. * and `splice()` functions that fail to remove the last element, `value[0]`,
  294. * of array-like objects even though the `length` property is set to `0`.
  295. * The `shift()` method is buggy in IE 8 compatibility mode, while `splice()`
  296. * is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
  297. *
  298. * @memberOf _.support
  299. * @type boolean
  300. */
  301. support.spliceObjects = (arrayRef.splice.call(object, 0, 1), !object[0]);
  302. }(1));
  303. /**
  304. * By default, the template delimiters used by Lo-Dash are similar to those in
  305. * embedded Ruby (ERB). Change the following template settings to use alternative
  306. * delimiters.
  307. *
  308. * @static
  309. * @memberOf _
  310. * @type Object
  311. */
  312. lodash.templateSettings = {
  313. /**
  314. * Used to detect `data` property values to be HTML-escaped.
  315. *
  316. * @memberOf _.templateSettings
  317. * @type RegExp
  318. */
  319. 'escape': /<%-([\s\S]+?)%>/g,
  320. /**
  321. * Used to detect code to be evaluated.
  322. *
  323. * @memberOf _.templateSettings
  324. * @type RegExp
  325. */
  326. 'evaluate': /<%([\s\S]+?)%>/g,
  327. /**
  328. * Used to detect `data` property values to inject.
  329. *
  330. * @memberOf _.templateSettings
  331. * @type RegExp
  332. */
  333. 'interpolate': reInterpolate,
  334. /**
  335. * Used to reference the data object in the template text.
  336. *
  337. * @memberOf _.templateSettings
  338. * @type string
  339. */
  340. 'variable': ''
  341. };
  342. /*--------------------------------------------------------------------------*/
  343. /**
  344. * The base implementation of `_.bind` that creates the bound function and
  345. * sets its meta data.
  346. *
  347. * @private
  348. * @param {Array} bindData The bind data array.
  349. * @returns {Function} Returns the new bound function.
  350. */
  351. function baseBind(bindData) {
  352. var func = bindData[0],
  353. partialArgs = bindData[2],
  354. thisArg = bindData[4];
  355. function bound() {
  356. // `Function#bind` spec
  357. // http://es5.github.io/#x15.3.4.5
  358. if (partialArgs) {
  359. // avoid `arguments` object deoptimizations by using `slice` instead
  360. // of `Array.prototype.slice.call` and not assigning `arguments` to a
  361. // variable as a ternary expression
  362. var args = slice(partialArgs);
  363. push.apply(args, arguments);
  364. }
  365. // mimic the constructor's `return` behavior
  366. // http://es5.github.io/#x13.2.2
  367. if (this instanceof bound) {
  368. // ensure `new bound` is an instance of `func`
  369. var thisBinding = baseCreate(func.prototype),
  370. result = func.apply(thisBinding, args || arguments);
  371. return isObject(result) ? result : thisBinding;
  372. }
  373. return func.apply(thisArg, args || arguments);
  374. }
  375. return bound;
  376. }
  377. /**
  378. * The base implementation of `_.create` without support for assigning
  379. * properties to the created object.
  380. *
  381. * @private
  382. * @param {Object} prototype The object to inherit from.
  383. * @returns {Object} Returns the new object.
  384. */
  385. function baseCreate(prototype, properties) {
  386. return isObject(prototype) ? nativeCreate(prototype) : {};
  387. }
  388. // fallback for browsers without `Object.create`
  389. if (!nativeCreate) {
  390. baseCreate = (function() {
  391. function Object() {}
  392. return function(prototype) {
  393. if (isObject(prototype)) {
  394. Object.prototype = prototype;
  395. var result = new Object;
  396. Object.prototype = null;
  397. }
  398. return result || root.Object();
  399. };
  400. }());
  401. }
  402. /**
  403. * The base implementation of `_.createCallback` without support for creating
  404. * "_.pluck" or "_.where" style callbacks.
  405. *
  406. * @private
  407. * @param {*} [func=identity] The value to convert to a callback.
  408. * @param {*} [thisArg] The `this` binding of the created callback.
  409. * @param {number} [argCount] The number of arguments the callback accepts.
  410. * @returns {Function} Returns a callback function.
  411. */
  412. function baseCreateCallback(func, thisArg, argCount) {
  413. if (typeof func != 'function') {
  414. return identity;
  415. }
  416. // exit early for no `thisArg` or already bound by `Function#bind`
  417. if (typeof thisArg == 'undefined' || !('prototype' in func)) {
  418. return func;
  419. }
  420. switch (argCount) {
  421. case 1: return function(value) {
  422. return func.call(thisArg, value);
  423. };
  424. case 2: return function(a, b) {
  425. return func.call(thisArg, a, b);
  426. };
  427. case 3: return function(value, index, collection) {
  428. return func.call(thisArg, value, index, collection);
  429. };
  430. case 4: return function(accumulator, value, index, collection) {
  431. return func.call(thisArg, accumulator, value, index, collection);
  432. };
  433. }
  434. return bind(func, thisArg);
  435. }
  436. /**
  437. * The base implementation of `createWrapper` that creates the wrapper and
  438. * sets its meta data.
  439. *
  440. * @private
  441. * @param {Array} bindData The bind data array.
  442. * @returns {Function} Returns the new function.
  443. */
  444. function baseCreateWrapper(bindData) {
  445. var func = bindData[0],
  446. bitmask = bindData[1],
  447. partialArgs = bindData[2],
  448. partialRightArgs = bindData[3],
  449. thisArg = bindData[4],
  450. arity = bindData[5];
  451. var isBind = bitmask & 1,
  452. isBindKey = bitmask & 2,
  453. isCurry = bitmask & 4,
  454. isCurryBound = bitmask & 8,
  455. key = func;
  456. function bound() {
  457. var thisBinding = isBind ? thisArg : this;
  458. if (partialArgs) {
  459. var args = slice(partialArgs);
  460. push.apply(args, arguments);
  461. }
  462. if (partialRightArgs || isCurry) {
  463. args || (args = slice(arguments));
  464. if (partialRightArgs) {
  465. push.apply(args, partialRightArgs);
  466. }
  467. if (isCurry && args.length < arity) {
  468. bitmask |= 16 & ~32;
  469. return baseCreateWrapper([func, (isCurryBound ? bitmask : bitmask & ~3), args, null, thisArg, arity]);
  470. }
  471. }
  472. args || (args = arguments);
  473. if (isBindKey) {
  474. func = thisBinding[key];
  475. }
  476. if (this instanceof bound) {
  477. thisBinding = baseCreate(func.prototype);
  478. var result = func.apply(thisBinding, args);
  479. return isObject(result) ? result : thisBinding;
  480. }
  481. return func.apply(thisBinding, args);
  482. }
  483. return bound;
  484. }
  485. /**
  486. * The base implementation of `_.difference` that accepts a single array
  487. * of values to exclude.
  488. *
  489. * @private
  490. * @param {Array} array The array to process.
  491. * @param {Array} [values] The array of values to exclude.
  492. * @returns {Array} Returns a new array of filtered values.
  493. */
  494. function baseDifference(array, values) {
  495. var index = -1,
  496. indexOf = getIndexOf(),
  497. length = array ? array.length : 0,
  498. result = [];
  499. while (++index < length) {
  500. var value = array[index];
  501. if (indexOf(values, value) < 0) {
  502. result.push(value);
  503. }
  504. }
  505. return result;
  506. }
  507. /**
  508. * The base implementation of `_.flatten` without support for callback
  509. * shorthands or `thisArg` binding.
  510. *
  511. * @private
  512. * @param {Array} array The array to flatten.
  513. * @param {boolean} [isShallow=false] A flag to restrict flattening to a single level.
  514. * @param {boolean} [isStrict=false] A flag to restrict flattening to arrays and `arguments` objects.
  515. * @param {number} [fromIndex=0] The index to start from.
  516. * @returns {Array} Returns a new flattened array.
  517. */
  518. function baseFlatten(array, isShallow, isStrict, fromIndex) {
  519. var index = (fromIndex || 0) - 1,
  520. length = array ? array.length : 0,
  521. result = [];
  522. while (++index < length) {
  523. var value = array[index];
  524. if (value && typeof value == 'object' && typeof value.length == 'number'
  525. && (isArray(value) || isArguments(value))) {
  526. // recursively flatten arrays (susceptible to call stack limits)
  527. if (!isShallow) {
  528. value = baseFlatten(value, isShallow, isStrict);
  529. }
  530. var valIndex = -1,
  531. valLength = value.length,
  532. resIndex = result.length;
  533. result.length += valLength;
  534. while (++valIndex < valLength) {
  535. result[resIndex++] = value[valIndex];
  536. }
  537. } else if (!isStrict) {
  538. result.push(value);
  539. }
  540. }
  541. return result;
  542. }
  543. /**
  544. * The base implementation of `_.isEqual`, without support for `thisArg` binding,
  545. * that allows partial "_.where" style comparisons.
  546. *
  547. * @private
  548. * @param {*} a The value to compare.
  549. * @param {*} b The other value to compare.
  550. * @param {Function} [callback] The function to customize comparing values.
  551. * @param {Function} [isWhere=false] A flag to indicate performing partial comparisons.
  552. * @param {Array} [stackA=[]] Tracks traversed `a` objects.
  553. * @param {Array} [stackB=[]] Tracks traversed `b` objects.
  554. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  555. */
  556. function baseIsEqual(a, b, stackA, stackB) {
  557. if (a === b) {
  558. return a !== 0 || (1 / a == 1 / b);
  559. }
  560. var type = typeof a,
  561. otherType = typeof b;
  562. if (a === a &&
  563. !(a && objectTypes[type]) &&
  564. !(b && objectTypes[otherType])) {
  565. return false;
  566. }
  567. if (a == null || b == null) {
  568. return a === b;
  569. }
  570. var className = toString.call(a),
  571. otherClass = toString.call(b);
  572. if (className != otherClass) {
  573. return false;
  574. }
  575. switch (className) {
  576. case boolClass:
  577. case dateClass:
  578. return +a == +b;
  579. case numberClass:
  580. return a != +a
  581. ? b != +b
  582. : (a == 0 ? (1 / a == 1 / b) : a == +b);
  583. case regexpClass:
  584. case stringClass:
  585. return a == String(b);
  586. }
  587. var isArr = className == arrayClass;
  588. if (!isArr) {
  589. var aWrapped = a instanceof lodash,
  590. bWrapped = b instanceof lodash;
  591. if (aWrapped || bWrapped) {
  592. return baseIsEqual(aWrapped ? a.__wrapped__ : a, bWrapped ? b.__wrapped__ : b, stackA, stackB);
  593. }
  594. if (className != objectClass) {
  595. return false;
  596. }
  597. var ctorA = a.constructor,
  598. ctorB = b.constructor;
  599. if (ctorA != ctorB &&
  600. !(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) &&
  601. ('constructor' in a && 'constructor' in b)
  602. ) {
  603. return false;
  604. }
  605. }
  606. stackA || (stackA = []);
  607. stackB || (stackB = []);
  608. var length = stackA.length;
  609. while (length--) {
  610. if (stackA[length] == a) {
  611. return stackB[length] == b;
  612. }
  613. }
  614. var result = true,
  615. size = 0;
  616. stackA.push(a);
  617. stackB.push(b);
  618. if (isArr) {
  619. size = b.length;
  620. result = size == a.length;
  621. if (result) {
  622. while (size--) {
  623. if (!(result = baseIsEqual(a[size], b[size], stackA, stackB))) {
  624. break;
  625. }
  626. }
  627. }
  628. }
  629. else {
  630. forIn(b, function(value, key, b) {
  631. if (hasOwnProperty.call(b, key)) {
  632. size++;
  633. return !(result = hasOwnProperty.call(a, key) && baseIsEqual(a[key], value, stackA, stackB)) && indicatorObject;
  634. }
  635. });
  636. if (result) {
  637. forIn(a, function(value, key, a) {
  638. if (hasOwnProperty.call(a, key)) {
  639. return !(result = --size > -1) && indicatorObject;
  640. }
  641. });
  642. }
  643. }
  644. stackA.pop();
  645. stackB.pop();
  646. return result;
  647. }
  648. /**
  649. * The base implementation of `_.random` without argument juggling or support
  650. * for returning floating-point numbers.
  651. *
  652. * @private
  653. * @param {number} min The minimum possible value.
  654. * @param {number} max The maximum possible value.
  655. * @returns {number} Returns a random number.
  656. */
  657. function baseRandom(min, max) {
  658. return min + floor(nativeRandom() * (max - min + 1));
  659. }
  660. /**
  661. * The base implementation of `_.uniq` without support for callback shorthands
  662. * or `thisArg` binding.
  663. *
  664. * @private
  665. * @param {Array} array The array to process.
  666. * @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted.
  667. * @param {Function} [callback] The function called per iteration.
  668. * @returns {Array} Returns a duplicate-value-free array.
  669. */
  670. function baseUniq(array, isSorted, callback) {
  671. var index = -1,
  672. indexOf = getIndexOf(),
  673. length = array ? array.length : 0,
  674. result = [],
  675. seen = callback ? [] : result;
  676. while (++index < length) {
  677. var value = array[index],
  678. computed = callback ? callback(value, index, array) : value;
  679. if (isSorted
  680. ? !index || seen[seen.length - 1] !== computed
  681. : indexOf(seen, computed) < 0
  682. ) {
  683. if (callback) {
  684. seen.push(computed);
  685. }
  686. result.push(value);
  687. }
  688. }
  689. return result;
  690. }
  691. /**
  692. * Creates a function that aggregates a collection, creating an object composed
  693. * of keys generated from the results of running each element of the collection
  694. * through a callback. The given `setter` function sets the keys and values
  695. * of the composed object.
  696. *
  697. * @private
  698. * @param {Function} setter The setter function.
  699. * @returns {Function} Returns the new aggregator function.
  700. */
  701. function createAggregator(setter) {
  702. return function(collection, callback, thisArg) {
  703. var result = {};
  704. callback = createCallback(callback, thisArg, 3);
  705. var index = -1,
  706. length = collection ? collection.length : 0;
  707. if (typeof length == 'number') {
  708. while (++index < length) {
  709. var value = collection[index];
  710. setter(result, value, callback(value, index, collection), collection);
  711. }
  712. } else {
  713. forOwn(collection, function(value, key, collection) {
  714. setter(result, value, callback(value, key, collection), collection);
  715. });
  716. }
  717. return result;
  718. };
  719. }
  720. /**
  721. * Creates a function that, when called, either curries or invokes `func`
  722. * with an optional `this` binding and partially applied arguments.
  723. *
  724. * @private
  725. * @param {Function|string} func The function or method name to reference.
  726. * @param {number} bitmask The bitmask of method flags to compose.
  727. * The bitmask may be composed of the following flags:
  728. * 1 - `_.bind`
  729. * 2 - `_.bindKey`
  730. * 4 - `_.curry`
  731. * 8 - `_.curry` (bound)
  732. * 16 - `_.partial`
  733. * 32 - `_.partialRight`
  734. * @param {Array} [partialArgs] An array of arguments to prepend to those
  735. * provided to the new function.
  736. * @param {Array} [partialRightArgs] An array of arguments to append to those
  737. * provided to the new function.
  738. * @param {*} [thisArg] The `this` binding of `func`.
  739. * @param {number} [arity] The arity of `func`.
  740. * @returns {Function} Returns the new function.
  741. */
  742. function createWrapper(func, bitmask, partialArgs, partialRightArgs, thisArg, arity) {
  743. var isBind = bitmask & 1,
  744. isBindKey = bitmask & 2,
  745. isCurry = bitmask & 4,
  746. isCurryBound = bitmask & 8,
  747. isPartial = bitmask & 16,
  748. isPartialRight = bitmask & 32;
  749. if (!isBindKey && !isFunction(func)) {
  750. throw new TypeError;
  751. }
  752. if (isPartial && !partialArgs.length) {
  753. bitmask &= ~16;
  754. isPartial = partialArgs = false;
  755. }
  756. if (isPartialRight && !partialRightArgs.length) {
  757. bitmask &= ~32;
  758. isPartialRight = partialRightArgs = false;
  759. }
  760. // fast path for `_.bind`
  761. var creater = (bitmask == 1 || bitmask === 17) ? baseBind : baseCreateWrapper;
  762. return creater([func, bitmask, partialArgs, partialRightArgs, thisArg, arity]);
  763. }
  764. /**
  765. * Used by `escape` to convert characters to HTML entities.
  766. *
  767. * @private
  768. * @param {string} match The matched character to escape.
  769. * @returns {string} Returns the escaped character.
  770. */
  771. function escapeHtmlChar(match) {
  772. return htmlEscapes[match];
  773. }
  774. /**
  775. * Gets the appropriate "indexOf" function. If the `_.indexOf` method is
  776. * customized, this method returns the custom method, otherwise it returns
  777. * the `baseIndexOf` function.
  778. *
  779. * @private
  780. * @returns {Function} Returns the "indexOf" function.
  781. */
  782. function getIndexOf() {
  783. var result = (result = lodash.indexOf) === indexOf ? baseIndexOf : result;
  784. return result;
  785. }
  786. /**
  787. * Checks if `value` is a native function.
  788. *
  789. * @private
  790. * @param {*} value The value to check.
  791. * @returns {boolean} Returns `true` if the `value` is a native function, else `false`.
  792. */
  793. function isNative(value) {
  794. return typeof value == 'function' && reNative.test(value);
  795. }
  796. /**
  797. * Used by `unescape` to convert HTML entities to characters.
  798. *
  799. * @private
  800. * @param {string} match The matched character to unescape.
  801. * @returns {string} Returns the unescaped character.
  802. */
  803. function unescapeHtmlChar(match) {
  804. return htmlUnescapes[match];
  805. }
  806. /*--------------------------------------------------------------------------*/
  807. /**
  808. * Checks if `value` is an `arguments` object.
  809. *
  810. * @static
  811. * @memberOf _
  812. * @category Objects
  813. * @param {*} value The value to check.
  814. * @returns {boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
  815. * @example
  816. *
  817. * (function() { return _.isArguments(arguments); })(1, 2, 3);
  818. * // => true
  819. *
  820. * _.isArguments([1, 2, 3]);
  821. * // => false
  822. */
  823. function isArguments(value) {
  824. return value && typeof value == 'object' && typeof value.length == 'number' &&
  825. toString.call(value) == argsClass || false;
  826. }
  827. // fallback for browsers that can't detect `arguments` objects by [[Class]]
  828. if (!isArguments(arguments)) {
  829. isArguments = function(value) {
  830. return value && typeof value == 'object' && typeof value.length == 'number' &&
  831. hasOwnProperty.call(value, 'callee') && !propertyIsEnumerable.call(value, 'callee') || false;
  832. };
  833. }
  834. /**
  835. * Checks if `value` is an array.
  836. *
  837. * @static
  838. * @memberOf _
  839. * @type Function
  840. * @category Objects
  841. * @param {*} value The value to check.
  842. * @returns {boolean} Returns `true` if the `value` is an array, else `false`.
  843. * @example
  844. *
  845. * (function() { return _.isArray(arguments); })();
  846. * // => false
  847. *
  848. * _.isArray([1, 2, 3]);
  849. * // => true
  850. */
  851. var isArray = nativeIsArray || function(value) {
  852. return value && typeof value == 'object' && typeof value.length == 'number' &&
  853. toString.call(value) == arrayClass || false;
  854. };
  855. /**
  856. * A fallback implementation of `Object.keys` which produces an array of the
  857. * given object's own enumerable property names.
  858. *
  859. * @private
  860. * @type Function
  861. * @param {Object} object The object to inspect.
  862. * @returns {Array} Returns an array of property names.
  863. */
  864. var shimKeys = function(object) {
  865. var index, iterable = object, result = [];
  866. if (!iterable) return result;
  867. if (!(objectTypes[typeof object])) return result;
  868. for (index in iterable) {
  869. if (hasOwnProperty.call(iterable, index)) {
  870. result.push(index);
  871. }
  872. }
  873. return result
  874. };
  875. /**
  876. * Creates an array composed of the own enumerable property names of an object.
  877. *
  878. * @static
  879. * @memberOf _
  880. * @category Objects
  881. * @param {Object} object The object to inspect.
  882. * @returns {Array} Returns an array of property names.
  883. * @example
  884. *
  885. * _.keys({ 'one': 1, 'two': 2, 'three': 3 });
  886. * // => ['one', 'two', 'three'] (property order is not guaranteed across environments)
  887. */
  888. var keys = !nativeKeys ? shimKeys : function(object) {
  889. if (!isObject(object)) {
  890. return [];
  891. }
  892. return nativeKeys(object);
  893. };
  894. /**
  895. * Used to convert characters to HTML entities:
  896. *
  897. * Though the `>` character is escaped for symmetry, characters like `>` and `/`
  898. * don't require escaping in HTML and have no special meaning unless they're part
  899. * of a tag or an unquoted attribute value.
  900. * http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
  901. */
  902. var htmlEscapes = {
  903. '&': '&amp;',
  904. '<': '&lt;',
  905. '>': '&gt;',
  906. '"': '&quot;',
  907. "'": '&#x27;'
  908. };
  909. /** Used to convert HTML entities to characters */
  910. var htmlUnescapes = invert(htmlEscapes);
  911. /** Used to match HTML entities and HTML characters */
  912. var reEscapedHtml = RegExp('(' + keys(htmlUnescapes).join('|') + ')', 'g'),
  913. reUnescapedHtml = RegExp('[' + keys(htmlEscapes).join('') + ']', 'g');
  914. /*--------------------------------------------------------------------------*/
  915. /**
  916. * Assigns own enumerable properties of source object(s) to the destination
  917. * object. Subsequent sources will overwrite property assignments of previous
  918. * sources. If a callback is provided it will be executed to produce the
  919. * assigned values. The callback is bound to `thisArg` and invoked with two
  920. * arguments; (objectValue, sourceValue).
  921. *
  922. * @static
  923. * @memberOf _
  924. * @type Function
  925. * @alias extend
  926. * @category Objects
  927. * @param {Object} object The destination object.
  928. * @param {...Object} [source] The source objects.
  929. * @param {Function} [callback] The function to customize assigning values.
  930. * @param {*} [thisArg] The `this` binding of `callback`.
  931. * @returns {Object} Returns the destination object.
  932. * @example
  933. *
  934. * _.assign({ 'name': 'fred' }, { 'employer': 'slate' });
  935. * // => { 'name': 'fred', 'employer': 'slate' }
  936. *
  937. * var defaults = _.partialRight(_.assign, function(a, b) {
  938. * return typeof a == 'undefined' ? b : a;
  939. * });
  940. *
  941. * var object = { 'name': 'barney' };
  942. * defaults(object, { 'name': 'fred', 'employer': 'slate' });
  943. * // => { 'name': 'barney', 'employer': 'slate' }
  944. */
  945. function assign(object) {
  946. if (!object) {
  947. return object;
  948. }
  949. for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
  950. var iterable = arguments[argsIndex];
  951. if (iterable) {
  952. for (var key in iterable) {
  953. object[key] = iterable[key];
  954. }
  955. }
  956. }
  957. return object;
  958. }
  959. /**
  960. * Creates a clone of `value`. If `isDeep` is `true` nested objects will also
  961. * be cloned, otherwise they will be assigned by reference. If a callback
  962. * is provided it will be executed to produce the cloned values. If the
  963. * callback returns `undefined` cloning will be handled by the method instead.
  964. * The callback is bound to `thisArg` and invoked with one argument; (value).
  965. *
  966. * @static
  967. * @memberOf _
  968. * @category Objects
  969. * @param {*} value The value to clone.
  970. * @param {boolean} [isDeep=false] Specify a deep clone.
  971. * @param {Function} [callback] The function to customize cloning values.
  972. * @param {*} [thisArg] The `this` binding of `callback`.
  973. * @returns {*} Returns the cloned value.
  974. * @example
  975. *
  976. * var characters = [
  977. * { 'name': 'barney', 'age': 36 },
  978. * { 'name': 'fred', 'age': 40 }
  979. * ];
  980. *
  981. * var shallow = _.clone(characters);
  982. * shallow[0] === characters[0];
  983. * // => true
  984. *
  985. * var deep = _.clone(characters, true);
  986. * deep[0] === characters[0];
  987. * // => false
  988. *
  989. * _.mixin({
  990. * 'clone': _.partialRight(_.clone, function(value) {
  991. * return _.isElement(value) ? value.cloneNode(false) : undefined;
  992. * })
  993. * });
  994. *
  995. * var clone = _.clone(document.body);
  996. * clone.childNodes.length;
  997. * // => 0
  998. */
  999. function clone(value) {
  1000. return isObject(value)
  1001. ? (isArray(value) ? slice(value) : assign({}, value))
  1002. : value;
  1003. }
  1004. /**
  1005. * Assigns own enumerable properties of source object(s) to the destination
  1006. * object for all destination properties that resolve to `undefined`. Once a
  1007. * property is set, additional defaults of the same property will be ignored.
  1008. *
  1009. * @static
  1010. * @memberOf _
  1011. * @type Function
  1012. * @category Objects
  1013. * @param {Object} object The destination object.
  1014. * @param {...Object} [source] The source objects.
  1015. * @param- {Object} [guard] Allows working with `_.reduce` without using its
  1016. * `key` and `object` arguments as sources.
  1017. * @returns {Object} Returns the destination object.
  1018. * @example
  1019. *
  1020. * var object = { 'name': 'barney' };
  1021. * _.defaults(object, { 'name': 'fred', 'employer': 'slate' });
  1022. * // => { 'name': 'barney', 'employer': 'slate' }
  1023. */
  1024. function defaults(object) {
  1025. if (!object) {
  1026. return object;
  1027. }
  1028. for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
  1029. var iterable = arguments[argsIndex];
  1030. if (iterable) {
  1031. for (var key in iterable) {
  1032. if (typeof object[key] == 'undefined') {
  1033. object[key] = iterable[key];
  1034. }
  1035. }
  1036. }
  1037. }
  1038. return object;
  1039. }
  1040. /**
  1041. * Iterates over own and inherited enumerable properties of an object,
  1042. * executing the callback for each property. The callback is bound to `thisArg`
  1043. * and invoked with three arguments; (value, key, object). Callbacks may exit
  1044. * iteration early by explicitly returning `false`.
  1045. *
  1046. * @static
  1047. * @memberOf _
  1048. * @type Function
  1049. * @category Objects
  1050. * @param {Object} object The object to iterate over.
  1051. * @param {Function} [callback=identity] The function called per iteration.
  1052. * @param {*} [thisArg] The `this` binding of `callback`.
  1053. * @returns {Object} Returns `object`.
  1054. * @example
  1055. *
  1056. * function Shape() {
  1057. * this.x = 0;
  1058. * this.y = 0;
  1059. * }
  1060. *
  1061. * Shape.prototype.move = function(x, y) {
  1062. * this.x += x;
  1063. * this.y += y;
  1064. * };
  1065. *
  1066. * _.forIn(new Shape, function(value, key) {
  1067. * console.log(key);
  1068. * });
  1069. * // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments)
  1070. */
  1071. var forIn = function(collection, callback) {
  1072. var index, iterable = collection, result = iterable;
  1073. if (!iterable) return result;
  1074. if (!objectTypes[typeof iterable]) return result;
  1075. for (index in iterable) {
  1076. if (callback(iterable[index], index, collection) === indicatorObject) return result;
  1077. }
  1078. return result
  1079. };
  1080. /**
  1081. * Iterates over own enumerable properties of an object, executing the callback
  1082. * for each property. The callback is bound to `thisArg` and invoked with three
  1083. * arguments; (value, key, object). Callbacks may exit iteration early by
  1084. * explicitly returning `false`.
  1085. *
  1086. * @static
  1087. * @memberOf _
  1088. * @type Function
  1089. * @category Objects
  1090. * @param {Object} object The object to iterate over.
  1091. * @param {Function} [callback=identity] The function called per iteration.
  1092. * @param {*} [thisArg] The `this` binding of `callback`.
  1093. * @returns {Object} Returns `object`.
  1094. * @example
  1095. *
  1096. * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  1097. * console.log(key);
  1098. * });
  1099. * // => logs '0', '1', and 'length' (property order is not guaranteed across environments)
  1100. */
  1101. var forOwn = function(collection, callback) {
  1102. var index, iterable = collection, result = iterable;
  1103. if (!iterable) return result;
  1104. if (!objectTypes[typeof iterable]) return result;
  1105. for (index in iterable) {
  1106. if (hasOwnProperty.call(iterable, index)) {
  1107. if (callback(iterable[index], index, collection) === indicatorObject) return result;
  1108. }
  1109. }
  1110. return result
  1111. };
  1112. /**
  1113. * Creates a sorted array of property names of all enumerable properties,
  1114. * own and inherited, of `object` that have function values.
  1115. *
  1116. * @static
  1117. * @memberOf _
  1118. * @alias methods
  1119. * @category Objects
  1120. * @param {Object} object The object to inspect.
  1121. * @returns {Array} Returns an array of property names that have function values.
  1122. * @example
  1123. *
  1124. * _.functions(_);
  1125. * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
  1126. */
  1127. function functions(object) {
  1128. var result = [];
  1129. forIn(object, function(value, key) {
  1130. if (isFunction(value)) {
  1131. result.push(key);
  1132. }
  1133. });
  1134. return result.sort();
  1135. }
  1136. /**
  1137. * Checks if the specified property name exists as a direct property of `object`,
  1138. * instead of an inherited property.
  1139. *
  1140. * @static
  1141. * @memberOf _
  1142. * @category Objects
  1143. * @param {Object} object The object to inspect.
  1144. * @param {string} key The name of the property to check.
  1145. * @returns {boolean} Returns `true` if key is a direct property, else `false`.
  1146. * @example
  1147. *
  1148. * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
  1149. * // => true
  1150. */
  1151. function has(object, key) {
  1152. return object ? hasOwnProperty.call(object, key) : false;
  1153. }
  1154. /**
  1155. * Creates an object composed of the inverted keys and values of the given object.
  1156. *
  1157. * @static
  1158. * @memberOf _
  1159. * @category Objects
  1160. * @param {Object} object The object to invert.
  1161. * @returns {Object} Returns the created inverted object.
  1162. * @example
  1163. *
  1164. * _.invert({ 'first': 'fred', 'second': 'barney' });
  1165. * // => { 'fred': 'first', 'barney': 'second' }
  1166. */
  1167. function invert(object) {
  1168. var index = -1,
  1169. props = keys(object),
  1170. length = props.length,
  1171. result = {};
  1172. while (++index < length) {
  1173. var key = props[index];
  1174. result[object[key]] = key;
  1175. }
  1176. return result;
  1177. }
  1178. /**
  1179. * Checks if `value` is a boolean value.
  1180. *
  1181. * @static
  1182. * @memberOf _
  1183. * @category Objects
  1184. * @param {*} value The value to check.
  1185. * @returns {boolean} Returns `true` if the `value` is a boolean value, else `false`.
  1186. * @example
  1187. *
  1188. * _.isBoolean(null);
  1189. * // => false
  1190. */
  1191. function isBoolean(value) {
  1192. return value === true || value === false ||
  1193. value && typeof value == 'object' && toString.call(value) == boolClass || false;
  1194. }
  1195. /**
  1196. * Checks if `value` is a date.
  1197. *
  1198. * @static
  1199. * @memberOf _
  1200. * @category Objects
  1201. * @param {*} value The value to check.
  1202. * @returns {boolean} Returns `true` if the `value` is a date, else `false`.
  1203. * @example
  1204. *
  1205. * _.isDate(new Date);
  1206. * // => true
  1207. */
  1208. function isDate(value) {
  1209. return value && typeof value == 'object' && toString.call(value) == dateClass || false;
  1210. }
  1211. /**
  1212. * Checks if `value` is a DOM element.
  1213. *
  1214. * @static
  1215. * @memberOf _
  1216. * @category Objects
  1217. * @param {*} value The value to check.
  1218. * @returns {boolean} Returns `true` if the `value` is a DOM element, else `false`.
  1219. * @example
  1220. *
  1221. * _.isElement(document.body);
  1222. * // => true
  1223. */
  1224. function isElement(value) {
  1225. return value && value.nodeType === 1 || false;
  1226. }
  1227. /**
  1228. * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
  1229. * length of `0` and objects with no own enumerable properties are considered
  1230. * "empty".
  1231. *
  1232. * @static
  1233. * @memberOf _
  1234. * @category Objects
  1235. * @param {Array|Object|string} value The value to inspect.
  1236. * @returns {boolean} Returns `true` if the `value` is empty, else `false`.
  1237. * @example
  1238. *
  1239. * _.isEmpty([1, 2, 3]);
  1240. * // => false
  1241. *
  1242. * _.isEmpty({});
  1243. * // => true
  1244. *
  1245. * _.isEmpty('');
  1246. * // => true
  1247. */
  1248. function isEmpty(value) {
  1249. if (!value) {
  1250. return true;
  1251. }
  1252. if (isArray(value) || isString(value)) {
  1253. return !value.length;
  1254. }
  1255. for (var key in value) {
  1256. if (hasOwnProperty.call(value, key)) {
  1257. return false;
  1258. }
  1259. }
  1260. return true;
  1261. }
  1262. /**
  1263. * Performs a deep comparison between two values to determine if they are
  1264. * equivalent to each other. If a callback is provided it will be executed
  1265. * to compare values. If the callback returns `undefined` comparisons will
  1266. * be handled by the method instead. The callback is bound to `thisArg` and
  1267. * invoked with two arguments; (a, b).
  1268. *
  1269. * @static
  1270. * @memberOf _
  1271. * @category Objects
  1272. * @param {*} a The value to compare.
  1273. * @param {*} b The other value to compare.
  1274. * @param {Function} [callback] The function to customize comparing values.
  1275. * @param {*} [thisArg] The `this` binding of `callback`.
  1276. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  1277. * @example
  1278. *
  1279. * var object = { 'name': 'fred' };
  1280. * var copy = { 'name': 'fred' };
  1281. *
  1282. * object == copy;
  1283. * // => false
  1284. *
  1285. * _.isEqual(object, copy);
  1286. * // => true
  1287. *
  1288. * var words = ['hello', 'goodbye'];
  1289. * var otherWords = ['hi', 'goodbye'];
  1290. *
  1291. * _.isEqual(words, otherWords, function(a, b) {
  1292. * var reGreet = /^(?:hello|hi)$/i,
  1293. * aGreet = _.isString(a) && reGreet.test(a),
  1294. * bGreet = _.isString(b) && reGreet.test(b);
  1295. *
  1296. * return (aGreet || bGreet) ? (aGreet == bGreet) : undefined;
  1297. * });
  1298. * // => true
  1299. */
  1300. function isEqual(a, b) {
  1301. return baseIsEqual(a, b);
  1302. }
  1303. /**
  1304. * Checks if `value` is, or can be coerced to, a finite number.
  1305. *
  1306. * Note: This is not the same as native `isFinite` which will return true for
  1307. * booleans and empty strings. See http://es5.github.io/#x15.1.2.5.
  1308. *
  1309. * @static
  1310. * @memberOf _
  1311. * @category Objects
  1312. * @param {*} value The value to check.
  1313. * @returns {boolean} Returns `true` if the `value` is finite, else `false`.
  1314. * @example
  1315. *
  1316. * _.isFinite(-101);
  1317. * // => true
  1318. *
  1319. * _.isFinite('10');
  1320. * // => true
  1321. *
  1322. * _.isFinite(true);
  1323. * // => false
  1324. *
  1325. * _.isFinite('');
  1326. * // => false
  1327. *
  1328. * _.isFinite(Infinity);
  1329. * // => false
  1330. */
  1331. function isFinite(value) {
  1332. return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value));
  1333. }
  1334. /**
  1335. * Checks if `value` is a function.
  1336. *
  1337. * @static
  1338. * @memberOf _
  1339. * @category Objects
  1340. * @param {*} value The value to check.
  1341. * @returns {boolean} Returns `true` if the `value` is a function, else `false`.
  1342. * @example
  1343. *
  1344. * _.isFunction(_);
  1345. * // => true
  1346. */
  1347. function isFunction(value) {
  1348. return typeof value == 'function';
  1349. }
  1350. // fallback for older versions of Chrome and Safari
  1351. if (isFunction(/x/)) {
  1352. isFunction = function(value) {
  1353. return typeof value == 'function' && toString.call(value) == funcClass;
  1354. };
  1355. }
  1356. /**
  1357. * Checks if `value` is the language type of Object.
  1358. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  1359. *
  1360. * @static
  1361. * @memberOf _
  1362. * @category Objects
  1363. * @param {*} value The value to check.
  1364. * @returns {boolean} Returns `true` if the `value` is an object, else `false`.
  1365. * @example
  1366. *
  1367. * _.isObject({});
  1368. * // => true
  1369. *
  1370. * _.isObject([1, 2, 3]);
  1371. * // => true
  1372. *
  1373. * _.isObject(1);
  1374. * // => false
  1375. */
  1376. function isObject(value) {
  1377. // check if the value is the ECMAScript language type of Object
  1378. // http://es5.github.io/#x8
  1379. // and avoid a V8 bug
  1380. // http://code.google.com/p/v8/issues/detail?id=2291
  1381. return !!(value && objectTypes[typeof value]);
  1382. }
  1383. /**
  1384. * Checks if `value` is `NaN`.
  1385. *
  1386. * Note: This is not the same as native `isNaN` which will return `true` for
  1387. * `undefined` and other non-numeric values. See http://es5.github.io/#x15.1.2.4.
  1388. *
  1389. * @static
  1390. * @memberOf _
  1391. * @category Objects
  1392. * @param {*} value The value to check.
  1393. * @returns {boolean} Returns `true` if the `value` is `NaN`, else `false`.
  1394. * @example
  1395. *
  1396. * _.isNaN(NaN);
  1397. * // => true
  1398. *
  1399. * _.isNaN(new Number(NaN));
  1400. * // => true
  1401. *
  1402. * isNaN(undefined);
  1403. * // => true
  1404. *
  1405. * _.isNaN(undefined);
  1406. * // => false
  1407. */
  1408. function isNaN(value) {
  1409. // `NaN` as a primitive is the only value that is not equal to itself
  1410. // (perform the [[Class]] check first to avoid errors with some host objects in IE)
  1411. return isNumber(value) && value != +value;
  1412. }
  1413. /**
  1414. * Checks if `value` is `null`.
  1415. *
  1416. * @static
  1417. * @memberOf _
  1418. * @category Objects
  1419. * @param {*} value The value to check.
  1420. * @returns {boolean} Returns `true` if the `value` is `null`, else `false`.
  1421. * @example
  1422. *
  1423. * _.isNull(null);
  1424. * // => true
  1425. *
  1426. * _.isNull(undefined);
  1427. * // => false
  1428. */
  1429. function isNull(value) {
  1430. return value === null;
  1431. }
  1432. /**
  1433. * Checks if `value` is a number.
  1434. *
  1435. * Note: `NaN` is considered a number. See http://es5.github.io/#x8.5.
  1436. *
  1437. * @static
  1438. * @memberOf _
  1439. * @category Objects
  1440. * @param {*} value The value to check.
  1441. * @returns {boolean} Returns `true` if the `value` is a number, else `false`.
  1442. * @example
  1443. *
  1444. * _.isNumber(8.4 * 5);
  1445. * // => true
  1446. */
  1447. function isNumber(value) {
  1448. return typeof value == 'number' ||
  1449. value && typeof value == 'object' && toString.call(value) == numberClass || false;
  1450. }
  1451. /**
  1452. * Checks if `value` is a regular expression.
  1453. *
  1454. * @static
  1455. * @memberOf _
  1456. * @category Objects
  1457. * @param {*} value The value to check.
  1458. * @returns {boolean} Returns `true` if the `value` is a regular expression, else `false`.
  1459. * @example
  1460. *
  1461. * _.isRegExp(/fred/);
  1462. * // => true
  1463. */
  1464. function isRegExp(value) {
  1465. return value && objectTypes[typeof value] && toString.call(value) == regexpClass || false;
  1466. }
  1467. /**
  1468. * Checks if `value` is a string.
  1469. *
  1470. * @static
  1471. * @memberOf _
  1472. * @category Objects
  1473. * @param {*} value The value to check.
  1474. * @returns {boolean} Returns `true` if the `value` is a string, else `false`.
  1475. * @example
  1476. *
  1477. * _.isString('fred');
  1478. * // => true
  1479. */
  1480. function isString(value) {
  1481. return typeof value == 'string' ||
  1482. value && typeof value == 'object' && toString.call(value) == stringClass || false;
  1483. }
  1484. /**
  1485. * Checks if `value` is `undefined`.
  1486. *
  1487. * @static
  1488. * @memberOf _
  1489. * @category Objects
  1490. * @param {*} value The value to check.
  1491. * @returns {boolean} Returns `true` if the `value` is `undefined`, else `false`.
  1492. * @example
  1493. *
  1494. * _.isUndefined(void 0);
  1495. * // => true
  1496. */
  1497. function isUndefined(value) {
  1498. return typeof value == 'undefined';
  1499. }
  1500. /**
  1501. * Creates a shallow clone of `object` excluding the specified properties.
  1502. * Property names may be specified as individual arguments or as arrays of
  1503. * property names. If a callback is provided it will be executed for each
  1504. * property of `object` omitting the properties the callback returns truey
  1505. * for. The callback is bound to `thisArg` and invoked with three arguments;
  1506. * (value, key, object).
  1507. *
  1508. * @static
  1509. * @memberOf _
  1510. * @category Objects
  1511. * @param {Object} object The source object.
  1512. * @param {Function|...string|string[]} [callback] The properties to omit or the
  1513. * function called per iteration.
  1514. * @param {*} [thisArg] The `this` binding of `callback`.
  1515. * @returns {Object} Returns an object without the omitted properties.
  1516. * @example
  1517. *
  1518. * _.omit({ 'name': 'fred', 'age': 40 }, 'age');
  1519. * // => { 'name': 'fred' }
  1520. *
  1521. * _.omit({ 'name': 'fred', 'age': 40 }, function(value) {
  1522. * return typeof value == 'number';
  1523. * });
  1524. * // => { 'name': 'fred' }
  1525. */
  1526. function omit(object) {
  1527. var props = [];
  1528. forIn(object, function(value, key) {
  1529. props.push(key);
  1530. });
  1531. props = baseDifference(props, baseFlatten(arguments, true, false, 1));
  1532. var index = -1,
  1533. length = props.length,
  1534. result = {};
  1535. while (++index < length) {
  1536. var key = props[index];
  1537. result[key] = object[key];
  1538. }
  1539. return result;
  1540. }
  1541. /**
  1542. * Creates a two dimensional array of an object's key-value pairs,
  1543. * i.e. `[[key1, value1], [key2, value2]]`.
  1544. *
  1545. * @static
  1546. * @memberOf _
  1547. * @category Objects
  1548. * @param {Object} object The object to inspect.
  1549. * @returns {Array} Returns new array of key-value pairs.
  1550. * @example
  1551. *
  1552. * _.pairs({ 'barney': 36, 'fred': 40 });
  1553. * // => [['barney', 36], ['fred', 40]] (property order is not guaranteed across environments)
  1554. */
  1555. function pairs(object) {
  1556. var index = -1,
  1557. props = keys(object),
  1558. length = props.length,
  1559. result = Array(length);
  1560. while (++index < length) {
  1561. var key = props[index];
  1562. result[index] = [key, object[key]];
  1563. }
  1564. return result;
  1565. }
  1566. /**
  1567. * Creates a shallow clone of `object` composed of the specified properties.
  1568. * Property names may be specified as individual arguments or as arrays of
  1569. * property names. If a callback is provided it will be executed for each
  1570. * property of `object` picking the properties the callback returns truey
  1571. * for. The callback is bound to `thisArg` and invoked with three arguments;
  1572. * (value, key, object).
  1573. *
  1574. * @static
  1575. * @memberOf _
  1576. * @category Objects
  1577. * @param {Object} object The source object.
  1578. * @param {Function|...string|string[]} [callback] The function called per
  1579. * iteration or property names to pick, specified as individual property
  1580. * names or arrays of property names.
  1581. * @param {*} [thisArg] The `this` binding of `callback`.
  1582. * @returns {Object} Returns an object composed of the picked properties.
  1583. * @example
  1584. *
  1585. * _.pick({ 'name': 'fred', '_userid': 'fred1' }, 'name');
  1586. * // => { 'name': 'fred' }
  1587. *
  1588. * _.pick({ 'name': 'fred', '_userid': 'fred1' }, function(value, key) {
  1589. * return key.charAt(0) != '_';
  1590. * });
  1591. * // => { 'name': 'fred' }
  1592. */
  1593. function pick(object) {
  1594. var index = -1,
  1595. props = baseFlatten(arguments, true, false, 1),
  1596. length = props.length,
  1597. result = {};
  1598. while (++index < length) {
  1599. var key = props[index];
  1600. if (key in object) {
  1601. result[key] = object[key];
  1602. }
  1603. }
  1604. return result;
  1605. }
  1606. /**
  1607. * Creates an array composed of the own enumerable property values of `object`.
  1608. *
  1609. * @static
  1610. * @memberOf _
  1611. * @category Objects
  1612. * @param {Object} object The object to inspect.
  1613. * @returns {Array} Returns an array of property values.
  1614. * @example
  1615. *
  1616. * _.values({ 'one': 1, 'two': 2, 'three': 3 });
  1617. * // => [1, 2, 3] (property order is not guaranteed across environments)
  1618. */
  1619. function values(object) {
  1620. var index = -1,
  1621. props = keys(object),
  1622. length = props.length,
  1623. result = Array(length);
  1624. while (++index < length) {
  1625. result[index] = object[props[index]];
  1626. }
  1627. return result;
  1628. }
  1629. /*--------------------------------------------------------------------------*/
  1630. /**
  1631. * Checks if a given value is present in a collection using strict equality
  1632. * for comparisons, i.e. `===`. If `fromIndex` is negative, it is used as the
  1633. * offset from the end of the collection.
  1634. *
  1635. * @static
  1636. * @memberOf _
  1637. * @alias include
  1638. * @category Collections
  1639. * @param {Array|Object|string} collection The collection to iterate over.
  1640. * @param {*} target The value to check for.
  1641. * @param {number} [fromIndex=0] The index to search from.
  1642. * @returns {boolean} Returns `true` if the `target` element is found, else `false`.
  1643. * @example
  1644. *
  1645. * _.contains([1, 2, 3], 1);
  1646. * // => true
  1647. *
  1648. * _.contains([1, 2, 3], 1, 2);
  1649. * // => false
  1650. *
  1651. * _.contains({ 'name': 'fred', 'age': 40 }, 'fred');
  1652. * // => true
  1653. *
  1654. * _.contains('pebbles', 'eb');
  1655. * // => true
  1656. */
  1657. function contains(collection, target) {
  1658. var indexOf = getIndexOf(),
  1659. length = collection ? collection.length : 0,
  1660. result = false;
  1661. if (length && typeof length == 'number') {
  1662. result = indexOf(collection, target) > -1;
  1663. } else {
  1664. forOwn(collection, function(value) {
  1665. return (result = value === target) && indicatorObject;
  1666. });
  1667. }
  1668. return result;
  1669. }
  1670. /**
  1671. * Creates an object composed of keys generated from the results of running
  1672. * each element of `collection` through the callback. The corresponding value
  1673. * of each key is the number of times the key was returned by the callback.
  1674. * The callback is bound to `thisArg` and invoked with three arguments;
  1675. * (value, index|key, collection).
  1676. *
  1677. * If a property name is provided for `callback` the created "_.pluck" style
  1678. * callback will return the property value of the given element.
  1679. *
  1680. * If an object is provided for `callback` the created "_.where" style callback
  1681. * will return `true` for elements that have the properties of the given object,
  1682. * else `false`.
  1683. *
  1684. * @static
  1685. * @memberOf _
  1686. * @category Collections
  1687. * @param {Array|Object|string} collection The collection to iterate over.
  1688. * @param {Function|Object|string} [callback=identity] The function called
  1689. * per iteration. If a property name or object is provided it will be used
  1690. * to create a "_.pluck" or "_.where" style callback, respectively.
  1691. * @param {*} [thisArg] The `this` binding of `callback`.
  1692. * @returns {Object} Returns the composed aggregate object.
  1693. * @example
  1694. *
  1695. * _.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); });
  1696. * // => { '4': 1, '6': 2 }
  1697. *
  1698. * _.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
  1699. * // => { '4': 1, '6': 2 }
  1700. *
  1701. * _.countBy(['one', 'two', 'three'], 'length');
  1702. * // => { '3': 2, '5': 1 }
  1703. */
  1704. var countBy = createAggregator(function(result, value, key) {
  1705. (hasOwnProperty.call(result, key) ? result[key]++ : result[key] = 1);
  1706. });
  1707. /**
  1708. * Checks if the given callback returns truey value for **all** elements of
  1709. * a collection. The callback is bound to `thisArg` and invoked with three
  1710. * arguments; (value, index|key, collection).
  1711. *
  1712. * If a property name is provided for `callback` the created "_.pluck" style
  1713. * callback will return the property value of the given element.
  1714. *
  1715. * If an object is provided for `callback` the created "_.where" style callback
  1716. * will return `true` for elements that have the properties of the given object,
  1717. * else `false`.
  1718. *
  1719. * @static
  1720. * @memberOf _
  1721. * @alias all
  1722. * @category Collections
  1723. * @param {Array|Object|string} collection The collection to iterate over.
  1724. * @param {Function|Object|string} [callback=identity] The function called
  1725. * per iteration. If a property name or object is provided it will be used
  1726. * to create a "_.pluck" or "_.where" style callback, respectively.
  1727. * @param {*} [thisArg] The `this` binding of `callback`.
  1728. * @returns {boolean} Returns `true` if all elements passed the callback check,
  1729. * else `false`.
  1730. * @example
  1731. *
  1732. * _.every([true, 1, null, 'yes']);
  1733. * // => false
  1734. *
  1735. * var characters = [
  1736. * { 'name': 'barney', 'age': 36 },
  1737. * { 'name': 'fred', 'age': 40 }
  1738. * ];
  1739. *
  1740. * // using "_.pluck" callback shorthand
  1741. * _.every(characters, 'age');
  1742. * // => true
  1743. *
  1744. * // using "_.where" callback shorthand
  1745. * _.every(characters, { 'age': 36 });
  1746. * // => false
  1747. */
  1748. function every(collection, callback, thisArg) {
  1749. var result = true;
  1750. callback = createCallback(callback, thisArg, 3);
  1751. var index = -1,
  1752. length = collection ? collection.length : 0;
  1753. if (typeof length == 'number') {
  1754. while (++index < length) {
  1755. if (!(result = !!callback(collection[index], index, collection))) {
  1756. break;
  1757. }
  1758. }
  1759. } else {
  1760. forOwn(collection, function(value, index, collection) {
  1761. return !(result = !!callback(value, index, collection)) && indicatorObject;
  1762. });
  1763. }
  1764. return result;
  1765. }
  1766. /**
  1767. * Iterates over elements of a collection, returning an array of all elements
  1768. * the callback returns truey for. The callback is bound to `thisArg` and
  1769. * invoked with three arguments; (value, index|key, collection).
  1770. *
  1771. * If a property name is provided for `callback` the created "_.pluck" style
  1772. * callback will return the property value of the given element.
  1773. *
  1774. * If an object is provided for `callback` the created "_.where" style callback
  1775. * will return `true` for elements that have the properties of the given object,
  1776. * else `false`.
  1777. *
  1778. * @static
  1779. * @memberOf _
  1780. * @alias select
  1781. * @category Collections
  1782. * @param {Array|Object|string} collection The collection to iterate over.
  1783. * @param {Function|Object|string} [callback=identity] The function called
  1784. * per iteration. If a property name or object is provided it will be used
  1785. * to create a "_.pluck" or "_.where" style callback, respectively.
  1786. * @param {*} [thisArg] The `this` binding of `callback`.
  1787. * @returns {Array} Returns a new array of elements that passed the callback check.
  1788. * @example
  1789. *
  1790. * var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  1791. * // => [2, 4, 6]
  1792. *
  1793. * var characters = [
  1794. * { 'name': 'barney', 'age': 36, 'blocked': false },
  1795. * { 'name': 'fred', 'age': 40, 'blocked': true }
  1796. * ];
  1797. *
  1798. * // using "_.pluck" callback shorthand
  1799. * _.filter(characters, 'blocked');
  1800. * // => [{ 'name': 'fred', 'age': 40, 'blocked': true }]
  1801. *
  1802. * // using "_.where" callback shorthand
  1803. * _.filter(characters, { 'age': 36 });
  1804. * // => [{ 'name': 'barney', 'age': 36, 'blocked': false }]
  1805. */
  1806. function filter(collection, callback, thisArg) {
  1807. var result = [];
  1808. callback = createCallback(callback, thisArg, 3);
  1809. var index = -1,
  1810. length = collection ? collection.length : 0;
  1811. if (typeof length == 'number') {
  1812. while (++index < length) {
  1813. var value = collection[index];
  1814. if (callback(value, index, collection)) {
  1815. result.push(value);
  1816. }
  1817. }
  1818. } else {
  1819. forOwn(collection, function(value, index, collection) {
  1820. if (callback(value, index, collection)) {
  1821. result.push(value);
  1822. }
  1823. });
  1824. }
  1825. return result;
  1826. }
  1827. /**
  1828. * Iterates over elements of a collection, returning the first element that
  1829. * the callback returns truey for. The callback is bound to `thisArg` and
  1830. * invoked with three arguments; (value, index|key, collection).
  1831. *
  1832. * If a property name is provided for `callback` the created "_.pluck" style
  1833. * callback will return the property value of the given element.
  1834. *
  1835. * If an object is provided for `callback` the created "_.where" style callback
  1836. * will return `true` for elements that have the properties of the given object,
  1837. * else `false`.
  1838. *
  1839. * @static
  1840. * @memberOf _
  1841. * @alias detect, findWhere
  1842. * @category Collections
  1843. * @param {Array|Object|string} collection The collection to iterate over.
  1844. * @param {Function|Object|string} [callback=identity] The function called
  1845. * per iteration. If a property name or object is provided it will be used
  1846. * to create a "_.pluck" or "_.where" style callback, respectively.
  1847. * @param {*} [thisArg] The `this` binding of `callback`.
  1848. * @returns {*} Returns the found element, else `undefined`.
  1849. * @example
  1850. *
  1851. * var characters = [
  1852. * { 'name': 'barney', 'age': 36, 'blocked': false },
  1853. * { 'name': 'fred', 'age': 40, 'blocked': true },
  1854. * { 'name': 'pebbles', 'age': 1, 'blocked': false }
  1855. * ];
  1856. *
  1857. * _.find(characters, function(chr) {
  1858. * return chr.age < 40;
  1859. * });
  1860. * // => { 'name': 'barney', 'age': 36, 'blocked': false }
  1861. *
  1862. * // using "_.where" callback shorthand
  1863. * _.find(characters, { 'age': 1 });
  1864. * // => { 'name': 'pebbles', 'age': 1, 'blocked': false }
  1865. *
  1866. * // using "_.pluck" callback shorthand
  1867. * _.find(characters, 'blocked');
  1868. * // => { 'name': 'fred', 'age': 40, 'blocked': true }
  1869. */
  1870. function find(collection, callback, thisArg) {
  1871. callback = createCallback(callback, thisArg, 3);
  1872. var index = -1,
  1873. length = collection ? collection.length : 0;
  1874. if (typeof length == 'number') {
  1875. while (++index < length) {
  1876. var value = collection[index];
  1877. if (callback(value, index, collection)) {
  1878. return value;
  1879. }
  1880. }
  1881. } else {
  1882. var result;
  1883. forOwn(collection, function(value, index, collection) {
  1884. if (callback(value, index, collection)) {
  1885. result = value;
  1886. return indicatorObject;
  1887. }
  1888. });
  1889. return result;
  1890. }
  1891. }
  1892. /**
  1893. * Examines each element in a `collection`, returning the first that
  1894. * has the given properties. When checking `properties`, this method
  1895. * performs a deep comparison between values to determine if they are
  1896. * equivalent to each other.
  1897. *
  1898. * @static
  1899. * @memberOf _
  1900. * @category Collections
  1901. * @param {Array|Object|string} collection The collection to iterate over.
  1902. * @param {Object} properties The object of property values to filter by.
  1903. * @returns {*} Returns the found element, else `undefined`.
  1904. * @example
  1905. *
  1906. * var food = [
  1907. * { 'name': 'apple', 'organic': false, 'type': 'fruit' },
  1908. * { 'name': 'banana', 'organic': true, 'type': 'fruit' },
  1909. * { 'name': 'beet', 'organic': false, 'type': 'vegetable' }
  1910. * ];
  1911. *
  1912. * _.findWhere(food, { 'type': 'vegetable' });
  1913. * // => { 'name': 'beet', 'organic': false, 'type': 'vegetable' }
  1914. */
  1915. function findWhere(object, properties) {
  1916. return where(object, properties, true);
  1917. }
  1918. /**
  1919. * Iterates over elements of a collection, executing the callback for each
  1920. * element. The callback is bound to `thisArg` and invoked with three arguments;
  1921. * (value, index|key, collection). Callbacks may exit iteration early by
  1922. * explicitly returning `false`.
  1923. *
  1924. * Note: As with other "Collections" methods, objects with a `length` property
  1925. * are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn`
  1926. * may be used for object iteration.
  1927. *
  1928. * @static
  1929. * @memberOf _
  1930. * @alias each
  1931. * @category Collections
  1932. * @param {Array|Object|string} collection The collection to iterate over.
  1933. * @param {Function} [callback=identity] The function called per iteration.
  1934. * @param {*} [thisArg] The `this` binding of `callback`.
  1935. * @returns {Array|Object|string} Returns `collection`.
  1936. * @example
  1937. *
  1938. * _([1, 2, 3]).forEach(function(num) { console.log(num); }).join(',');
  1939. * // => logs each number and returns '1,2,3'
  1940. *
  1941. * _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); });
  1942. * // => logs each number and returns the object (property order is not guaranteed across environments)
  1943. */
  1944. function forEach(collection, callback, thisArg) {
  1945. var index = -1,
  1946. length = collection ? collection.length : 0;
  1947. callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3);
  1948. if (typeof length == 'number') {
  1949. while (++index < length) {
  1950. if (callback(collection[index], index, collection) === indicatorObject) {
  1951. break;
  1952. }
  1953. }
  1954. } else {
  1955. forOwn(collection, callback);
  1956. }
  1957. }
  1958. /**
  1959. * This method is like `_.forEach` except that it iterates over elements
  1960. * of a `collection` from right to left.
  1961. *
  1962. * @static
  1963. * @memberOf _
  1964. * @alias eachRight
  1965. * @category Collections
  1966. * @param {Array|Object|string} collection The collection to iterate over.
  1967. * @param {Function} [callback=identity] The function called per iteration.
  1968. * @param {*} [thisArg] The `this` binding of `callback`.
  1969. * @returns {Array|Object|string} Returns `collection`.
  1970. * @example
  1971. *
  1972. * _([1, 2, 3]).forEachRight(function(num) { console.log(num); }).join(',');
  1973. * // => logs each number from right to left and returns '3,2,1'
  1974. */
  1975. function forEachRight(collection, callback) {
  1976. var length = collection ? collection.length : 0;
  1977. if (typeof length == 'number') {
  1978. while (length--) {
  1979. if (callback(collection[length], length, collection) === false) {
  1980. break;
  1981. }
  1982. }
  1983. } else {
  1984. var props = keys(collection);
  1985. length = props.length;
  1986. forOwn(collection, function(value, key, collection) {
  1987. key = props ? props[--length] : --length;
  1988. return callback(collection[key], key, collection) === false && indicatorObject;
  1989. });
  1990. }
  1991. }
  1992. /**
  1993. * Creates an object composed of keys generated from the results of running
  1994. * each element of a collection through the callback. The corresponding value
  1995. * of each key is an array of the elements responsible for generating the key.
  1996. * The callback is bound to `thisArg` and invoked with three arguments;
  1997. * (value, index|key, collection).
  1998. *
  1999. * If a property name is provided for `callback` the created "_.pluck" style
  2000. * callback will return the property value of the given element.
  2001. *
  2002. * If an object is provided for `callback` the created "_.where" style callback
  2003. * will return `true` for elements that have the properties of the given object,
  2004. * else `false`
  2005. *
  2006. * @static
  2007. * @memberOf _
  2008. * @category Collections
  2009. * @param {Array|Object|string} collection The collection to iterate over.
  2010. * @param {Function|Object|string} [callback=identity] The function called
  2011. * per iteration. If a property name or object is provided it will be used
  2012. * to create a "_.pluck" or "_.where" style callback, respectively.
  2013. * @param {*} [thisArg] The `this` binding of `callback`.
  2014. * @returns {Object} Returns the composed aggregate object.
  2015. * @example
  2016. *
  2017. * _.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); });
  2018. * // => { '4': [4.2], '6': [6.1, 6.4] }
  2019. *
  2020. * _.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
  2021. * // => { '4': [4.2], '6': [6.1, 6.4] }
  2022. *
  2023. * // using "_.pluck" callback shorthand
  2024. * _.groupBy(['one', 'two', 'three'], 'length');
  2025. * // => { '3': ['one', 'two'], '5': ['three'] }
  2026. */
  2027. var groupBy = createAggregator(function(result, value, key) {
  2028. (hasOwnProperty.call(result, key) ? result[key] : result[key] = []).push(value);
  2029. });
  2030. /**
  2031. * Creates an object composed of keys generated from the results of running
  2032. * each element of the collection through the given callback. The corresponding
  2033. * value of each key is the last element responsible for generating the key.
  2034. * The callback is bound to `thisArg` and invoked with three arguments;
  2035. * (value, index|key, collection).
  2036. *
  2037. * If a property name is provided for `callback` the created "_.pluck" style
  2038. * callback will return the property value of the given element.
  2039. *
  2040. * If an object is provided for `callback` the created "_.where" style callback
  2041. * will return `true` for elements that have the properties of the given object,
  2042. * else `false`.
  2043. *
  2044. * @static
  2045. * @memberOf _
  2046. * @category Collections
  2047. * @param {Array|Object|string} collection The collection to iterate over.
  2048. * @param {Function|Object|string} [callback=identity] The function called
  2049. * per iteration. If a property name or object is provided it will be used
  2050. * to create a "_.pluck" or "_.where" style callback, respectively.
  2051. * @param {*} [thisArg] The `this` binding of `callback`.
  2052. * @returns {Object} Returns the composed aggregate object.
  2053. * @example
  2054. *
  2055. * var keys = [
  2056. * { 'dir': 'left', 'code': 97 },
  2057. * { 'dir': 'right', 'code': 100 }
  2058. * ];
  2059. *
  2060. * _.indexBy(keys, 'dir');
  2061. * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
  2062. *
  2063. * _.indexBy(keys, function(key) { return String.fromCharCode(key.code); });
  2064. * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  2065. *
  2066. * _.indexBy(characters, function(key) { this.fromCharCode(key.code); }, String);
  2067. * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  2068. */
  2069. var indexBy = createAggregator(function(result, value, key) {
  2070. result[key] = value;
  2071. });
  2072. /**
  2073. * Invokes the method named by `methodName` on each element in the `collection`
  2074. * returning an array of the results of each invoked method. Additional arguments
  2075. * will be provided to each invoked method. If `methodName` is a function it
  2076. * will be invoked for, and `this` bound to, each element in the `collection`.
  2077. *
  2078. * @static
  2079. * @memberOf _
  2080. * @category Collections
  2081. * @param {Array|Object|string} collection The collection to iterate over.
  2082. * @param {Function|string} methodName The name of the method to invoke or
  2083. * the function invoked per iteration.
  2084. * @param {...*} [arg] Arguments to invoke the method with.
  2085. * @returns {Array} Returns a new array of the results of each invoked method.
  2086. * @example
  2087. *
  2088. * _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  2089. * // => [[1, 5, 7], [1, 2, 3]]
  2090. *
  2091. * _.invoke([123, 456], String.prototype.split, '');
  2092. * // => [['1', '2', '3'], ['4', '5', '6']]
  2093. */
  2094. function invoke(collection, methodName) {
  2095. var args = slice(arguments, 2),
  2096. index = -1,
  2097. isFunc = typeof methodName == 'function',
  2098. length = collection ? collection.length : 0,
  2099. result = Array(typeof length == 'number' ? length : 0);
  2100. forEach(collection, function(value) {
  2101. result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
  2102. });
  2103. return result;
  2104. }
  2105. /**
  2106. * Creates an array of values by running each element in the collection
  2107. * through the callback. The callback is bound to `thisArg` and invoked with
  2108. * three arguments; (value, index|key, collection).
  2109. *
  2110. * If a property name is provided for `callback` the created "_.pluck" style
  2111. * callback will return the property value of the given element.
  2112. *
  2113. * If an object is provided for `callback` the created "_.where" style callback
  2114. * will return `true` for elements that have the properties of the given object,
  2115. * else `false`.
  2116. *
  2117. * @static
  2118. * @memberOf _
  2119. * @alias collect
  2120. * @category Collections
  2121. * @param {Array|Object|string} collection The collection to iterate over.
  2122. * @param {Function|Object|string} [callback=identity] The function called
  2123. * per iteration. If a property name or object is provided it will be used
  2124. * to create a "_.pluck" or "_.where" style callback, respectively.
  2125. * @param {*} [thisArg] The `this` binding of `callback`.
  2126. * @returns {Array} Returns a new array of the results of each `callback` execution.
  2127. * @example
  2128. *
  2129. * _.map([1, 2, 3], function(num) { return num * 3; });
  2130. * // => [3, 6, 9]
  2131. *
  2132. * _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
  2133. * // => [3, 6, 9] (property order is not guaranteed across environments)
  2134. *
  2135. * var characters = [
  2136. * { 'name': 'barney', 'age': 36 },
  2137. * { 'name': 'fred', 'age': 40 }
  2138. * ];
  2139. *
  2140. * // using "_.pluck" callback shorthand
  2141. * _.map(characters, 'name');
  2142. * // => ['barney', 'fred']
  2143. */
  2144. function map(collection, callback, thisArg) {
  2145. var index = -1,
  2146. length = collection ? collection.length : 0;
  2147. callback = createCallback(callback, thisArg, 3);
  2148. if (typeof length == 'number') {
  2149. var result = Array(length);
  2150. while (++index < length) {
  2151. result[index] = callback(collection[index], index, collection);
  2152. }
  2153. } else {
  2154. result = [];
  2155. forOwn(collection, function(value, key, collection) {
  2156. result[++index] = callback(value, key, collection);
  2157. });
  2158. }
  2159. return result;
  2160. }
  2161. /**
  2162. * Retrieves the maximum value of a collection. If the collection is empty or
  2163. * falsey `-Infinity` is returned. If a callback is provided it will be executed
  2164. * for each value in the collection to generate the criterion by which the value
  2165. * is ranked. The callback is bound to `thisArg` and invoked with three
  2166. * arguments; (value, index, collection).
  2167. *
  2168. * If a property name is provided for `callback` the created "_.pluck" style
  2169. * callback will return the property value of the given element.
  2170. *
  2171. * If an object is provided for `callback` the created "_.where" style callback
  2172. * will return `true` for elements that have the properties of the given object,
  2173. * else `false`.
  2174. *
  2175. * @static
  2176. * @memberOf _
  2177. * @category Collections
  2178. * @param {Array|Object|string} collection The collection to iterate over.
  2179. * @param {Function|Object|string} [callback=identity] The function called
  2180. * per iteration. If a property name or object is provided it will be used
  2181. * to create a "_.pluck" or "_.where" style callback, respectively.
  2182. * @param {*} [thisArg] The `this` binding of `callback`.
  2183. * @returns {*} Returns the maximum value.
  2184. * @example
  2185. *
  2186. * _.max([4, 2, 8, 6]);
  2187. * // => 8
  2188. *
  2189. * var characters = [
  2190. * { 'name': 'barney', 'age': 36 },
  2191. * { 'name': 'fred', 'age': 40 }
  2192. * ];
  2193. *
  2194. * _.max(characters, function(chr) { return chr.age; });
  2195. * // => { 'name': 'fred', 'age': 40 };
  2196. *
  2197. * // using "_.pluck" callback shorthand
  2198. * _.max(characters, 'age');
  2199. * // => { 'name': 'fred', 'age': 40 };
  2200. */
  2201. function max(collection, callback, thisArg) {
  2202. var computed = -Infinity,
  2203. result = computed;
  2204. // allows working with functions like `_.map` without using
  2205. // their `index` argument as a callback
  2206. if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) {
  2207. callback = null;
  2208. }
  2209. var index = -1,
  2210. length = collection ? collection.length : 0;
  2211. if (callback == null && typeof length == 'number') {
  2212. while (++index < length) {
  2213. var value = collection[index];
  2214. if (value > result) {
  2215. result = value;
  2216. }
  2217. }
  2218. } else {
  2219. callback = createCallback(callback, thisArg, 3);
  2220. forEach(collection, function(value, index, collection) {
  2221. var current = callback(value, index, collection);
  2222. if (current > computed) {
  2223. computed = current;
  2224. result = value;
  2225. }
  2226. });
  2227. }
  2228. return result;
  2229. }
  2230. /**
  2231. * Retrieves the minimum value of a collection. If the collection is empty or
  2232. * falsey `Infinity` is returned. If a callback is provided it will be executed
  2233. * for each value in the collection to generate the criterion by which the value
  2234. * is ranked. The callback is bound to `thisArg` and invoked with three
  2235. * arguments; (value, index, collection).
  2236. *
  2237. * If a property name is provided for `callback` the created "_.pluck" style
  2238. * callback will return the property value of the given element.
  2239. *
  2240. * If an object is provided for `callback` the created "_.where" style callback
  2241. * will return `true` for elements that have the properties of the given object,
  2242. * else `false`.
  2243. *
  2244. * @static
  2245. * @memberOf _
  2246. * @category Collections
  2247. * @param {Array|Object|string} collection The collection to iterate over.
  2248. * @param {Function|Object|string} [callback=identity] The function called
  2249. * per iteration. If a property name or object is provided it will be used
  2250. * to create a "_.pluck" or "_.where" style callback, respectively.
  2251. * @param {*} [thisArg] The `this` binding of `callback`.
  2252. * @returns {*} Returns the minimum value.
  2253. * @example
  2254. *
  2255. * _.min([4, 2, 8, 6]);
  2256. * // => 2
  2257. *
  2258. * var characters = [
  2259. * { 'name': 'barney', 'age': 36 },
  2260. * { 'name': 'fred', 'age': 40 }
  2261. * ];
  2262. *
  2263. * _.min(characters, function(chr) { return chr.age; });
  2264. * // => { 'name': 'barney', 'age': 36 };
  2265. *
  2266. * // using "_.pluck" callback shorthand
  2267. * _.min(characters, 'age');
  2268. * // => { 'name': 'barney', 'age': 36 };
  2269. */
  2270. function min(collection, callback, thisArg) {
  2271. var computed = Infinity,
  2272. result = computed;
  2273. // allows working with functions like `_.map` without using
  2274. // their `index` argument as a callback
  2275. if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) {
  2276. callback = null;
  2277. }
  2278. var index = -1,
  2279. length = collection ? collection.length : 0;
  2280. if (callback == null && typeof length == 'number') {
  2281. while (++index < length) {
  2282. var value = collection[index];
  2283. if (value < result) {
  2284. result = value;
  2285. }
  2286. }
  2287. } else {
  2288. callback = createCallback(callback, thisArg, 3);
  2289. forEach(collection, function(value, index, collection) {
  2290. var current = callback(value, index, collection);
  2291. if (current < computed) {
  2292. computed = current;
  2293. result = value;
  2294. }
  2295. });
  2296. }
  2297. return result;
  2298. }
  2299. /**
  2300. * Retrieves the value of a specified property from all elements in the collection.
  2301. *
  2302. * @static
  2303. * @memberOf _
  2304. * @type Function
  2305. * @category Collections
  2306. * @param {Array|Object|string} collection The collection to iterate over.
  2307. * @param {string} property The name of the property to pluck.
  2308. * @returns {Array} Returns a new array of property values.
  2309. * @example
  2310. *
  2311. * var characters = [
  2312. * { 'name': 'barney', 'age': 36 },
  2313. * { 'name': 'fred', 'age': 40 }
  2314. * ];
  2315. *
  2316. * _.pluck(characters, 'name');
  2317. * // => ['barney', 'fred']
  2318. */
  2319. var pluck = map;
  2320. /**
  2321. * Reduces a collection to a value which is the accumulated result of running
  2322. * each element in the collection through the callback, where each successive
  2323. * callback execution consumes the return value of the previous execution. If
  2324. * `accumulator` is not provided the first element of the collection will be
  2325. * used as the initial `accumulator` value. The callback is bound to `thisArg`
  2326. * and invoked with four arguments; (accumulator, value, index|key, collection).
  2327. *
  2328. * @static
  2329. * @memberOf _
  2330. * @alias foldl, inject
  2331. * @category Collections
  2332. * @param {Array|Object|string} collection The collection to iterate over.
  2333. * @param {Function} [callback=identity] The function called per iteration.
  2334. * @param {*} [accumulator] Initial value of the accumulator.
  2335. * @param {*} [thisArg] The `this` binding of `callback`.
  2336. * @returns {*} Returns the accumulated value.
  2337. * @example
  2338. *
  2339. * var sum = _.reduce([1, 2, 3], function(sum, num) {
  2340. * return sum + num;
  2341. * });
  2342. * // => 6
  2343. *
  2344. * var mapped = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
  2345. * result[key] = num * 3;
  2346. * return result;
  2347. * }, {});
  2348. * // => { 'a': 3, 'b': 6, 'c': 9 }
  2349. */
  2350. function reduce(collection, callback, accumulator, thisArg) {
  2351. if (!collection) return accumulator;
  2352. var noaccum = arguments.length < 3;
  2353. callback = createCallback(callback, thisArg, 4);
  2354. var index = -1,
  2355. length = collection.length;
  2356. if (typeof length == 'number') {
  2357. if (noaccum) {
  2358. accumulator = collection[++index];
  2359. }
  2360. while (++index < length) {
  2361. accumulator = callback(accumulator, collection[index], index, collection);
  2362. }
  2363. } else {
  2364. forOwn(collection, function(value, index, collection) {
  2365. accumulator = noaccum
  2366. ? (noaccum = false, value)
  2367. : callback(accumulator, value, index, collection)
  2368. });
  2369. }
  2370. return accumulator;
  2371. }
  2372. /**
  2373. * This method is like `_.reduce` except that it iterates over elements
  2374. * of a `collection` from right to left.
  2375. *
  2376. * @static
  2377. * @memberOf _
  2378. * @alias foldr
  2379. * @category Collections
  2380. * @param {Array|Object|string} collection The collection to iterate over.
  2381. * @param {Function} [callback=identity] The function called per iteration.
  2382. * @param {*} [accumulator] Initial value of the accumulator.
  2383. * @param {*} [thisArg] The `this` binding of `callback`.
  2384. * @returns {*} Returns the accumulated value.
  2385. * @example
  2386. *
  2387. * var list = [[0, 1], [2, 3], [4, 5]];
  2388. * var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
  2389. * // => [4, 5, 2, 3, 0, 1]
  2390. */
  2391. function reduceRight(collection, callback, accumulator, thisArg) {
  2392. var noaccum = arguments.length < 3;
  2393. callback = createCallback(callback, thisArg, 4);
  2394. forEachRight(collection, function(value, index, collection) {
  2395. accumulator = noaccum
  2396. ? (noaccum = false, value)
  2397. : callback(accumulator, value, index, collection);
  2398. });
  2399. return accumulator;
  2400. }
  2401. /**
  2402. * The opposite of `_.filter` this method returns the elements of a
  2403. * collection that the callback does **not** return truey for.
  2404. *
  2405. * If a property name is provided for `callback` the created "_.pluck" style
  2406. * callback will return the property value of the given element.
  2407. *
  2408. * If an object is provided for `callback` the created "_.where" style callback
  2409. * will return `true` for elements that have the properties of the given object,
  2410. * else `false`.
  2411. *
  2412. * @static
  2413. * @memberOf _
  2414. * @category Collections
  2415. * @param {Array|Object|string} collection The collection to iterate over.
  2416. * @param {Function|Object|string} [callback=identity] The function called
  2417. * per iteration. If a property name or object is provided it will be used
  2418. * to create a "_.pluck" or "_.where" style callback, respectively.
  2419. * @param {*} [thisArg] The `this` binding of `callback`.
  2420. * @returns {Array} Returns a new array of elements that failed the callback check.
  2421. * @example
  2422. *
  2423. * var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  2424. * // => [1, 3, 5]
  2425. *
  2426. * var characters = [
  2427. * { 'name': 'barney', 'age': 36, 'blocked': false },
  2428. * { 'name': 'fred', 'age': 40, 'blocked': true }
  2429. * ];
  2430. *
  2431. * // using "_.pluck" callback shorthand
  2432. * _.reject(characters, 'blocked');
  2433. * // => [{ 'name': 'barney', 'age': 36, 'blocked': false }]
  2434. *
  2435. * // using "_.where" callback shorthand
  2436. * _.reject(characters, { 'age': 36 });
  2437. * // => [{ 'name': 'fred', 'age': 40, 'blocked': true }]
  2438. */
  2439. function reject(collection, callback, thisArg) {
  2440. callback = createCallback(callback, thisArg, 3);
  2441. return filter(collection, function(value, index, collection) {
  2442. return !callback(value, index, collection);
  2443. });
  2444. }
  2445. /**
  2446. * Retrieves a random element or `n` random elements from a collection.
  2447. *
  2448. * @static
  2449. * @memberOf _
  2450. * @category Collections
  2451. * @param {Array|Object|string} collection The collection to sample.
  2452. * @param {number} [n] The number of elements to sample.
  2453. * @param- {Object} [guard] Allows working with functions like `_.map`
  2454. * without using their `index` arguments as `n`.
  2455. * @returns {Array} Returns the random sample(s) of `collection`.
  2456. * @example
  2457. *
  2458. * _.sample([1, 2, 3, 4]);
  2459. * // => 2
  2460. *
  2461. * _.sample([1, 2, 3, 4], 2);
  2462. * // => [3, 1]
  2463. */
  2464. function sample(collection, n, guard) {
  2465. if (collection && typeof collection.length != 'number') {
  2466. collection = values(collection);
  2467. }
  2468. if (n == null || guard) {
  2469. return collection ? collection[baseRandom(0, collection.length - 1)] : undefined;
  2470. }
  2471. var result = shuffle(collection);
  2472. result.length = nativeMin(nativeMax(0, n), result.length);
  2473. return result;
  2474. }
  2475. /**
  2476. * Creates an array of shuffled values, using a version of the Fisher-Yates
  2477. * shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle.
  2478. *
  2479. * @static
  2480. * @memberOf _
  2481. * @category Collections
  2482. * @param {Array|Object|string} collection The collection to shuffle.
  2483. * @returns {Array} Returns a new shuffled collection.
  2484. * @example
  2485. *
  2486. * _.shuffle([1, 2, 3, 4, 5, 6]);
  2487. * // => [4, 1, 6, 3, 5, 2]
  2488. */
  2489. function shuffle(collection) {
  2490. var index = -1,
  2491. length = collection ? collection.length : 0,
  2492. result = Array(typeof length == 'number' ? length : 0);
  2493. forEach(collection, function(value) {
  2494. var rand = baseRandom(0, ++index);
  2495. result[index] = result[rand];
  2496. result[rand] = value;
  2497. });
  2498. return result;
  2499. }
  2500. /**
  2501. * Gets the size of the `collection` by returning `collection.length` for arrays
  2502. * and array-like objects or the number of own enumerable properties for objects.
  2503. *
  2504. * @static
  2505. * @memberOf _
  2506. * @category Collections
  2507. * @param {Array|Object|string} collection The collection to inspect.
  2508. * @returns {number} Returns `collection.length` or number of own enumerable properties.
  2509. * @example
  2510. *
  2511. * _.size([1, 2]);
  2512. * // => 2
  2513. *
  2514. * _.size({ 'one': 1, 'two': 2, 'three': 3 });
  2515. * // => 3
  2516. *
  2517. * _.size('pebbles');
  2518. * // => 7
  2519. */
  2520. function size(collection) {
  2521. var length = collection ? collection.length : 0;
  2522. return typeof length == 'number' ? length : keys(collection).length;
  2523. }
  2524. /**
  2525. * Checks if the callback returns a truey value for **any** element of a
  2526. * collection. The function returns as soon as it finds a passing value and
  2527. * does not iterate over the entire collection. The callback is bound to
  2528. * `thisArg` and invoked with three arguments; (value, index|key, collection).
  2529. *
  2530. * If a property name is provided for `callback` the created "_.pluck" style
  2531. * callback will return the property value of the given element.
  2532. *
  2533. * If an object is provided for `callback` the created "_.where" style callback
  2534. * will return `true` for elements that have the properties of the given object,
  2535. * else `false`.
  2536. *
  2537. * @static
  2538. * @memberOf _
  2539. * @alias any
  2540. * @category Collections
  2541. * @param {Array|Object|string} collection The collection to iterate over.
  2542. * @param {Function|Object|string} [callback=identity] The function called
  2543. * per iteration. If a property name or object is provided it will be used
  2544. * to create a "_.pluck" or "_.where" style callback, respectively.
  2545. * @param {*} [thisArg] The `this` binding of `callback`.
  2546. * @returns {boolean} Returns `true` if any element passed the callback check,
  2547. * else `false`.
  2548. * @example
  2549. *
  2550. * _.some([null, 0, 'yes', false], Boolean);
  2551. * // => true
  2552. *
  2553. * var characters = [
  2554. * { 'name': 'barney', 'age': 36, 'blocked': false },
  2555. * { 'name': 'fred', 'age': 40, 'blocked': true }
  2556. * ];
  2557. *
  2558. * // using "_.pluck" callback shorthand
  2559. * _.some(characters, 'blocked');
  2560. * // => true
  2561. *
  2562. * // using "_.where" callback shorthand
  2563. * _.some(characters, { 'age': 1 });
  2564. * // => false
  2565. */
  2566. function some(collection, callback, thisArg) {
  2567. var result;
  2568. callback = createCallback(callback, thisArg, 3);
  2569. var index = -1,
  2570. length = collection ? collection.length : 0;
  2571. if (typeof length == 'number') {
  2572. while (++index < length) {
  2573. if ((result = callback(collection[index], index, collection))) {
  2574. break;
  2575. }
  2576. }
  2577. } else {
  2578. forOwn(collection, function(value, index, collection) {
  2579. return (result = callback(value, index, collection)) && indicatorObject;
  2580. });
  2581. }
  2582. return !!result;
  2583. }
  2584. /**
  2585. * Creates an array of elements, sorted in ascending order by the results of
  2586. * running each element in a collection through the callback. This method
  2587. * performs a stable sort, that is, it will preserve the original sort order
  2588. * of equal elements. The callback is bound to `thisArg` and invoked with
  2589. * three arguments; (value, index|key, collection).
  2590. *
  2591. * If a property name is provided for `callback` the created "_.pluck" style
  2592. * callback will return the property value of the given element.
  2593. *
  2594. * If an array of property names is provided for `callback` the collection
  2595. * will be sorted by each property value.
  2596. *
  2597. * If an object is provided for `callback` the created "_.where" style callback
  2598. * will return `true` for elements that have the properties of the given object,
  2599. * else `false`.
  2600. *
  2601. * @static
  2602. * @memberOf _
  2603. * @category Collections
  2604. * @param {Array|Object|string} collection The collection to iterate over.
  2605. * @param {Array|Function|Object|string} [callback=identity] The function called
  2606. * per iteration. If a property name or object is provided it will be used
  2607. * to create a "_.pluck" or "_.where" style callback, respectively.
  2608. * @param {*} [thisArg] The `this` binding of `callback`.
  2609. * @returns {Array} Returns a new array of sorted elements.
  2610. * @example
  2611. *
  2612. * _.sortBy([1, 2, 3], function(num) { return Math.sin(num); });
  2613. * // => [3, 1, 2]
  2614. *
  2615. * _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
  2616. * // => [3, 1, 2]
  2617. *
  2618. * var characters = [
  2619. * { 'name': 'barney', 'age': 36 },
  2620. * { 'name': 'fred', 'age': 40 },
  2621. * { 'name': 'barney', 'age': 26 },
  2622. * { 'name': 'fred', 'age': 30 }
  2623. * ];
  2624. *
  2625. * // using "_.pluck" callback shorthand
  2626. * _.map(_.sortBy(characters, 'age'), _.values);
  2627. * // => [['barney', 26], ['fred', 30], ['barney', 36], ['fred', 40]]
  2628. *
  2629. * // sorting by multiple properties
  2630. * _.map(_.sortBy(characters, ['name', 'age']), _.values);
  2631. * // = > [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]]
  2632. */
  2633. function sortBy(collection, callback, thisArg) {
  2634. var index = -1,
  2635. length = collection ? collection.length : 0,
  2636. result = Array(typeof length == 'number' ? length : 0);
  2637. callback = createCallback(callback, thisArg, 3);
  2638. forEach(collection, function(value, key, collection) {
  2639. result[++index] = {
  2640. 'criteria': [callback(value, key, collection)],
  2641. 'index': index,
  2642. 'value': value
  2643. };
  2644. });
  2645. length = result.length;
  2646. result.sort(compareAscending);
  2647. while (length--) {
  2648. result[length] = result[length].value;
  2649. }
  2650. return result;
  2651. }
  2652. /**
  2653. * Converts the `collection` to an array.
  2654. *
  2655. * @static
  2656. * @memberOf _
  2657. * @category Collections
  2658. * @param {Array|Object|string} collection The collection to convert.
  2659. * @returns {Array} Returns the new converted array.
  2660. * @example
  2661. *
  2662. * (function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
  2663. * // => [2, 3, 4]
  2664. */
  2665. function toArray(collection) {
  2666. if (isArray(collection)) {
  2667. return slice(collection);
  2668. }
  2669. if (collection && typeof collection.length == 'number') {
  2670. return map(collection);
  2671. }
  2672. return values(collection);
  2673. }
  2674. /**
  2675. * Performs a deep comparison of each element in a `collection` to the given
  2676. * `properties` object, returning an array of all elements that have equivalent
  2677. * property values.
  2678. *
  2679. * @static
  2680. * @memberOf _
  2681. * @type Function
  2682. * @category Collections
  2683. * @param {Array|Object|string} collection The collection to iterate over.
  2684. * @param {Object} props The object of property values to filter by.
  2685. * @returns {Array} Returns a new array of elements that have the given properties.
  2686. * @example
  2687. *
  2688. * var characters = [
  2689. * { 'name': 'barney', 'age': 36, 'pets': ['hoppy'] },
  2690. * { 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }
  2691. * ];
  2692. *
  2693. * _.where(characters, { 'age': 36 });
  2694. * // => [{ 'name': 'barney', 'age': 36, 'pets': ['hoppy'] }]
  2695. *
  2696. * _.where(characters, { 'pets': ['dino'] });
  2697. * // => [{ 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }]
  2698. */
  2699. function where(collection, properties, first) {
  2700. return (first && isEmpty(properties))
  2701. ? undefined
  2702. : (first ? find : filter)(collection, properties);
  2703. }
  2704. /*--------------------------------------------------------------------------*/
  2705. /**
  2706. * Creates an array with all falsey values removed. The values `false`, `null`,
  2707. * `0`, `""`, `undefined`, and `NaN` are all falsey.
  2708. *
  2709. * @static
  2710. * @memberOf _
  2711. * @category Arrays
  2712. * @param {Array} array The array to compact.
  2713. * @returns {Array} Returns a new array of filtered values.
  2714. * @example
  2715. *
  2716. * _.compact([0, 1, false, 2, '', 3]);
  2717. * // => [1, 2, 3]
  2718. */
  2719. function compact(array) {
  2720. var index = -1,
  2721. length = array ? array.length : 0,
  2722. result = [];
  2723. while (++index < length) {
  2724. var value = array[index];
  2725. if (value) {
  2726. result.push(value);
  2727. }
  2728. }
  2729. return result;
  2730. }
  2731. /**
  2732. * Creates an array excluding all values of the provided arrays using strict
  2733. * equality for comparisons, i.e. `===`.
  2734. *
  2735. * @static
  2736. * @memberOf _
  2737. * @category Arrays
  2738. * @param {Array} array The array to process.
  2739. * @param {...Array} [values] The arrays of values to exclude.
  2740. * @returns {Array} Returns a new array of filtered values.
  2741. * @example
  2742. *
  2743. * _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
  2744. * // => [1, 3, 4]
  2745. */
  2746. function difference(array) {
  2747. return baseDifference(array, baseFlatten(arguments, true, true, 1));
  2748. }
  2749. /**
  2750. * Gets the first element or first `n` elements of an array. If a callback
  2751. * is provided elements at the beginning of the array are returned as long
  2752. * as the callback returns truey. The callback is bound to `thisArg` and
  2753. * invoked with three arguments; (value, index, array).
  2754. *
  2755. * If a property name is provided for `callback` the created "_.pluck" style
  2756. * callback will return the property value of the given element.
  2757. *
  2758. * If an object is provided for `callback` the created "_.where" style callback
  2759. * will return `true` for elements that have the properties of the given object,
  2760. * else `false`.
  2761. *
  2762. * @static
  2763. * @memberOf _
  2764. * @alias head, take
  2765. * @category Arrays
  2766. * @param {Array} array The array to query.
  2767. * @param {Function|Object|number|string} [callback] The function called
  2768. * per element or the number of elements to return. If a property name or
  2769. * object is provided it will be used to create a "_.pluck" or "_.where"
  2770. * style callback, respectively.
  2771. * @param {*} [thisArg] The `this` binding of `callback`.
  2772. * @returns {*} Returns the first element(s) of `array`.
  2773. * @example
  2774. *
  2775. * _.first([1, 2, 3]);
  2776. * // => 1
  2777. *
  2778. * _.first([1, 2, 3], 2);
  2779. * // => [1, 2]
  2780. *
  2781. * _.first([1, 2, 3], function(num) {
  2782. * return num < 3;
  2783. * });
  2784. * // => [1, 2]
  2785. *
  2786. * var characters = [
  2787. * { 'name': 'barney', 'blocked': true, 'employer': 'slate' },
  2788. * { 'name': 'fred', 'blocked': false, 'employer': 'slate' },
  2789. * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
  2790. * ];
  2791. *
  2792. * // using "_.pluck" callback shorthand
  2793. * _.first(characters, 'blocked');
  2794. * // => [{ 'name': 'barney', 'blocked': true, 'employer': 'slate' }]
  2795. *
  2796. * // using "_.where" callback shorthand
  2797. * _.pluck(_.first(characters, { 'employer': 'slate' }), 'name');
  2798. * // => ['barney', 'fred']
  2799. */
  2800. function first(array, callback, thisArg) {
  2801. var n = 0,
  2802. length = array ? array.length : 0;
  2803. if (typeof callback != 'number' && callback != null) {
  2804. var index = -1;
  2805. callback = createCallback(callback, thisArg, 3);
  2806. while (++index < length && callback(array[index], index, array)) {
  2807. n++;
  2808. }
  2809. } else {
  2810. n = callback;
  2811. if (n == null || thisArg) {
  2812. return array ? array[0] : undefined;
  2813. }
  2814. }
  2815. return slice(array, 0, nativeMin(nativeMax(0, n), length));
  2816. }
  2817. /**
  2818. * Flattens a nested array (the nesting can be to any depth). If `isShallow`
  2819. * is truey, the array will only be flattened a single level. If a callback
  2820. * is provided each element of the array is passed through the callback before
  2821. * flattening. The callback is bound to `thisArg` and invoked with three
  2822. * arguments; (value, index, array).
  2823. *
  2824. * If a property name is provided for `callback` the created "_.pluck" style
  2825. * callback will return the property value of the given element.
  2826. *
  2827. * If an object is provided for `callback` the created "_.where" style callback
  2828. * will return `true` for elements that have the properties of the given object,
  2829. * else `false`.
  2830. *
  2831. * @static
  2832. * @memberOf _
  2833. * @category Arrays
  2834. * @param {Array} array The array to flatten.
  2835. * @param {boolean} [isShallow=false] A flag to restrict flattening to a single level.
  2836. * @param {Function|Object|string} [callback=identity] The function called
  2837. * per iteration. If a property name or object is provided it will be used
  2838. * to create a "_.pluck" or "_.where" style callback, respectively.
  2839. * @param {*} [thisArg] The `this` binding of `callback`.
  2840. * @returns {Array} Returns a new flattened array.
  2841. * @example
  2842. *
  2843. * _.flatten([1, [2], [3, [[4]]]]);
  2844. * // => [1, 2, 3, 4];
  2845. *
  2846. * _.flatten([1, [2], [3, [[4]]]], true);
  2847. * // => [1, 2, 3, [[4]]];
  2848. *
  2849. * var characters = [
  2850. * { 'name': 'barney', 'age': 30, 'pets': ['hoppy'] },
  2851. * { 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }
  2852. * ];
  2853. *
  2854. * // using "_.pluck" callback shorthand
  2855. * _.flatten(characters, 'pets');
  2856. * // => ['hoppy', 'baby puss', 'dino']
  2857. */
  2858. function flatten(array, isShallow) {
  2859. return baseFlatten(array, isShallow);
  2860. }
  2861. /**
  2862. * Gets the index at which the first occurrence of `value` is found using
  2863. * strict equality for comparisons, i.e. `===`. If the array is already sorted
  2864. * providing `true` for `fromIndex` will run a faster binary search.
  2865. *
  2866. * @static
  2867. * @memberOf _
  2868. * @category Arrays
  2869. * @param {Array} array The array to search.
  2870. * @param {*} value The value to search for.
  2871. * @param {boolean|number} [fromIndex=0] The index to search from or `true`
  2872. * to perform a binary search on a sorted array.
  2873. * @returns {number} Returns the index of the matched value or `-1`.
  2874. * @example
  2875. *
  2876. * _.indexOf([1, 2, 3, 1, 2, 3], 2);
  2877. * // => 1
  2878. *
  2879. * _.indexOf([1, 2, 3, 1, 2, 3], 2, 3);
  2880. * // => 4
  2881. *
  2882. * _.indexOf([1, 1, 2, 2, 3, 3], 2, true);
  2883. * // => 2
  2884. */
  2885. function indexOf(array, value, fromIndex) {
  2886. if (typeof fromIndex == 'number') {
  2887. var length = array ? array.length : 0;
  2888. fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0);
  2889. } else if (fromIndex) {
  2890. var index = sortedIndex(array, value);
  2891. return array[index] === value ? index : -1;
  2892. }
  2893. return baseIndexOf(array, value, fromIndex);
  2894. }
  2895. /**
  2896. * Gets all but the last element or last `n` elements of an array. If a
  2897. * callback is provided elements at the end of the array are excluded from
  2898. * the result as long as the callback returns truey. The callback is bound
  2899. * to `thisArg` and invoked with three arguments; (value, index, array).
  2900. *
  2901. * If a property name is provided for `callback` the created "_.pluck" style
  2902. * callback will return the property value of the given element.
  2903. *
  2904. * If an object is provided for `callback` the created "_.where" style callback
  2905. * will return `true` for elements that have the properties of the given object,
  2906. * else `false`.
  2907. *
  2908. * @static
  2909. * @memberOf _
  2910. * @category Arrays
  2911. * @param {Array} array The array to query.
  2912. * @param {Function|Object|number|string} [callback=1] The function called
  2913. * per element or the number of elements to exclude. If a property name or
  2914. * object is provided it will be used to create a "_.pluck" or "_.where"
  2915. * style callback, respectively.
  2916. * @param {*} [thisArg] The `this` binding of `callback`.
  2917. * @returns {Array} Returns a slice of `array`.
  2918. * @example
  2919. *
  2920. * _.initial([1, 2, 3]);
  2921. * // => [1, 2]
  2922. *
  2923. * _.initial([1, 2, 3], 2);
  2924. * // => [1]
  2925. *
  2926. * _.initial([1, 2, 3], function(num) {
  2927. * return num > 1;
  2928. * });
  2929. * // => [1]
  2930. *
  2931. * var characters = [
  2932. * { 'name': 'barney', 'blocked': false, 'employer': 'slate' },
  2933. * { 'name': 'fred', 'blocked': true, 'employer': 'slate' },
  2934. * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
  2935. * ];
  2936. *
  2937. * // using "_.pluck" callback shorthand
  2938. * _.initial(characters, 'blocked');
  2939. * // => [{ 'name': 'barney', 'blocked': false, 'employer': 'slate' }]
  2940. *
  2941. * // using "_.where" callback shorthand
  2942. * _.pluck(_.initial(characters, { 'employer': 'na' }), 'name');
  2943. * // => ['barney', 'fred']
  2944. */
  2945. function initial(array, callback, thisArg) {
  2946. var n = 0,
  2947. length = array ? array.length : 0;
  2948. if (typeof callback != 'number' && callback != null) {
  2949. var index = length;
  2950. callback = createCallback(callback, thisArg, 3);
  2951. while (index-- && callback(array[index], index, array)) {
  2952. n++;
  2953. }
  2954. } else {
  2955. n = (callback == null || thisArg) ? 1 : callback || n;
  2956. }
  2957. return slice(array, 0, nativeMin(nativeMax(0, length - n), length));
  2958. }
  2959. /**
  2960. * Creates an array of unique values present in all provided arrays using
  2961. * strict equality for comparisons, i.e. `===`.
  2962. *
  2963. * @static
  2964. * @memberOf _
  2965. * @category Arrays
  2966. * @param {...Array} [array] The arrays to inspect.
  2967. * @returns {Array} Returns an array of shared values.
  2968. * @example
  2969. *
  2970. * _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);
  2971. * // => [1, 2]
  2972. */
  2973. function intersection() {
  2974. var args = [],
  2975. argsIndex = -1,
  2976. argsLength = arguments.length;
  2977. while (++argsIndex < argsLength) {
  2978. var value = arguments[argsIndex];
  2979. if (isArray(value) || isArguments(value)) {
  2980. args.push(value);
  2981. }
  2982. }
  2983. var array = args[0],
  2984. index = -1,
  2985. indexOf = getIndexOf(),
  2986. length = array ? array.length : 0,
  2987. result = [];
  2988. outer:
  2989. while (++index < length) {
  2990. value = array[index];
  2991. if (indexOf(result, value) < 0) {
  2992. var argsIndex = argsLength;
  2993. while (--argsIndex) {
  2994. if (indexOf(args[argsIndex], value) < 0) {
  2995. continue outer;
  2996. }
  2997. }
  2998. result.push(value);
  2999. }
  3000. }
  3001. return result;
  3002. }
  3003. /**
  3004. * Gets the last element or last `n` elements of an array. If a callback is
  3005. * provided elements at the end of the array are returned as long as the
  3006. * callback returns truey. The callback is bound to `thisArg` and invoked
  3007. * with three arguments; (value, index, array).
  3008. *
  3009. * If a property name is provided for `callback` the created "_.pluck" style
  3010. * callback will return the property value of the given element.
  3011. *
  3012. * If an object is provided for `callback` the created "_.where" style callback
  3013. * will return `true` for elements that have the properties of the given object,
  3014. * else `false`.
  3015. *
  3016. * @static
  3017. * @memberOf _
  3018. * @category Arrays
  3019. * @param {Array} array The array to query.
  3020. * @param {Function|Object|number|string} [callback] The function called
  3021. * per element or the number of elements to return. If a property name or
  3022. * object is provided it will be used to create a "_.pluck" or "_.where"
  3023. * style callback, respectively.
  3024. * @param {*} [thisArg] The `this` binding of `callback`.
  3025. * @returns {*} Returns the last element(s) of `array`.
  3026. * @example
  3027. *
  3028. * _.last([1, 2, 3]);
  3029. * // => 3
  3030. *
  3031. * _.last([1, 2, 3], 2);
  3032. * // => [2, 3]
  3033. *
  3034. * _.last([1, 2, 3], function(num) {
  3035. * return num > 1;
  3036. * });
  3037. * // => [2, 3]
  3038. *
  3039. * var characters = [
  3040. * { 'name': 'barney', 'blocked': false, 'employer': 'slate' },
  3041. * { 'name': 'fred', 'blocked': true, 'employer': 'slate' },
  3042. * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
  3043. * ];
  3044. *
  3045. * // using "_.pluck" callback shorthand
  3046. * _.pluck(_.last(characters, 'blocked'), 'name');
  3047. * // => ['fred', 'pebbles']
  3048. *
  3049. * // using "_.where" callback shorthand
  3050. * _.last(characters, { 'employer': 'na' });
  3051. * // => [{ 'name': 'pebbles', 'blocked': true, 'employer': 'na' }]
  3052. */
  3053. function last(array, callback, thisArg) {
  3054. var n = 0,
  3055. length = array ? array.length : 0;
  3056. if (typeof callback != 'number' && callback != null) {
  3057. var index = length;
  3058. callback = createCallback(callback, thisArg, 3);
  3059. while (index-- && callback(array[index], index, array)) {
  3060. n++;
  3061. }
  3062. } else {
  3063. n = callback;
  3064. if (n == null || thisArg) {
  3065. return array ? array[length - 1] : undefined;
  3066. }
  3067. }
  3068. return slice(array, nativeMax(0, length - n));
  3069. }
  3070. /**
  3071. * Gets the index at which the last occurrence of `value` is found using strict
  3072. * equality for comparisons, i.e. `===`. If `fromIndex` is negative, it is used
  3073. * as the offset from the end of the collection.
  3074. *
  3075. * If a property name is provided for `callback` the created "_.pluck" style
  3076. * callback will return the property value of the given element.
  3077. *
  3078. * If an object is provided for `callback` the created "_.where" style callback
  3079. * will return `true` for elements that have the properties of the given object,
  3080. * else `false`.
  3081. *
  3082. * @static
  3083. * @memberOf _
  3084. * @category Arrays
  3085. * @param {Array} array The array to search.
  3086. * @param {*} value The value to search for.
  3087. * @param {number} [fromIndex=array.length-1] The index to search from.
  3088. * @returns {number} Returns the index of the matched value or `-1`.
  3089. * @example
  3090. *
  3091. * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  3092. * // => 4
  3093. *
  3094. * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);
  3095. * // => 1
  3096. */
  3097. function lastIndexOf(array, value, fromIndex) {
  3098. var index = array ? array.length : 0;
  3099. if (typeof fromIndex == 'number') {
  3100. index = (fromIndex < 0 ? nativeMax(0, index + fromIndex) : nativeMin(fromIndex, index - 1)) + 1;
  3101. }
  3102. while (index--) {
  3103. if (array[index] === value) {
  3104. return index;
  3105. }
  3106. }
  3107. return -1;
  3108. }
  3109. /**
  3110. * Creates an array of numbers (positive and/or negative) progressing from
  3111. * `start` up to but not including `end`. If `start` is less than `stop` a
  3112. * zero-length range is created unless a negative `step` is specified.
  3113. *
  3114. * @static
  3115. * @memberOf _
  3116. * @category Arrays
  3117. * @param {number} [start=0] The start of the range.
  3118. * @param {number} end The end of the range.
  3119. * @param {number} [step=1] The value to increment or decrement by.
  3120. * @returns {Array} Returns a new range array.
  3121. * @example
  3122. *
  3123. * _.range(4);
  3124. * // => [0, 1, 2, 3]
  3125. *
  3126. * _.range(1, 5);
  3127. * // => [1, 2, 3, 4]
  3128. *
  3129. * _.range(0, 20, 5);
  3130. * // => [0, 5, 10, 15]
  3131. *
  3132. * _.range(0, -4, -1);
  3133. * // => [0, -1, -2, -3]
  3134. *
  3135. * _.range(1, 4, 0);
  3136. * // => [1, 1, 1]
  3137. *
  3138. * _.range(0);
  3139. * // => []
  3140. */
  3141. function range(start, end, step) {
  3142. start = +start || 0;
  3143. step = (+step || 1);
  3144. if (end == null) {
  3145. end = start;
  3146. start = 0;
  3147. }
  3148. // use `Array(length)` so engines like Chakra and V8 avoid slower modes
  3149. // http://youtu.be/XAqIpGU8ZZk#t=17m25s
  3150. var index = -1,
  3151. length = nativeMax(0, ceil((end - start) / step)),
  3152. result = Array(length);
  3153. while (++index < length) {
  3154. result[index] = start;
  3155. start += step;
  3156. }
  3157. return result;
  3158. }
  3159. /**
  3160. * The opposite of `_.initial` this method gets all but the first element or
  3161. * first `n` elements of an array. If a callback function is provided elements
  3162. * at the beginning of the array are excluded from the result as long as the
  3163. * callback returns truey. The callback is bound to `thisArg` and invoked
  3164. * with three arguments; (value, index, array).
  3165. *
  3166. * If a property name is provided for `callback` the created "_.pluck" style
  3167. * callback will return the property value of the given element.
  3168. *
  3169. * If an object is provided for `callback` the created "_.where" style callback
  3170. * will return `true` for elements that have the properties of the given object,
  3171. * else `false`.
  3172. *
  3173. * @static
  3174. * @memberOf _
  3175. * @alias drop, tail
  3176. * @category Arrays
  3177. * @param {Array} array The array to query.
  3178. * @param {Function|Object|number|string} [callback=1] The function called
  3179. * per element or the number of elements to exclude. If a property name or
  3180. * object is provided it will be used to create a "_.pluck" or "_.where"
  3181. * style callback, respectively.
  3182. * @param {*} [thisArg] The `this` binding of `callback`.
  3183. * @returns {Array} Returns a slice of `array`.
  3184. * @example
  3185. *
  3186. * _.rest([1, 2, 3]);
  3187. * // => [2, 3]
  3188. *
  3189. * _.rest([1, 2, 3], 2);
  3190. * // => [3]
  3191. *
  3192. * _.rest([1, 2, 3], function(num) {
  3193. * return num < 3;
  3194. * });
  3195. * // => [3]
  3196. *
  3197. * var characters = [
  3198. * { 'name': 'barney', 'blocked': true, 'employer': 'slate' },
  3199. * { 'name': 'fred', 'blocked': false, 'employer': 'slate' },
  3200. * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
  3201. * ];
  3202. *
  3203. * // using "_.pluck" callback shorthand
  3204. * _.pluck(_.rest(characters, 'blocked'), 'name');
  3205. * // => ['fred', 'pebbles']
  3206. *
  3207. * // using "_.where" callback shorthand
  3208. * _.rest(characters, { 'employer': 'slate' });
  3209. * // => [{ 'name': 'pebbles', 'blocked': true, 'employer': 'na' }]
  3210. */
  3211. function rest(array, callback, thisArg) {
  3212. if (typeof callback != 'number' && callback != null) {
  3213. var n = 0,
  3214. index = -1,
  3215. length = array ? array.length : 0;
  3216. callback = createCallback(callback, thisArg, 3);
  3217. while (++index < length && callback(array[index], index, array)) {
  3218. n++;
  3219. }
  3220. } else {
  3221. n = (callback == null || thisArg) ? 1 : nativeMax(0, callback);
  3222. }
  3223. return slice(array, n);
  3224. }
  3225. /**
  3226. * Uses a binary search to determine the smallest index at which a value
  3227. * should be inserted into a given sorted array in order to maintain the sort
  3228. * order of the array. If a callback is provided it will be executed for
  3229. * `value` and each element of `array` to compute their sort ranking. The
  3230. * callback is bound to `thisArg` and invoked with one argument; (value).
  3231. *
  3232. * If a property name is provided for `callback` the created "_.pluck" style
  3233. * callback will return the property value of the given element.
  3234. *
  3235. * If an object is provided for `callback` the created "_.where" style callback
  3236. * will return `true` for elements that have the properties of the given object,
  3237. * else `false`.
  3238. *
  3239. * @static
  3240. * @memberOf _
  3241. * @category Arrays
  3242. * @param {Array} array The array to inspect.
  3243. * @param {*} value The value to evaluate.
  3244. * @param {Function|Object|string} [callback=identity] The function called
  3245. * per iteration. If a property name or object is provided it will be used
  3246. * to create a "_.pluck" or "_.where" style callback, respectively.
  3247. * @param {*} [thisArg] The `this` binding of `callback`.
  3248. * @returns {number} Returns the index at which `value` should be inserted
  3249. * into `array`.
  3250. * @example
  3251. *
  3252. * _.sortedIndex([20, 30, 50], 40);
  3253. * // => 2
  3254. *
  3255. * // using "_.pluck" callback shorthand
  3256. * _.sortedIndex([{ 'x': 20 }, { 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
  3257. * // => 2
  3258. *
  3259. * var dict = {
  3260. * 'wordToNumber': { 'twenty': 20, 'thirty': 30, 'fourty': 40, 'fifty': 50 }
  3261. * };
  3262. *
  3263. * _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
  3264. * return dict.wordToNumber[word];
  3265. * });
  3266. * // => 2
  3267. *
  3268. * _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
  3269. * return this.wordToNumber[word];
  3270. * }, dict);
  3271. * // => 2
  3272. */
  3273. function sortedIndex(array, value, callback, thisArg) {
  3274. var low = 0,
  3275. high = array ? array.length : low;
  3276. // explicitly reference `identity` for better inlining in Firefox
  3277. callback = callback ? createCallback(callback, thisArg, 1) : identity;
  3278. value = callback(value);
  3279. while (low < high) {
  3280. var mid = (low + high) >>> 1;
  3281. (callback(array[mid]) < value)
  3282. ? low = mid + 1
  3283. : high = mid;
  3284. }
  3285. return low;
  3286. }
  3287. /**
  3288. * Creates an array of unique values, in order, of the provided arrays using
  3289. * strict equality for comparisons, i.e. `===`.
  3290. *
  3291. * @static
  3292. * @memberOf _
  3293. * @category Arrays
  3294. * @param {...Array} [array] The arrays to inspect.
  3295. * @returns {Array} Returns an array of combined values.
  3296. * @example
  3297. *
  3298. * _.union([1, 2, 3], [5, 2, 1, 4], [2, 1]);
  3299. * // => [1, 2, 3, 5, 4]
  3300. */
  3301. function union() {
  3302. return baseUniq(baseFlatten(arguments, true, true));
  3303. }
  3304. /**
  3305. * Creates a duplicate-value-free version of an array using strict equality
  3306. * for comparisons, i.e. `===`. If the array is sorted, providing
  3307. * `true` for `isSorted` will use a faster algorithm. If a callback is provided
  3308. * each element of `array` is passed through the callback before uniqueness
  3309. * is computed. The callback is bound to `thisArg` and invoked with three
  3310. * arguments; (value, index, array).
  3311. *
  3312. * If a property name is provided for `callback` the created "_.pluck" style
  3313. * callback will return the property value of the given element.
  3314. *
  3315. * If an object is provided for `callback` the created "_.where" style callback
  3316. * will return `true` for elements that have the properties of the given object,
  3317. * else `false`.
  3318. *
  3319. * @static
  3320. * @memberOf _
  3321. * @alias unique
  3322. * @category Arrays
  3323. * @param {Array} array The array to process.
  3324. * @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted.
  3325. * @param {Function|Object|string} [callback=identity] The function called
  3326. * per iteration. If a property name or object is provided it will be used
  3327. * to create a "_.pluck" or "_.where" style callback, respectively.
  3328. * @param {*} [thisArg] The `this` binding of `callback`.
  3329. * @returns {Array} Returns a duplicate-value-free array.
  3330. * @example
  3331. *
  3332. * _.uniq([1, 2, 1, 3, 1]);
  3333. * // => [1, 2, 3]
  3334. *
  3335. * _.uniq([1, 1, 2, 2, 3], true);
  3336. * // => [1, 2, 3]
  3337. *
  3338. * _.uniq(['A', 'b', 'C', 'a', 'B', 'c'], function(letter) { return letter.toLowerCase(); });
  3339. * // => ['A', 'b', 'C']
  3340. *
  3341. * _.uniq([1, 2.5, 3, 1.5, 2, 3.5], function(num) { return this.floor(num); }, Math);
  3342. * // => [1, 2.5, 3]
  3343. *
  3344. * // using "_.pluck" callback shorthand
  3345. * _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
  3346. * // => [{ 'x': 1 }, { 'x': 2 }]
  3347. */
  3348. function uniq(array, isSorted, callback, thisArg) {
  3349. // juggle arguments
  3350. if (typeof isSorted != 'boolean' && isSorted != null) {
  3351. thisArg = callback;
  3352. callback = (typeof isSorted != 'function' && thisArg && thisArg[isSorted] === array) ? null : isSorted;
  3353. isSorted = false;
  3354. }
  3355. if (callback != null) {
  3356. callback = createCallback(callback, thisArg, 3);
  3357. }
  3358. return baseUniq(array, isSorted, callback);
  3359. }
  3360. /**
  3361. * Creates an array excluding all provided values using strict equality for
  3362. * comparisons, i.e. `===`.
  3363. *
  3364. * @static
  3365. * @memberOf _
  3366. * @category Arrays
  3367. * @param {Array} array The array to filter.
  3368. * @param {...*} [value] The values to exclude.
  3369. * @returns {Array} Returns a new array of filtered values.
  3370. * @example
  3371. *
  3372. * _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  3373. * // => [2, 3, 4]
  3374. */
  3375. function without(array) {
  3376. return baseDifference(array, slice(arguments, 1));
  3377. }
  3378. /**
  3379. * Creates an array of grouped elements, the first of which contains the first
  3380. * elements of the given arrays, the second of which contains the second
  3381. * elements of the given arrays, and so on.
  3382. *
  3383. * @static
  3384. * @memberOf _
  3385. * @alias unzip
  3386. * @category Arrays
  3387. * @param {...Array} [array] Arrays to process.
  3388. * @returns {Array} Returns a new array of grouped elements.
  3389. * @example
  3390. *
  3391. * _.zip(['fred', 'barney'], [30, 40], [true, false]);
  3392. * // => [['fred', 30, true], ['barney', 40, false]]
  3393. */
  3394. function zip() {
  3395. var index = -1,
  3396. length = max(pluck(arguments, 'length')),
  3397. result = Array(length < 0 ? 0 : length);
  3398. while (++index < length) {
  3399. result[index] = pluck(arguments, index);
  3400. }
  3401. return result;
  3402. }
  3403. /**
  3404. * Creates an object composed from arrays of `keys` and `values`. Provide
  3405. * either a single two dimensional array, i.e. `[[key1, value1], [key2, value2]]`
  3406. * or two arrays, one of `keys` and one of corresponding `values`.
  3407. *
  3408. * @static
  3409. * @memberOf _
  3410. * @alias object
  3411. * @category Arrays
  3412. * @param {Array} keys The array of keys.
  3413. * @param {Array} [values=[]] The array of values.
  3414. * @returns {Object} Returns an object composed of the given keys and
  3415. * corresponding values.
  3416. * @example
  3417. *
  3418. * _.zipObject(['fred', 'barney'], [30, 40]);
  3419. * // => { 'fred': 30, 'barney': 40 }
  3420. */
  3421. function zipObject(keys, values) {
  3422. var index = -1,
  3423. length = keys ? keys.length : 0,
  3424. result = {};
  3425. if (!values && length && !isArray(keys[0])) {
  3426. values = [];
  3427. }
  3428. while (++index < length) {
  3429. var key = keys[index];
  3430. if (values) {
  3431. result[key] = values[index];
  3432. } else if (key) {
  3433. result[key[0]] = key[1];
  3434. }
  3435. }
  3436. return result;
  3437. }
  3438. /*--------------------------------------------------------------------------*/
  3439. /**
  3440. * Creates a function that executes `func`, with the `this` binding and
  3441. * arguments of the created function, only after being called `n` times.
  3442. *
  3443. * @static
  3444. * @memberOf _
  3445. * @category Functions
  3446. * @param {number} n The number of times the function must be called before
  3447. * `func` is executed.
  3448. * @param {Function} func The function to restrict.
  3449. * @returns {Function} Returns the new restricted function.
  3450. * @example
  3451. *
  3452. * var saves = ['profile', 'settings'];
  3453. *
  3454. * var done = _.after(saves.length, function() {
  3455. * console.log('Done saving!');
  3456. * });
  3457. *
  3458. * _.forEach(saves, function(type) {
  3459. * asyncSave({ 'type': type, 'complete': done });
  3460. * });
  3461. * // => logs 'Done saving!', after all saves have completed
  3462. */
  3463. function after(n, func) {
  3464. if (!isFunction(func)) {
  3465. throw new TypeError;
  3466. }
  3467. return function() {
  3468. if (--n < 1) {
  3469. return func.apply(this, arguments);
  3470. }
  3471. };
  3472. }
  3473. /**
  3474. * Creates a function that, when called, invokes `func` with the `this`
  3475. * binding of `thisArg` and prepends any additional `bind` arguments to those
  3476. * provided to the bound function.
  3477. *
  3478. * @static
  3479. * @memberOf _
  3480. * @category Functions
  3481. * @param {Function} func The function to bind.
  3482. * @param {*} [thisArg] The `this` binding of `func`.
  3483. * @param {...*} [arg] Arguments to be partially applied.
  3484. * @returns {Function} Returns the new bound function.
  3485. * @example
  3486. *
  3487. * var func = function(greeting) {
  3488. * return greeting + ' ' + this.name;
  3489. * };
  3490. *
  3491. * func = _.bind(func, { 'name': 'fred' }, 'hi');
  3492. * func();
  3493. * // => 'hi fred'
  3494. */
  3495. function bind(func, thisArg) {
  3496. return arguments.length > 2
  3497. ? createWrapper(func, 17, slice(arguments, 2), null, thisArg)
  3498. : createWrapper(func, 1, null, null, thisArg);
  3499. }
  3500. /**
  3501. * Binds methods of an object to the object itself, overwriting the existing
  3502. * method. Method names may be specified as individual arguments or as arrays
  3503. * of method names. If no method names are provided all the function properties
  3504. * of `object` will be bound.
  3505. *
  3506. * @static
  3507. * @memberOf _
  3508. * @category Functions
  3509. * @param {Object} object The object to bind and assign the bound methods to.
  3510. * @param {...string} [methodName] The object method names to
  3511. * bind, specified as individual method names or arrays of method names.
  3512. * @returns {Object} Returns `object`.
  3513. * @example
  3514. *
  3515. * var view = {
  3516. * 'label': 'docs',
  3517. * 'onClick': function() { console.log('clicked ' + this.label); }
  3518. * };
  3519. *
  3520. * _.bindAll(view);
  3521. * jQuery('#docs').on('click', view.onClick);
  3522. * // => logs 'clicked docs', when the button is clicked
  3523. */
  3524. function bindAll(object) {
  3525. var funcs = arguments.length > 1 ? baseFlatten(arguments, true, false, 1) : functions(object),
  3526. index = -1,
  3527. length = funcs.length;
  3528. while (++index < length) {
  3529. var key = funcs[index];
  3530. object[key] = createWrapper(object[key], 1, null, null, object);
  3531. }
  3532. return object;
  3533. }
  3534. /**
  3535. * Creates a function that is the composition of the provided functions,
  3536. * where each function consumes the return value of the function that follows.
  3537. * For example, composing the functions `f()`, `g()`, and `h()` produces `f(g(h()))`.
  3538. * Each function is executed with the `this` binding of the composed function.
  3539. *
  3540. * @static
  3541. * @memberOf _
  3542. * @category Functions
  3543. * @param {...Function} [func] Functions to compose.
  3544. * @returns {Function} Returns the new composed function.
  3545. * @example
  3546. *
  3547. * var realNameMap = {
  3548. * 'pebbles': 'penelope'
  3549. * };
  3550. *
  3551. * var format = function(name) {
  3552. * name = realNameMap[name.toLowerCase()] || name;
  3553. * return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();
  3554. * };
  3555. *
  3556. * var greet = function(formatted) {
  3557. * return 'Hiya ' + formatted + '!';
  3558. * };
  3559. *
  3560. * var welcome = _.compose(greet, format);
  3561. * welcome('pebbles');
  3562. * // => 'Hiya Penelope!'
  3563. */
  3564. function compose() {
  3565. var funcs = arguments,
  3566. length = funcs.length;
  3567. while (length--) {
  3568. if (!isFunction(funcs[length])) {
  3569. throw new TypeError;
  3570. }
  3571. }
  3572. return function() {
  3573. var args = arguments,
  3574. length = funcs.length;
  3575. while (length--) {
  3576. args = [funcs[length].apply(this, args)];
  3577. }
  3578. return args[0];
  3579. };
  3580. }
  3581. /**
  3582. * Creates a function that will delay the execution of `func` until after
  3583. * `wait` milliseconds have elapsed since the last time it was invoked.
  3584. * Provide an options object to indicate that `func` should be invoked on
  3585. * the leading and/or trailing edge of the `wait` timeout. Subsequent calls
  3586. * to the debounced function will return the result of the last `func` call.
  3587. *
  3588. * Note: If `leading` and `trailing` options are `true` `func` will be called
  3589. * on the trailing edge of the timeout only if the the debounced function is
  3590. * invoked more than once during the `wait` timeout.
  3591. *
  3592. * @static
  3593. * @memberOf _
  3594. * @category Functions
  3595. * @param {Function} func The function to debounce.
  3596. * @param {number} wait The number of milliseconds to delay.
  3597. * @param {Object} [options] The options object.
  3598. * @param {boolean} [options.leading=false] Specify execution on the leading edge of the timeout.
  3599. * @param {number} [options.maxWait] The maximum time `func` is allowed to be delayed before it's called.
  3600. * @param {boolean} [options.trailing=true] Specify execution on the trailing edge of the timeout.
  3601. * @returns {Function} Returns the new debounced function.
  3602. * @example
  3603. *
  3604. * // avoid costly calculations while the window size is in flux
  3605. * var lazyLayout = _.debounce(calculateLayout, 150);
  3606. * jQuery(window).on('resize', lazyLayout);
  3607. *
  3608. * // execute `sendMail` when the click event is fired, debouncing subsequent calls
  3609. * jQuery('#postbox').on('click', _.debounce(sendMail, 300, {
  3610. * 'leading': true,
  3611. * 'trailing': false
  3612. * });
  3613. *
  3614. * // ensure `batchLog` is executed once after 1 second of debounced calls
  3615. * var source = new EventSource('/stream');
  3616. * source.addEventListener('message', _.debounce(batchLog, 250, {
  3617. * 'maxWait': 1000
  3618. * }, false);
  3619. */
  3620. function debounce(func, wait, options) {
  3621. var args,
  3622. maxTimeoutId,
  3623. result,
  3624. stamp,
  3625. thisArg,
  3626. timeoutId,
  3627. trailingCall,
  3628. lastCalled = 0,
  3629. maxWait = false,
  3630. trailing = true;
  3631. if (!isFunction(func)) {
  3632. throw new TypeError;
  3633. }
  3634. wait = nativeMax(0, wait) || 0;
  3635. if (options === true) {
  3636. var leading = true;
  3637. trailing = false;
  3638. } else if (isObject(options)) {
  3639. leading = options.leading;
  3640. maxWait = 'maxWait' in options && (nativeMax(wait, options.maxWait) || 0);
  3641. trailing = 'trailing' in options ? options.trailing : trailing;
  3642. }
  3643. var delayed = function() {
  3644. var remaining = wait - (now() - stamp);
  3645. if (remaining <= 0) {
  3646. if (maxTimeoutId) {
  3647. clearTimeout(maxTimeoutId);
  3648. }
  3649. var isCalled = trailingCall;
  3650. maxTimeoutId = timeoutId = trailingCall = undefined;
  3651. if (isCalled) {
  3652. lastCalled = now();
  3653. result = func.apply(thisArg, args);
  3654. if (!timeoutId && !maxTimeoutId) {
  3655. args = thisArg = null;
  3656. }
  3657. }
  3658. } else {
  3659. timeoutId = setTimeout(delayed, remaining);
  3660. }
  3661. };
  3662. var maxDelayed = function() {
  3663. if (timeoutId) {
  3664. clearTimeout(timeoutId);
  3665. }
  3666. maxTimeoutId = timeoutId = trailingCall = undefined;
  3667. if (trailing || (maxWait !== wait)) {
  3668. lastCalled = now();
  3669. result = func.apply(thisArg, args);
  3670. if (!timeoutId && !maxTimeoutId) {
  3671. args = thisArg = null;
  3672. }
  3673. }
  3674. };
  3675. return function() {
  3676. args = arguments;
  3677. stamp = now();
  3678. thisArg = this;
  3679. trailingCall = trailing && (timeoutId || !leading);
  3680. if (maxWait === false) {
  3681. var leadingCall = leading && !timeoutId;
  3682. } else {
  3683. if (!maxTimeoutId && !leading) {
  3684. lastCalled = stamp;
  3685. }
  3686. var remaining = maxWait - (stamp - lastCalled),
  3687. isCalled = remaining <= 0;
  3688. if (isCalled) {
  3689. if (maxTimeoutId) {
  3690. maxTimeoutId = clearTimeout(maxTimeoutId);
  3691. }
  3692. lastCalled = stamp;
  3693. result = func.apply(thisArg, args);
  3694. }
  3695. else if (!maxTimeoutId) {
  3696. maxTimeoutId = setTimeout(maxDelayed, remaining);
  3697. }
  3698. }
  3699. if (isCalled && timeoutId) {
  3700. timeoutId = clearTimeout(timeoutId);
  3701. }
  3702. else if (!timeoutId && wait !== maxWait) {
  3703. timeoutId = setTimeout(delayed, wait);
  3704. }
  3705. if (leadingCall) {
  3706. isCalled = true;
  3707. result = func.apply(thisArg, args);
  3708. }
  3709. if (isCalled && !timeoutId && !maxTimeoutId) {
  3710. args = thisArg = null;
  3711. }
  3712. return result;
  3713. };
  3714. }
  3715. /**
  3716. * Defers executing the `func` function until the current call stack has cleared.
  3717. * Additional arguments will be provided to `func` when it is invoked.
  3718. *
  3719. * @static
  3720. * @memberOf _
  3721. * @category Functions
  3722. * @param {Function} func The function to defer.
  3723. * @param {...*} [arg] Arguments to invoke the function with.
  3724. * @returns {number} Returns the timer id.
  3725. * @example
  3726. *
  3727. * _.defer(function(text) { console.log(text); }, 'deferred');
  3728. * // logs 'deferred' after one or more milliseconds
  3729. */
  3730. function defer(func) {
  3731. if (!isFunction(func)) {
  3732. throw new TypeError;
  3733. }
  3734. var args = slice(arguments, 1);
  3735. return setTimeout(function() { func.apply(undefined, args); }, 1);
  3736. }
  3737. /**
  3738. * Executes the `func` function after `wait` milliseconds. Additional arguments
  3739. * will be provided to `func` when it is invoked.
  3740. *
  3741. * @static
  3742. * @memberOf _
  3743. * @category Functions
  3744. * @param {Function} func The function to delay.
  3745. * @param {number} wait The number of milliseconds to delay execution.
  3746. * @param {...*} [arg] Arguments to invoke the function with.
  3747. * @returns {number} Returns the timer id.
  3748. * @example
  3749. *
  3750. * _.delay(function(text) { console.log(text); }, 1000, 'later');
  3751. * // => logs 'later' after one second
  3752. */
  3753. function delay(func, wait) {
  3754. if (!isFunction(func)) {
  3755. throw new TypeError;
  3756. }
  3757. var args = slice(arguments, 2);
  3758. return setTimeout(function() { func.apply(undefined, args); }, wait);
  3759. }
  3760. /**
  3761. * Creates a function that memoizes the result of `func`. If `resolver` is
  3762. * provided it will be used to determine the cache key for storing the result
  3763. * based on the arguments provided to the memoized function. By default, the
  3764. * first argument provided to the memoized function is used as the cache key.
  3765. * The `func` is executed with the `this` binding of the memoized function.
  3766. * The result cache is exposed as the `cache` property on the memoized function.
  3767. *
  3768. * @static
  3769. * @memberOf _
  3770. * @category Functions
  3771. * @param {Function} func The function to have its output memoized.
  3772. * @param {Function} [resolver] A function used to resolve the cache key.
  3773. * @returns {Function} Returns the new memoizing function.
  3774. * @example
  3775. *
  3776. * var fibonacci = _.memoize(function(n) {
  3777. * return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
  3778. * });
  3779. *
  3780. * fibonacci(9)
  3781. * // => 34
  3782. *
  3783. * var data = {
  3784. * 'fred': { 'name': 'fred', 'age': 40 },
  3785. * 'pebbles': { 'name': 'pebbles', 'age': 1 }
  3786. * };
  3787. *
  3788. * // modifying the result cache
  3789. * var get = _.memoize(function(name) { return data[name]; }, _.identity);
  3790. * get('pebbles');
  3791. * // => { 'name': 'pebbles', 'age': 1 }
  3792. *
  3793. * get.cache.pebbles.name = 'penelope';
  3794. * get('pebbles');
  3795. * // => { 'name': 'penelope', 'age': 1 }
  3796. */
  3797. function memoize(func, resolver) {
  3798. var cache = {};
  3799. return function() {
  3800. var key = resolver ? resolver.apply(this, arguments) : keyPrefix + arguments[0];
  3801. return hasOwnProperty.call(cache, key)
  3802. ? cache[key]
  3803. : (cache[key] = func.apply(this, arguments));
  3804. };
  3805. }
  3806. /**
  3807. * Creates a function that is restricted to execute `func` once. Repeat calls to
  3808. * the function will return the value of the first call. The `func` is executed
  3809. * with the `this` binding of the created function.
  3810. *
  3811. * @static
  3812. * @memberOf _
  3813. * @category Functions
  3814. * @param {Function} func The function to restrict.
  3815. * @returns {Function} Returns the new restricted function.
  3816. * @example
  3817. *
  3818. * var initialize = _.once(createApplication);
  3819. * initialize();
  3820. * initialize();
  3821. * // `initialize` executes `createApplication` once
  3822. */
  3823. function once(func) {
  3824. var ran,
  3825. result;
  3826. if (!isFunction(func)) {
  3827. throw new TypeError;
  3828. }
  3829. return function() {
  3830. if (ran) {
  3831. return result;
  3832. }
  3833. ran = true;
  3834. result = func.apply(this, arguments);
  3835. // clear the `func` variable so the function may be garbage collected
  3836. func = null;
  3837. return result;
  3838. };
  3839. }
  3840. /**
  3841. * Creates a function that, when called, invokes `func` with any additional
  3842. * `partial` arguments prepended to those provided to the new function. This
  3843. * method is similar to `_.bind` except it does **not** alter the `this` binding.
  3844. *
  3845. * @static
  3846. * @memberOf _
  3847. * @category Functions
  3848. * @param {Function} func The function to partially apply arguments to.
  3849. * @param {...*} [arg] Arguments to be partially applied.
  3850. * @returns {Function} Returns the new partially applied function.
  3851. * @example
  3852. *
  3853. * var greet = function(greeting, name) { return greeting + ' ' + name; };
  3854. * var hi = _.partial(greet, 'hi');
  3855. * hi('fred');
  3856. * // => 'hi fred'
  3857. */
  3858. function partial(func) {
  3859. return createWrapper(func, 16, slice(arguments, 1));
  3860. }
  3861. /**
  3862. * Creates a function that, when executed, will only call the `func` function
  3863. * at most once per every `wait` milliseconds. Provide an options object to
  3864. * indicate that `func` should be invoked on the leading and/or trailing edge
  3865. * of the `wait` timeout. Subsequent calls to the throttled function will
  3866. * return the result of the last `func` call.
  3867. *
  3868. * Note: If `leading` and `trailing` options are `true` `func` will be called
  3869. * on the trailing edge of the timeout only if the the throttled function is
  3870. * invoked more than once during the `wait` timeout.
  3871. *
  3872. * @static
  3873. * @memberOf _
  3874. * @category Functions
  3875. * @param {Function} func The function to throttle.
  3876. * @param {number} wait The number of milliseconds to throttle executions to.
  3877. * @param {Object} [options] The options object.
  3878. * @param {boolean} [options.leading=true] Specify execution on the leading edge of the timeout.
  3879. * @param {boolean} [options.trailing=true] Specify execution on the trailing edge of the timeout.
  3880. * @returns {Function} Returns the new throttled function.
  3881. * @example
  3882. *
  3883. * // avoid excessively updating the position while scrolling
  3884. * var throttled = _.throttle(updatePosition, 100);
  3885. * jQuery(window).on('scroll', throttled);
  3886. *
  3887. * // execute `renewToken` when the click event is fired, but not more than once every 5 minutes
  3888. * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {
  3889. * 'trailing': false
  3890. * }));
  3891. */
  3892. function throttle(func, wait, options) {
  3893. var leading = true,
  3894. trailing = true;
  3895. if (!isFunction(func)) {
  3896. throw new TypeError;
  3897. }
  3898. if (options === false) {
  3899. leading = false;
  3900. } else if (isObject(options)) {
  3901. leading = 'leading' in options ? options.leading : leading;
  3902. trailing = 'trailing' in options ? options.trailing : trailing;
  3903. }
  3904. options = {};
  3905. options.leading = leading;
  3906. options.maxWait = wait;
  3907. options.trailing = trailing;
  3908. return debounce(func, wait, options);
  3909. }
  3910. /**
  3911. * Creates a function that provides `value` to the wrapper function as its
  3912. * first argument. Additional arguments provided to the function are appended
  3913. * to those provided to the wrapper function. The wrapper is executed with
  3914. * the `this` binding of the created function.
  3915. *
  3916. * @static
  3917. * @memberOf _
  3918. * @category Functions
  3919. * @param {*} value The value to wrap.
  3920. * @param {Function} wrapper The wrapper function.
  3921. * @returns {Function} Returns the new function.
  3922. * @example
  3923. *
  3924. * var p = _.wrap(_.escape, function(func, text) {
  3925. * return '<p>' + func(text) + '</p>';
  3926. * });
  3927. *
  3928. * p('Fred, Wilma, & Pebbles');
  3929. * // => '<p>Fred, Wilma, &amp; Pebbles</p>'
  3930. */
  3931. function wrap(value, wrapper) {
  3932. return createWrapper(wrapper, 16, [value]);
  3933. }
  3934. /*--------------------------------------------------------------------------*/
  3935. /**
  3936. * Produces a callback bound to an optional `thisArg`. If `func` is a property
  3937. * name the created callback will return the property value for a given element.
  3938. * If `func` is an object the created callback will return `true` for elements
  3939. * that contain the equivalent object properties, otherwise it will return `false`.
  3940. *
  3941. * @static
  3942. * @memberOf _
  3943. * @category Utilities
  3944. * @param {*} [func=identity] The value to convert to a callback.
  3945. * @param {*} [thisArg] The `this` binding of the created callback.
  3946. * @param {number} [argCount] The number of arguments the callback accepts.
  3947. * @returns {Function} Returns a callback function.
  3948. * @example
  3949. *
  3950. * var characters = [
  3951. * { 'name': 'barney', 'age': 36 },
  3952. * { 'name': 'fred', 'age': 40 }
  3953. * ];
  3954. *
  3955. * // wrap to create custom callback shorthands
  3956. * _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) {
  3957. * var match = /^(.+?)__([gl]t)(.+)$/.exec(callback);
  3958. * return !match ? func(callback, thisArg) : function(object) {
  3959. * return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3];
  3960. * };
  3961. * });
  3962. *
  3963. * _.filter(characters, 'age__gt38');
  3964. * // => [{ 'name': 'fred', 'age': 40 }]
  3965. */
  3966. function createCallback(func, thisArg, argCount) {
  3967. var type = typeof func;
  3968. if (func == null || type == 'function') {
  3969. return baseCreateCallback(func, thisArg, argCount);
  3970. }
  3971. // handle "_.pluck" style callback shorthands
  3972. if (type != 'object') {
  3973. return property(func);
  3974. }
  3975. var props = keys(func);
  3976. return function(object) {
  3977. var length = props.length,
  3978. result = false;
  3979. while (length--) {
  3980. if (!(result = object[props[length]] === func[props[length]])) {
  3981. break;
  3982. }
  3983. }
  3984. return result;
  3985. };
  3986. }
  3987. /**
  3988. * Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their
  3989. * corresponding HTML entities.
  3990. *
  3991. * @static
  3992. * @memberOf _
  3993. * @category Utilities
  3994. * @param {string} string The string to escape.
  3995. * @returns {string} Returns the escaped string.
  3996. * @example
  3997. *
  3998. * _.escape('Fred, Wilma, & Pebbles');
  3999. * // => 'Fred, Wilma, &amp; Pebbles'
  4000. */
  4001. function escape(string) {
  4002. return string == null ? '' : String(string).replace(reUnescapedHtml, escapeHtmlChar);
  4003. }
  4004. /**
  4005. * This method returns the first argument provided to it.
  4006. *
  4007. * @static
  4008. * @memberOf _
  4009. * @category Utilities
  4010. * @param {*} value Any value.
  4011. * @returns {*} Returns `value`.
  4012. * @example
  4013. *
  4014. * var object = { 'name': 'fred' };
  4015. * _.identity(object) === object;
  4016. * // => true
  4017. */
  4018. function identity(value) {
  4019. return value;
  4020. }
  4021. /**
  4022. * Adds function properties of a source object to the destination object.
  4023. * If `object` is a function methods will be added to its prototype as well.
  4024. *
  4025. * @static
  4026. * @memberOf _
  4027. * @category Utilities
  4028. * @param {Function|Object} [object=lodash] object The destination object.
  4029. * @param {Object} source The object of functions to add.
  4030. * @param {Object} [options] The options object.
  4031. * @param {boolean} [options.chain=true] Specify whether the functions added are chainable.
  4032. * @example
  4033. *
  4034. * function capitalize(string) {
  4035. * return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
  4036. * }
  4037. *
  4038. * _.mixin({ 'capitalize': capitalize });
  4039. * _.capitalize('fred');
  4040. * // => 'Fred'
  4041. *
  4042. * _('fred').capitalize().value();
  4043. * // => 'Fred'
  4044. *
  4045. * _.mixin({ 'capitalize': capitalize }, { 'chain': false });
  4046. * _('fred').capitalize();
  4047. * // => 'Fred'
  4048. */
  4049. function mixin(object) {
  4050. forEach(functions(object), function(methodName) {
  4051. var func = lodash[methodName] = object[methodName];
  4052. lodash.prototype[methodName] = function() {
  4053. var args = [this.__wrapped__];
  4054. push.apply(args, arguments);
  4055. var result = func.apply(lodash, args);
  4056. return this.__chain__
  4057. ? new lodashWrapper(result, true)
  4058. : result;
  4059. };
  4060. });
  4061. }
  4062. /**
  4063. * Reverts the '_' variable to its previous value and returns a reference to
  4064. * the `lodash` function.
  4065. *
  4066. * @static
  4067. * @memberOf _
  4068. * @category Utilities
  4069. * @returns {Function} Returns the `lodash` function.
  4070. * @example
  4071. *
  4072. * var lodash = _.noConflict();
  4073. */
  4074. function noConflict() {
  4075. root._ = oldDash;
  4076. return this;
  4077. }
  4078. /**
  4079. * A no-operation function.
  4080. *
  4081. * @static
  4082. * @memberOf _
  4083. * @category Utilities
  4084. * @example
  4085. *
  4086. * var object = { 'name': 'fred' };
  4087. * _.noop(object) === undefined;
  4088. * // => true
  4089. */
  4090. function noop() {
  4091. // no operation performed
  4092. }
  4093. /**
  4094. * Gets the number of milliseconds that have elapsed since the Unix epoch
  4095. * (1 January 1970 00:00:00 UTC).
  4096. *
  4097. * @static
  4098. * @memberOf _
  4099. * @category Utilities
  4100. * @example
  4101. *
  4102. * var stamp = _.now();
  4103. * _.defer(function() { console.log(_.now() - stamp); });
  4104. * // => logs the number of milliseconds it took for the deferred function to be called
  4105. */
  4106. var now = isNative(now = Date.now) && now || function() {
  4107. return new Date().getTime();
  4108. };
  4109. /**
  4110. * Creates a "_.pluck" style function, which returns the `key` value of a
  4111. * given object.
  4112. *
  4113. * @static
  4114. * @memberOf _
  4115. * @category Utilities
  4116. * @param {string} key The name of the property to retrieve.
  4117. * @returns {Function} Returns the new function.
  4118. * @example
  4119. *
  4120. * var characters = [
  4121. * { 'name': 'fred', 'age': 40 },
  4122. * { 'name': 'barney', 'age': 36 }
  4123. * ];
  4124. *
  4125. * var getName = _.property('name');
  4126. *
  4127. * _.map(characters, getName);
  4128. * // => ['barney', 'fred']
  4129. *
  4130. * _.sortBy(characters, getName);
  4131. * // => [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }]
  4132. */
  4133. function property(key) {
  4134. return function(object) {
  4135. return object[key];
  4136. };
  4137. }
  4138. /**
  4139. * Produces a random number between `min` and `max` (inclusive). If only one
  4140. * argument is provided a number between `0` and the given number will be
  4141. * returned. If `floating` is truey or either `min` or `max` are floats a
  4142. * floating-point number will be returned instead of an integer.
  4143. *
  4144. * @static
  4145. * @memberOf _
  4146. * @category Utilities
  4147. * @param {number} [min=0] The minimum possible value.
  4148. * @param {number} [max=1] The maximum possible value.
  4149. * @param {boolean} [floating=false] Specify returning a floating-point number.
  4150. * @returns {number} Returns a random number.
  4151. * @example
  4152. *
  4153. * _.random(0, 5);
  4154. * // => an integer between 0 and 5
  4155. *
  4156. * _.random(5);
  4157. * // => also an integer between 0 and 5
  4158. *
  4159. * _.random(5, true);
  4160. * // => a floating-point number between 0 and 5
  4161. *
  4162. * _.random(1.2, 5.2);
  4163. * // => a floating-point number between 1.2 and 5.2
  4164. */
  4165. function random(min, max) {
  4166. if (min == null && max == null) {
  4167. max = 1;
  4168. }
  4169. min = +min || 0;
  4170. if (max == null) {
  4171. max = min;
  4172. min = 0;
  4173. } else {
  4174. max = +max || 0;
  4175. }
  4176. return min + floor(nativeRandom() * (max - min + 1));
  4177. }
  4178. /**
  4179. * Resolves the value of property `key` on `object`. If `key` is a function
  4180. * it will be invoked with the `this` binding of `object` and its result returned,
  4181. * else the property value is returned. If `object` is falsey then `undefined`
  4182. * is returned.
  4183. *
  4184. * @static
  4185. * @memberOf _
  4186. * @category Utilities
  4187. * @param {Object} object The object to inspect.
  4188. * @param {string} key The name of the property to resolve.
  4189. * @returns {*} Returns the resolved value.
  4190. * @example
  4191. *
  4192. * var object = {
  4193. * 'cheese': 'crumpets',
  4194. * 'stuff': function() {
  4195. * return 'nonsense';
  4196. * }
  4197. * };
  4198. *
  4199. * _.result(object, 'cheese');
  4200. * // => 'crumpets'
  4201. *
  4202. * _.result(object, 'stuff');
  4203. * // => 'nonsense'
  4204. */
  4205. function result(object, key) {
  4206. if (object) {
  4207. var value = object[key];
  4208. return isFunction(value) ? object[key]() : value;
  4209. }
  4210. }
  4211. /**
  4212. * A micro-templating method that handles arbitrary delimiters, preserves
  4213. * whitespace, and correctly escapes quotes within interpolated code.
  4214. *
  4215. * Note: In the development build, `_.template` utilizes sourceURLs for easier
  4216. * debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
  4217. *
  4218. * For more information on precompiling templates see:
  4219. * https://lodash.com/custom-builds
  4220. *
  4221. * For more information on Chrome extension sandboxes see:
  4222. * http://developer.chrome.com/stable/extensions/sandboxingEval.html
  4223. *
  4224. * @static
  4225. * @memberOf _
  4226. * @category Utilities
  4227. * @param {string} text The template text.
  4228. * @param {Object} data The data object used to populate the text.
  4229. * @param {Object} [options] The options object.
  4230. * @param {RegExp} [options.escape] The "escape" delimiter.
  4231. * @param {RegExp} [options.evaluate] The "evaluate" delimiter.
  4232. * @param {Object} [options.imports] An object to import into the template as local variables.
  4233. * @param {RegExp} [options.interpolate] The "interpolate" delimiter.
  4234. * @param {string} [sourceURL] The sourceURL of the template's compiled source.
  4235. * @param {string} [variable] The data object variable name.
  4236. * @returns {Function|string} Returns a compiled function when no `data` object
  4237. * is given, else it returns the interpolated text.
  4238. * @example
  4239. *
  4240. * // using the "interpolate" delimiter to create a compiled template
  4241. * var compiled = _.template('hello <%= name %>');
  4242. * compiled({ 'name': 'fred' });
  4243. * // => 'hello fred'
  4244. *
  4245. * // using the "escape" delimiter to escape HTML in data property values
  4246. * _.template('<b><%- value %></b>', { 'value': '<script>' });
  4247. * // => '<b>&lt;script&gt;</b>'
  4248. *
  4249. * // using the "evaluate" delimiter to generate HTML
  4250. * var list = '<% _.forEach(people, function(name) { %><li><%- name %></li><% }); %>';
  4251. * _.template(list, { 'people': ['fred', 'barney'] });
  4252. * // => '<li>fred</li><li>barney</li>'
  4253. *
  4254. * // using the ES6 delimiter as an alternative to the default "interpolate" delimiter
  4255. * _.template('hello ${ name }', { 'name': 'pebbles' });
  4256. * // => 'hello pebbles'
  4257. *
  4258. * // using the internal `print` function in "evaluate" delimiters
  4259. * _.template('<% print("hello " + name); %>!', { 'name': 'barney' });
  4260. * // => 'hello barney!'
  4261. *
  4262. * // using a custom template delimiters
  4263. * _.templateSettings = {
  4264. * 'interpolate': /{{([\s\S]+?)}}/g
  4265. * };
  4266. *
  4267. * _.template('hello {{ name }}!', { 'name': 'mustache' });
  4268. * // => 'hello mustache!'
  4269. *
  4270. * // using the `imports` option to import jQuery
  4271. * var list = '<% jq.each(people, function(name) { %><li><%- name %></li><% }); %>';
  4272. * _.template(list, { 'people': ['fred', 'barney'] }, { 'imports': { 'jq': jQuery } });
  4273. * // => '<li>fred</li><li>barney</li>'
  4274. *
  4275. * // using the `sourceURL` option to specify a custom sourceURL for the template
  4276. * var compiled = _.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' });
  4277. * compiled(data);
  4278. * // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector
  4279. *
  4280. * // using the `variable` option to ensure a with-statement isn't used in the compiled template
  4281. * var compiled = _.template('hi <%= data.name %>!', null, { 'variable': 'data' });
  4282. * compiled.source;
  4283. * // => function(data) {
  4284. * var __t, __p = '', __e = _.escape;
  4285. * __p += 'hi ' + ((__t = ( data.name )) == null ? '' : __t) + '!';
  4286. * return __p;
  4287. * }
  4288. *
  4289. * // using the `source` property to inline compiled templates for meaningful
  4290. * // line numbers in error messages and a stack trace
  4291. * fs.writeFileSync(path.join(cwd, 'jst.js'), '\
  4292. * var JST = {\
  4293. * "main": ' + _.template(mainText).source + '\
  4294. * };\
  4295. * ');
  4296. */
  4297. function template(text, data, options) {
  4298. var _ = lodash,
  4299. settings = _.templateSettings;
  4300. text = String(text || '');
  4301. options = defaults({}, options, settings);
  4302. var index = 0,
  4303. source = "__p += '",
  4304. variable = options.variable;
  4305. var reDelimiters = RegExp(
  4306. (options.escape || reNoMatch).source + '|' +
  4307. (options.interpolate || reNoMatch).source + '|' +
  4308. (options.evaluate || reNoMatch).source + '|$'
  4309. , 'g');
  4310. text.replace(reDelimiters, function(match, escapeValue, interpolateValue, evaluateValue, offset) {
  4311. source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar);
  4312. if (escapeValue) {
  4313. source += "' +\n_.escape(" + escapeValue + ") +\n'";
  4314. }
  4315. if (evaluateValue) {
  4316. source += "';\n" + evaluateValue + ";\n__p += '";
  4317. }
  4318. if (interpolateValue) {
  4319. source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
  4320. }
  4321. index = offset + match.length;
  4322. return match;
  4323. });
  4324. source += "';\n";
  4325. if (!variable) {
  4326. variable = 'obj';
  4327. source = 'with (' + variable + ' || {}) {\n' + source + '\n}\n';
  4328. }
  4329. source = 'function(' + variable + ') {\n' +
  4330. "var __t, __p = '', __j = Array.prototype.join;\n" +
  4331. "function print() { __p += __j.call(arguments, '') }\n" +
  4332. source +
  4333. 'return __p\n}';
  4334. try {
  4335. var result = Function('_', 'return ' + source)(_);
  4336. } catch(e) {
  4337. e.source = source;
  4338. throw e;
  4339. }
  4340. if (data) {
  4341. return result(data);
  4342. }
  4343. result.source = source;
  4344. return result;
  4345. }
  4346. /**
  4347. * Executes the callback `n` times, returning an array of the results
  4348. * of each callback execution. The callback is bound to `thisArg` and invoked
  4349. * with one argument; (index).
  4350. *
  4351. * @static
  4352. * @memberOf _
  4353. * @category Utilities
  4354. * @param {number} n The number of times to execute the callback.
  4355. * @param {Function} callback The function called per iteration.
  4356. * @param {*} [thisArg] The `this` binding of `callback`.
  4357. * @returns {Array} Returns an array of the results of each `callback` execution.
  4358. * @example
  4359. *
  4360. * var diceRolls = _.times(3, _.partial(_.random, 1, 6));
  4361. * // => [3, 6, 4]
  4362. *
  4363. * _.times(3, function(n) { mage.castSpell(n); });
  4364. * // => calls `mage.castSpell(n)` three times, passing `n` of `0`, `1`, and `2` respectively
  4365. *
  4366. * _.times(3, function(n) { this.cast(n); }, mage);
  4367. * // => also calls `mage.castSpell(n)` three times
  4368. */
  4369. function times(n, callback, thisArg) {
  4370. n = (n = +n) > -1 ? n : 0;
  4371. var index = -1,
  4372. result = Array(n);
  4373. callback = baseCreateCallback(callback, thisArg, 1);
  4374. while (++index < n) {
  4375. result[index] = callback(index);
  4376. }
  4377. return result;
  4378. }
  4379. /**
  4380. * The inverse of `_.escape` this method converts the HTML entities
  4381. * `&amp;`, `&lt;`, `&gt;`, `&quot;`, and `&#39;` in `string` to their
  4382. * corresponding characters.
  4383. *
  4384. * @static
  4385. * @memberOf _
  4386. * @category Utilities
  4387. * @param {string} string The string to unescape.
  4388. * @returns {string} Returns the unescaped string.
  4389. * @example
  4390. *
  4391. * _.unescape('Fred, Barney &amp; Pebbles');
  4392. * // => 'Fred, Barney & Pebbles'
  4393. */
  4394. function unescape(string) {
  4395. return string == null ? '' : String(string).replace(reEscapedHtml, unescapeHtmlChar);
  4396. }
  4397. /**
  4398. * Generates a unique ID. If `prefix` is provided the ID will be appended to it.
  4399. *
  4400. * @static
  4401. * @memberOf _
  4402. * @category Utilities
  4403. * @param {string} [prefix] The value to prefix the ID with.
  4404. * @returns {string} Returns the unique ID.
  4405. * @example
  4406. *
  4407. * _.uniqueId('contact_');
  4408. * // => 'contact_104'
  4409. *
  4410. * _.uniqueId();
  4411. * // => '105'
  4412. */
  4413. function uniqueId(prefix) {
  4414. var id = ++idCounter + '';
  4415. return prefix ? prefix + id : id;
  4416. }
  4417. /*--------------------------------------------------------------------------*/
  4418. /**
  4419. * Creates a `lodash` object that wraps the given value with explicit
  4420. * method chaining enabled.
  4421. *
  4422. * @static
  4423. * @memberOf _
  4424. * @category Chaining
  4425. * @param {*} value The value to wrap.
  4426. * @returns {Object} Returns the wrapper object.
  4427. * @example
  4428. *
  4429. * var characters = [
  4430. * { 'name': 'barney', 'age': 36 },
  4431. * { 'name': 'fred', 'age': 40 },
  4432. * { 'name': 'pebbles', 'age': 1 }
  4433. * ];
  4434. *
  4435. * var youngest = _.chain(characters)
  4436. * .sortBy('age')
  4437. * .map(function(chr) { return chr.name + ' is ' + chr.age; })
  4438. * .first()
  4439. * .value();
  4440. * // => 'pebbles is 1'
  4441. */
  4442. function chain(value) {
  4443. value = new lodashWrapper(value);
  4444. value.__chain__ = true;
  4445. return value;
  4446. }
  4447. /**
  4448. * Invokes `interceptor` with the `value` as the first argument and then
  4449. * returns `value`. The purpose of this method is to "tap into" a method
  4450. * chain in order to perform operations on intermediate results within
  4451. * the chain.
  4452. *
  4453. * @static
  4454. * @memberOf _
  4455. * @category Chaining
  4456. * @param {*} value The value to provide to `interceptor`.
  4457. * @param {Function} interceptor The function to invoke.
  4458. * @returns {*} Returns `value`.
  4459. * @example
  4460. *
  4461. * _([1, 2, 3, 4])
  4462. * .tap(function(array) { array.pop(); })
  4463. * .reverse()
  4464. * .value();
  4465. * // => [3, 2, 1]
  4466. */
  4467. function tap(value, interceptor) {
  4468. interceptor(value);
  4469. return value;
  4470. }
  4471. /**
  4472. * Enables explicit method chaining on the wrapper object.
  4473. *
  4474. * @name chain
  4475. * @memberOf _
  4476. * @category Chaining
  4477. * @returns {*} Returns the wrapper object.
  4478. * @example
  4479. *
  4480. * var characters = [
  4481. * { 'name': 'barney', 'age': 36 },
  4482. * { 'name': 'fred', 'age': 40 }
  4483. * ];
  4484. *
  4485. * // without explicit chaining
  4486. * _(characters).first();
  4487. * // => { 'name': 'barney', 'age': 36 }
  4488. *
  4489. * // with explicit chaining
  4490. * _(characters).chain()
  4491. * .first()
  4492. * .pick('age')
  4493. * .value();
  4494. * // => { 'age': 36 }
  4495. */
  4496. function wrapperChain() {
  4497. this.__chain__ = true;
  4498. return this;
  4499. }
  4500. /**
  4501. * Extracts the wrapped value.
  4502. *
  4503. * @name valueOf
  4504. * @memberOf _
  4505. * @alias value
  4506. * @category Chaining
  4507. * @returns {*} Returns the wrapped value.
  4508. * @example
  4509. *
  4510. * _([1, 2, 3]).valueOf();
  4511. * // => [1, 2, 3]
  4512. */
  4513. function wrapperValueOf() {
  4514. return this.__wrapped__;
  4515. }
  4516. /*--------------------------------------------------------------------------*/
  4517. // add functions that return wrapped values when chaining
  4518. lodash.after = after;
  4519. lodash.bind = bind;
  4520. lodash.bindAll = bindAll;
  4521. lodash.chain = chain;
  4522. lodash.compact = compact;
  4523. lodash.compose = compose;
  4524. lodash.countBy = countBy;
  4525. lodash.debounce = debounce;
  4526. lodash.defaults = defaults;
  4527. lodash.defer = defer;
  4528. lodash.delay = delay;
  4529. lodash.difference = difference;
  4530. lodash.filter = filter;
  4531. lodash.flatten = flatten;
  4532. lodash.forEach = forEach;
  4533. lodash.functions = functions;
  4534. lodash.groupBy = groupBy;
  4535. lodash.indexBy = indexBy;
  4536. lodash.initial = initial;
  4537. lodash.intersection = intersection;
  4538. lodash.invert = invert;
  4539. lodash.invoke = invoke;
  4540. lodash.keys = keys;
  4541. lodash.map = map;
  4542. lodash.max = max;
  4543. lodash.memoize = memoize;
  4544. lodash.min = min;
  4545. lodash.omit = omit;
  4546. lodash.once = once;
  4547. lodash.pairs = pairs;
  4548. lodash.partial = partial;
  4549. lodash.pick = pick;
  4550. lodash.pluck = pluck;
  4551. lodash.range = range;
  4552. lodash.reject = reject;
  4553. lodash.rest = rest;
  4554. lodash.shuffle = shuffle;
  4555. lodash.sortBy = sortBy;
  4556. lodash.tap = tap;
  4557. lodash.throttle = throttle;
  4558. lodash.times = times;
  4559. lodash.toArray = toArray;
  4560. lodash.union = union;
  4561. lodash.uniq = uniq;
  4562. lodash.values = values;
  4563. lodash.where = where;
  4564. lodash.without = without;
  4565. lodash.wrap = wrap;
  4566. lodash.zip = zip;
  4567. // add aliases
  4568. lodash.collect = map;
  4569. lodash.drop = rest;
  4570. lodash.each = forEach;
  4571. lodash.extend = assign;
  4572. lodash.methods = functions;
  4573. lodash.object = zipObject;
  4574. lodash.select = filter;
  4575. lodash.tail = rest;
  4576. lodash.unique = uniq;
  4577. /*--------------------------------------------------------------------------*/
  4578. // add functions that return unwrapped values when chaining
  4579. lodash.clone = clone;
  4580. lodash.contains = contains;
  4581. lodash.escape = escape;
  4582. lodash.every = every;
  4583. lodash.find = find;
  4584. lodash.has = has;
  4585. lodash.identity = identity;
  4586. lodash.indexOf = indexOf;
  4587. lodash.isArguments = isArguments;
  4588. lodash.isArray = isArray;
  4589. lodash.isBoolean = isBoolean;
  4590. lodash.isDate = isDate;
  4591. lodash.isElement = isElement;
  4592. lodash.isEmpty = isEmpty;
  4593. lodash.isEqual = isEqual;
  4594. lodash.isFinite = isFinite;
  4595. lodash.isFunction = isFunction;
  4596. lodash.isNaN = isNaN;
  4597. lodash.isNull = isNull;
  4598. lodash.isNumber = isNumber;
  4599. lodash.isObject = isObject;
  4600. lodash.isRegExp = isRegExp;
  4601. lodash.isString = isString;
  4602. lodash.isUndefined = isUndefined;
  4603. lodash.lastIndexOf = lastIndexOf;
  4604. lodash.mixin = mixin;
  4605. lodash.noConflict = noConflict;
  4606. lodash.random = random;
  4607. lodash.reduce = reduce;
  4608. lodash.reduceRight = reduceRight;
  4609. lodash.result = result;
  4610. lodash.size = size;
  4611. lodash.some = some;
  4612. lodash.sortedIndex = sortedIndex;
  4613. lodash.template = template;
  4614. lodash.unescape = unescape;
  4615. lodash.uniqueId = uniqueId;
  4616. // add aliases
  4617. lodash.all = every;
  4618. lodash.any = some;
  4619. lodash.detect = find;
  4620. lodash.findWhere = findWhere;
  4621. lodash.foldl = reduce;
  4622. lodash.foldr = reduceRight;
  4623. lodash.include = contains;
  4624. lodash.inject = reduce;
  4625. /*--------------------------------------------------------------------------*/
  4626. // add functions capable of returning wrapped and unwrapped values when chaining
  4627. lodash.first = first;
  4628. lodash.last = last;
  4629. lodash.sample = sample;
  4630. // add aliases
  4631. lodash.take = first;
  4632. lodash.head = first;
  4633. /*--------------------------------------------------------------------------*/
  4634. // add functions to `lodash.prototype`
  4635. mixin(lodash);
  4636. /**
  4637. * The semantic version number.
  4638. *
  4639. * @static
  4640. * @memberOf _
  4641. * @type string
  4642. */
  4643. lodash.VERSION = '2.4.2';
  4644. // add "Chaining" functions to the wrapper
  4645. lodash.prototype.chain = wrapperChain;
  4646. lodash.prototype.value = wrapperValueOf;
  4647. // add `Array` mutator functions to the wrapper
  4648. forEach(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
  4649. var func = arrayRef[methodName];
  4650. lodash.prototype[methodName] = function() {
  4651. var value = this.__wrapped__;
  4652. func.apply(value, arguments);
  4653. // avoid array-like object bugs with `Array#shift` and `Array#splice`
  4654. // in Firefox < 10 and IE < 9
  4655. if (!support.spliceObjects && value.length === 0) {
  4656. delete value[0];
  4657. }
  4658. return this;
  4659. };
  4660. });
  4661. // add `Array` accessor functions to the wrapper
  4662. forEach(['concat', 'join', 'slice'], function(methodName) {
  4663. var func = arrayRef[methodName];
  4664. lodash.prototype[methodName] = function() {
  4665. var value = this.__wrapped__,
  4666. result = func.apply(value, arguments);
  4667. if (this.__chain__) {
  4668. result = new lodashWrapper(result);
  4669. result.__chain__ = true;
  4670. }
  4671. return result;
  4672. };
  4673. });
  4674. /*--------------------------------------------------------------------------*/
  4675. // some AMD build optimizers like r.js check for condition patterns like the following:
  4676. if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  4677. // Expose Lo-Dash to the global object even when an AMD loader is present in
  4678. // case Lo-Dash is loaded with a RequireJS shim config.
  4679. // See http://requirejs.org/docs/api.html#config-shim
  4680. root._ = lodash;
  4681. // define as an anonymous module so, through path mapping, it can be
  4682. // referenced as the "underscore" module
  4683. define(function() {
  4684. return lodash;
  4685. });
  4686. }
  4687. // check for `exports` after `define` in case a build optimizer adds an `exports` object
  4688. else if (freeExports && freeModule) {
  4689. // in Node.js or RingoJS
  4690. if (moduleExports) {
  4691. (freeModule.exports = lodash)._ = lodash;
  4692. }
  4693. // in Narwhal or Rhino -require
  4694. else {
  4695. freeExports._ = lodash;
  4696. }
  4697. }
  4698. else {
  4699. // in a browser or Rhino
  4700. root._ = lodash;
  4701. }
  4702. }.call(this));