lodash.js 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461
  1. /*!
  2. * Lo-Dash v0.7.0 <http://lodash.com>
  3. * Copyright 2012 John-David Dalton <http://allyoucanleet.com/>
  4. * Based on Underscore.js 1.3.3, copyright 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
  5. * <http://documentcloud.github.com/underscore>
  6. * Available under MIT license <http://lodash.com/license>
  7. */
  8. ;(function(window, undefined) {
  9. 'use strict';
  10. /**
  11. * Used to cache the last `_.templateSettings.evaluate` delimiter to avoid
  12. * unnecessarily assigning `reEvaluateDelimiter` a new generated regexp.
  13. * Assigned in `_.template`.
  14. */
  15. var lastEvaluateDelimiter;
  16. /**
  17. * Used to cache the last template `options.variable` to avoid unnecessarily
  18. * assigning `reDoubleVariable` a new generated regexp. Assigned in `_.template`.
  19. */
  20. var lastVariable;
  21. /**
  22. * Used to match potentially incorrect data object references, like `obj.obj`,
  23. * in compiled templates. Assigned in `_.template`.
  24. */
  25. var reDoubleVariable;
  26. /**
  27. * Used to match "evaluate" delimiters, including internal delimiters,
  28. * in template text. Assigned in `_.template`.
  29. */
  30. var reEvaluateDelimiter;
  31. /** Detect free variable `exports` */
  32. var freeExports = typeof exports == 'object' && exports &&
  33. (typeof global == 'object' && global && global == global.global && (window = global), exports);
  34. /** Native prototype shortcuts */
  35. var ArrayProto = Array.prototype,
  36. BoolProto = Boolean.prototype,
  37. ObjectProto = Object.prototype,
  38. NumberProto = Number.prototype,
  39. StringProto = String.prototype;
  40. /** Used to generate unique IDs */
  41. var idCounter = 0;
  42. /** Used by `cachedContains` as the default size when optimizations are enabled for large arrays */
  43. var largeArraySize = 30;
  44. /** Used to restore the original `_` reference in `noConflict` */
  45. var oldDash = window._;
  46. /** Used to detect delimiter values that should be processed by `tokenizeEvaluate` */
  47. var reComplexDelimiter = /[-?+=!~*%&^<>|{(\/]|\[\D|\b(?:delete|in|instanceof|new|typeof|void)\b/;
  48. /** Used to match HTML entities */
  49. var reEscapedHtml = /&(?:amp|lt|gt|quot|#x27);/g;
  50. /** Used to match empty string literals in compiled template source */
  51. var reEmptyStringLeading = /\b__p \+= '';/g,
  52. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  53. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  54. /** Used to match regexp flags from their coerced string values */
  55. var reFlags = /\w*$/;
  56. /** Used to insert the data object variable into compiled template source */
  57. var reInsertVariable = /(?:__e|__t = )\(\s*(?![\d\s"']|this\.)/g;
  58. /** Used to detect if a method is native */
  59. var reNative = RegExp('^' +
  60. (ObjectProto.valueOf + '')
  61. .replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&')
  62. .replace(/valueOf|for [^\]]+/g, '.+?') + '$'
  63. );
  64. /** Used to match internally used tokens in template text */
  65. var reToken = /__token(\d+)__/g;
  66. /** Used to match HTML characters */
  67. var reUnescapedHtml = /[&<>"']/g;
  68. /** Used to match unescaped characters in compiled string literals */
  69. var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
  70. /** Used to fix the JScript [[DontEnum]] bug */
  71. var shadowed = [
  72. 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable',
  73. 'toLocaleString', 'toString', 'valueOf'
  74. ];
  75. /** Used to make template sourceURLs easier to identify */
  76. var templateCounter = 0;
  77. /** Used to replace template delimiters */
  78. var tokenHead = '__token',
  79. tokenFoot = '__';
  80. /** Used to store tokenized template text snippets */
  81. var tokenized = [];
  82. /** Native method shortcuts */
  83. var concat = ArrayProto.concat,
  84. hasOwnProperty = ObjectProto.hasOwnProperty,
  85. push = ArrayProto.push,
  86. propertyIsEnumerable = ObjectProto.propertyIsEnumerable,
  87. slice = ArrayProto.slice,
  88. toString = ObjectProto.toString;
  89. /* Native method shortcuts for methods with the same name as other `lodash` methods */
  90. var nativeBind = reNative.test(nativeBind = slice.bind) && nativeBind,
  91. nativeFloor = Math.floor,
  92. nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray,
  93. nativeIsFinite = window.isFinite,
  94. nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys,
  95. nativeMax = Math.max,
  96. nativeMin = Math.min,
  97. nativeRandom = Math.random;
  98. /** `Object#toString` result shortcuts */
  99. var argsClass = '[object Arguments]',
  100. arrayClass = '[object Array]',
  101. boolClass = '[object Boolean]',
  102. dateClass = '[object Date]',
  103. funcClass = '[object Function]',
  104. numberClass = '[object Number]',
  105. objectClass = '[object Object]',
  106. regexpClass = '[object RegExp]',
  107. stringClass = '[object String]';
  108. /** Timer shortcuts */
  109. var clearTimeout = window.clearTimeout,
  110. setTimeout = window.setTimeout;
  111. /**
  112. * Detect the JScript [[DontEnum]] bug:
  113. *
  114. * In IE < 9 an objects own properties, shadowing non-enumerable ones, are
  115. * made non-enumerable as well.
  116. */
  117. var hasDontEnumBug;
  118. /**
  119. * Detect if `Array#shift` and `Array#splice` augment array-like objects
  120. * incorrectly:
  121. *
  122. * Firefox < 10, IE compatibility mode, and IE < 9 have buggy Array `shift()`
  123. * and `splice()` functions that fail to remove the last element, `value[0]`,
  124. * of array-like objects even though the `length` property is set to `0`.
  125. * The `shift()` method is buggy in IE 8 compatibility mode, while `splice()`
  126. * is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
  127. */
  128. var hasObjectSpliceBug;
  129. /** Detect if own properties are iterated after inherited properties (IE < 9) */
  130. var iteratesOwnLast;
  131. /** Detect if an `arguments` object's indexes are non-enumerable (IE < 9) */
  132. var noArgsEnum = true;
  133. (function() {
  134. var object = { '0': 1, 'length': 1 },
  135. props = [];
  136. function ctor() { this.x = 1; }
  137. ctor.prototype = { 'valueOf': 1, 'y': 1 };
  138. for (var prop in new ctor) { props.push(prop); }
  139. for (prop in arguments) { noArgsEnum = !prop; }
  140. hasDontEnumBug = (props + '').length < 4;
  141. iteratesOwnLast = props[0] != 'x';
  142. hasObjectSpliceBug = (props.splice.call(object, 0, 1), object[0]);
  143. }(1));
  144. /** Detect if an `arguments` object's [[Class]] is unresolvable (Firefox < 4, IE < 9) */
  145. var noArgsClass = !isArguments(arguments);
  146. /** Detect if `Array#slice` cannot be used to convert strings to arrays (Opera < 10.52) */
  147. var noArraySliceOnStrings = slice.call('x')[0] != 'x';
  148. /**
  149. * Detect lack of support for accessing string characters by index:
  150. *
  151. * IE < 8 can't access characters by index and IE 8 can only access
  152. * characters by index on string literals.
  153. */
  154. var noCharByIndex = ('x'[0] + Object('x')[0]) != 'xx';
  155. /**
  156. * Detect if a node's [[Class]] is unresolvable (IE < 9)
  157. * and that the JS engine won't error when attempting to coerce an object to
  158. * a string without a `toString` property value of `typeof` "function".
  159. */
  160. try {
  161. var noNodeClass = ({ 'toString': 0 } + '', toString.call(window.document || 0) == objectClass);
  162. } catch(e) { }
  163. /* Detect if `Function#bind` exists and is inferred to be fast (all but V8) */
  164. var isBindFast = nativeBind && /\n|Opera/.test(nativeBind + toString.call(window.opera));
  165. /* Detect if `Object.keys` exists and is inferred to be fast (IE, Opera, V8) */
  166. var isKeysFast = nativeKeys && /^.+$|true/.test(nativeKeys + !!window.attachEvent);
  167. /* Detect if strict mode, "use strict", is inferred to be fast (V8) */
  168. var isStrictFast = !isBindFast;
  169. /**
  170. * Detect if sourceURL syntax is usable without erroring:
  171. *
  172. * The JS engine in Adobe products, like InDesign, will throw a syntax error
  173. * when it encounters a single line comment beginning with the `@` symbol.
  174. *
  175. * The JS engine in Narwhal will generate the function `function anonymous(){//}`
  176. * and throw a syntax error.
  177. *
  178. * Avoid comments beginning `@` symbols in IE because they are part of its
  179. * non-standard conditional compilation support.
  180. * http://msdn.microsoft.com/en-us/library/121hztk3(v=vs.94).aspx
  181. */
  182. try {
  183. var useSourceURL = (Function('//@')(), !window.attachEvent);
  184. } catch(e) { }
  185. /** Used to identify object classifications that are array-like */
  186. var arrayLikeClasses = {};
  187. arrayLikeClasses[boolClass] = arrayLikeClasses[dateClass] = arrayLikeClasses[funcClass] =
  188. arrayLikeClasses[numberClass] = arrayLikeClasses[objectClass] = arrayLikeClasses[regexpClass] = false;
  189. arrayLikeClasses[argsClass] = arrayLikeClasses[arrayClass] = arrayLikeClasses[stringClass] = true;
  190. /** Used to identify object classifications that `_.clone` supports */
  191. var cloneableClasses = {};
  192. cloneableClasses[argsClass] = cloneableClasses[funcClass] = false;
  193. cloneableClasses[arrayClass] = cloneableClasses[boolClass] = cloneableClasses[dateClass] =
  194. cloneableClasses[numberClass] = cloneableClasses[objectClass] = cloneableClasses[regexpClass] =
  195. cloneableClasses[stringClass] = true;
  196. /**
  197. * Used to convert characters to HTML entities:
  198. *
  199. * Though the `>` character is escaped for symmetry, characters like `>` and `/`
  200. * don't require escaping in HTML and have no special meaning unless they're part
  201. * of a tag or an unquoted attribute value.
  202. * http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
  203. */
  204. var htmlEscapes = {
  205. '&': '&amp;',
  206. '<': '&lt;',
  207. '>': '&gt;',
  208. '"': '&quot;',
  209. "'": '&#x27;'
  210. };
  211. /** Used to convert HTML entities to characters */
  212. var htmlUnescapes = {
  213. '&amp;': '&',
  214. '&lt;': '<',
  215. '&gt;': '>',
  216. '&quot;': '"',
  217. '&#x27;': "'"
  218. };
  219. /** Used to determine if values are of the language type Object */
  220. var objectTypes = {
  221. 'boolean': false,
  222. 'function': true,
  223. 'object': true,
  224. 'number': false,
  225. 'string': false,
  226. 'undefined': false,
  227. 'unknown': true
  228. };
  229. /** Used to escape characters for inclusion in compiled string literals */
  230. var stringEscapes = {
  231. '\\': '\\',
  232. "'": "'",
  233. '\n': 'n',
  234. '\r': 'r',
  235. '\t': 't',
  236. '\u2028': 'u2028',
  237. '\u2029': 'u2029'
  238. };
  239. /*--------------------------------------------------------------------------*/
  240. /**
  241. * The `lodash` function.
  242. *
  243. * @name _
  244. * @constructor
  245. * @param {Mixed} value The value to wrap in a `LoDash` instance.
  246. * @returns {Object} Returns a `LoDash` instance.
  247. */
  248. function lodash(value) {
  249. // allow invoking `lodash` without the `new` operator
  250. return new LoDash(value);
  251. }
  252. /**
  253. * Creates a `LoDash` instance that wraps a value to allow chaining.
  254. *
  255. * @private
  256. * @constructor
  257. * @param {Mixed} value The value to wrap.
  258. */
  259. function LoDash(value) {
  260. // exit early if already wrapped
  261. if (value && value.__wrapped__) {
  262. return value;
  263. }
  264. this.__wrapped__ = value;
  265. }
  266. /**
  267. * By default, the template delimiters used by Lo-Dash are similar to those in
  268. * embedded Ruby (ERB). Change the following template settings to use alternative
  269. * delimiters.
  270. *
  271. * @static
  272. * @memberOf _
  273. * @type Object
  274. */
  275. lodash.templateSettings = {
  276. /**
  277. * Used to detect `data` property values to be HTML-escaped.
  278. *
  279. * @static
  280. * @memberOf _.templateSettings
  281. * @type RegExp
  282. */
  283. 'escape': /<%-([\s\S]+?)%>/g,
  284. /**
  285. * Used to detect code to be evaluated.
  286. *
  287. * @static
  288. * @memberOf _.templateSettings
  289. * @type RegExp
  290. */
  291. 'evaluate': /<%([\s\S]+?)%>/g,
  292. /**
  293. * Used to detect `data` property values to inject.
  294. *
  295. * @static
  296. * @memberOf _.templateSettings
  297. * @type RegExp
  298. */
  299. 'interpolate': /<%=([\s\S]+?)%>/g,
  300. /**
  301. * Used to reference the data object in the template text.
  302. *
  303. * @static
  304. * @memberOf _.templateSettings
  305. * @type String
  306. */
  307. 'variable': ''
  308. };
  309. /*--------------------------------------------------------------------------*/
  310. /**
  311. * The template used to create iterator functions.
  312. *
  313. * @private
  314. * @param {Obect} data The data object used to populate the text.
  315. * @returns {String} Returns the interpolated text.
  316. */
  317. var iteratorTemplate = template(
  318. // conditional strict mode
  319. '<% if (useStrict) { %>\'use strict\';\n<% } %>' +
  320. // the `iteratee` may be reassigned by the `top` snippet
  321. 'var index, value, iteratee = <%= firstArg %>, ' +
  322. // assign the `result` variable an initial value
  323. 'result<% if (init) { %> = <%= init %><% } %>;\n' +
  324. // add code to exit early or do so if the first argument is falsey
  325. '<%= exit %>;\n' +
  326. // add code after the exit snippet but before the iteration branches
  327. '<%= top %>;\n' +
  328. // the following branch is for iterating arrays and array-like objects
  329. '<% if (arrayBranch) { %>' +
  330. 'var length = iteratee.length; index = -1;' +
  331. ' <% if (objectBranch) { %>\nif (length > -1 && length === length >>> 0) {<% } %>' +
  332. // add support for accessing string characters by index if needed
  333. ' <% if (noCharByIndex) { %>\n' +
  334. ' if (toString.call(iteratee) == stringClass) {\n' +
  335. ' iteratee = iteratee.split(\'\')\n' +
  336. ' }' +
  337. ' <% } %>\n' +
  338. ' <%= arrayBranch.beforeLoop %>;\n' +
  339. ' while (++index < length) {\n' +
  340. ' value = iteratee[index];\n' +
  341. ' <%= arrayBranch.inLoop %>\n' +
  342. ' }' +
  343. ' <% if (objectBranch) { %>\n}<% } %>' +
  344. '<% } %>' +
  345. // the following branch is for iterating an object's own/inherited properties
  346. '<% if (objectBranch) { %>' +
  347. ' <% if (arrayBranch) { %>\nelse {' +
  348. // add support for iterating over `arguments` objects if needed
  349. ' <% } else if (noArgsEnum) { %>\n' +
  350. ' var length = iteratee.length; index = -1;\n' +
  351. ' if (length && isArguments(iteratee)) {\n' +
  352. ' while (++index < length) {\n' +
  353. ' value = iteratee[index += \'\'];\n' +
  354. ' <%= objectBranch.inLoop %>\n' +
  355. ' }\n' +
  356. ' } else {' +
  357. ' <% } %>' +
  358. // Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1
  359. // (if the prototype or a property on the prototype has been set)
  360. // incorrectly sets a function's `prototype` property [[Enumerable]]
  361. // value to `true`. Because of this Lo-Dash standardizes on skipping
  362. // the the `prototype` property of functions regardless of its
  363. // [[Enumerable]] value.
  364. ' <% if (!hasDontEnumBug) { %>\n' +
  365. ' var skipProto = typeof iteratee == \'function\' && \n' +
  366. ' propertyIsEnumerable.call(iteratee, \'prototype\');\n' +
  367. ' <% } %>' +
  368. // iterate own properties using `Object.keys` if it's fast
  369. ' <% if (isKeysFast && useHas) { %>\n' +
  370. ' var ownIndex = -1,\n' +
  371. ' ownProps = objectTypes[typeof iteratee] ? nativeKeys(iteratee) : [],\n' +
  372. ' length = ownProps.length;\n\n' +
  373. ' <%= objectBranch.beforeLoop %>;\n' +
  374. ' while (++ownIndex < length) {\n' +
  375. ' index = ownProps[ownIndex];\n' +
  376. ' <% if (!hasDontEnumBug) { %>if (!(skipProto && index == \'prototype\')) {\n <% } %>' +
  377. ' value = iteratee[index];\n' +
  378. ' <%= objectBranch.inLoop %>\n' +
  379. ' <% if (!hasDontEnumBug) { %>}\n<% } %>' +
  380. ' }' +
  381. // else using a for-in loop
  382. ' <% } else { %>\n' +
  383. ' <%= objectBranch.beforeLoop %>;\n' +
  384. ' for (index in iteratee) {<%' +
  385. ' if (!hasDontEnumBug || useHas) { %>\n if (<%' +
  386. ' if (!hasDontEnumBug) { %>!(skipProto && index == \'prototype\')<% }' +
  387. ' if (!hasDontEnumBug && useHas) { %> && <% }' +
  388. ' if (useHas) { %>hasOwnProperty.call(iteratee, index)<% }' +
  389. ' %>) {' +
  390. ' <% } %>\n' +
  391. ' value = iteratee[index];\n' +
  392. ' <%= objectBranch.inLoop %>;\n' +
  393. ' <% if (!hasDontEnumBug || useHas) { %>}\n<% } %>' +
  394. ' }' +
  395. ' <% } %>' +
  396. // Because IE < 9 can't set the `[[Enumerable]]` attribute of an
  397. // existing property and the `constructor` property of a prototype
  398. // defaults to non-enumerable, Lo-Dash skips the `constructor`
  399. // property when it infers it's iterating over a `prototype` object.
  400. ' <% if (hasDontEnumBug) { %>\n\n' +
  401. ' var ctor = iteratee.constructor;\n' +
  402. ' <% for (var k = 0; k < 7; k++) { %>\n' +
  403. ' index = \'<%= shadowed[k] %>\';\n' +
  404. ' if (<%' +
  405. ' if (shadowed[k] == \'constructor\') {' +
  406. ' %>!(ctor && ctor.prototype === iteratee) && <%' +
  407. ' } %>hasOwnProperty.call(iteratee, index)) {\n' +
  408. ' value = iteratee[index];\n' +
  409. ' <%= objectBranch.inLoop %>\n' +
  410. ' }' +
  411. ' <% } %>' +
  412. ' <% } %>' +
  413. ' <% if (arrayBranch || noArgsEnum) { %>\n}<% } %>' +
  414. '<% } %>\n' +
  415. // add code to the bottom of the iteration function
  416. '<%= bottom %>;\n' +
  417. // finally, return the `result`
  418. 'return result'
  419. );
  420. /**
  421. * Reusable iterator options shared by
  422. * `every`, `filter`, `find`, `forEach`, `forIn`, `forOwn`, `groupBy`, `map`,
  423. * `reject`, `some`, and `sortBy`.
  424. */
  425. var baseIteratorOptions = {
  426. 'args': 'collection, callback, thisArg',
  427. 'init': 'collection',
  428. 'top':
  429. 'if (!callback) {\n' +
  430. ' callback = identity\n' +
  431. '}\n' +
  432. 'else if (thisArg) {\n' +
  433. ' callback = iteratorBind(callback, thisArg)\n' +
  434. '}',
  435. 'inLoop': 'if (callback(value, index, collection) === false) return result'
  436. };
  437. /** Reusable iterator options for `countBy`, `groupBy`, and `sortBy` */
  438. var countByIteratorOptions = {
  439. 'init': '{}',
  440. 'top':
  441. 'var prop;\n' +
  442. 'if (typeof callback != \'function\') {\n' +
  443. ' var valueProp = callback;\n' +
  444. ' callback = function(value) { return value[valueProp] }\n' +
  445. '}\n' +
  446. 'else if (thisArg) {\n' +
  447. ' callback = iteratorBind(callback, thisArg)\n' +
  448. '}',
  449. 'inLoop':
  450. 'prop = callback(value, index, collection);\n' +
  451. '(hasOwnProperty.call(result, prop) ? result[prop]++ : result[prop] = 1)'
  452. };
  453. /** Reusable iterator options for `every` and `some` */
  454. var everyIteratorOptions = {
  455. 'init': 'true',
  456. 'inLoop': 'if (!callback(value, index, collection)) return !result'
  457. };
  458. /** Reusable iterator options for `defaults` and `extend` */
  459. var extendIteratorOptions = {
  460. 'useHas': false,
  461. 'useStrict': false,
  462. 'args': 'object',
  463. 'init': 'object',
  464. 'top':
  465. 'for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {\n' +
  466. ' if (iteratee = arguments[argsIndex]) {',
  467. 'inLoop': 'result[index] = value',
  468. 'bottom': ' }\n}'
  469. };
  470. /** Reusable iterator options for `filter`, `reject`, and `where` */
  471. var filterIteratorOptions = {
  472. 'init': '[]',
  473. 'inLoop': 'callback(value, index, collection) && result.push(value)'
  474. };
  475. /** Reusable iterator options for `find`, `forEach`, `forIn`, and `forOwn` */
  476. var forEachIteratorOptions = {
  477. 'top': 'if (thisArg) callback = iteratorBind(callback, thisArg)'
  478. };
  479. /** Reusable iterator options for `forIn` and `forOwn` */
  480. var forOwnIteratorOptions = {
  481. 'inLoop': {
  482. 'object': baseIteratorOptions.inLoop
  483. }
  484. };
  485. /** Reusable iterator options for `invoke`, `map`, `pluck`, and `sortBy` */
  486. var mapIteratorOptions = {
  487. 'init': '',
  488. 'exit': 'if (!collection) return []',
  489. 'beforeLoop': {
  490. 'array': 'result = Array(length)',
  491. 'object': 'result = ' + (isKeysFast ? 'Array(length)' : '[]')
  492. },
  493. 'inLoop': {
  494. 'array': 'result[index] = callback(value, index, collection)',
  495. 'object': 'result' + (isKeysFast ? '[ownIndex] = ' : '.push') + '(callback(value, index, collection))'
  496. }
  497. };
  498. /** Reusable iterator options for `omit` and `pick` */
  499. var omitIteratorOptions = {
  500. 'useHas': false,
  501. 'args': 'object, callback, thisArg',
  502. 'init': '{}',
  503. 'top':
  504. 'var isFunc = typeof callback == \'function\';\n' +
  505. 'if (!isFunc) {\n' +
  506. ' var props = concat.apply(ArrayProto, arguments)\n' +
  507. '} else if (thisArg) {\n' +
  508. ' callback = iteratorBind(callback, thisArg)\n' +
  509. '}',
  510. 'inLoop':
  511. 'if (isFunc\n' +
  512. ' ? !callback(value, index, object)\n' +
  513. ' : indexOf(props, index) < 0\n' +
  514. ') result[index] = value'
  515. };
  516. /*--------------------------------------------------------------------------*/
  517. /**
  518. * Creates a new function optimized for searching large arrays for a given `value`,
  519. * starting at `fromIndex`, using strict equality for comparisons, i.e. `===`.
  520. *
  521. * @private
  522. * @param {Array} array The array to search.
  523. * @param {Mixed} value The value to search for.
  524. * @param {Number} [fromIndex=0] The index to start searching from.
  525. * @param {Number} [largeSize=30] The length at which an array is considered large.
  526. * @returns {Boolean} Returns `true` if `value` is found, else `false`.
  527. */
  528. function cachedContains(array, fromIndex, largeSize) {
  529. fromIndex || (fromIndex = 0);
  530. var length = array.length,
  531. isLarge = (length - fromIndex) >= (largeSize || largeArraySize),
  532. cache = isLarge ? {} : array;
  533. if (isLarge) {
  534. // init value cache
  535. var key,
  536. index = fromIndex - 1;
  537. while (++index < length) {
  538. // manually coerce `value` to string because `hasOwnProperty`, in some
  539. // older versions of Firefox, coerces objects incorrectly
  540. key = array[index] + '';
  541. (hasOwnProperty.call(cache, key) ? cache[key] : (cache[key] = [])).push(array[index]);
  542. }
  543. }
  544. return function(value) {
  545. if (isLarge) {
  546. var key = value + '';
  547. return hasOwnProperty.call(cache, key) && indexOf(cache[key], value) > -1;
  548. }
  549. return indexOf(cache, value, fromIndex) > -1;
  550. }
  551. }
  552. /**
  553. * Creates compiled iteration functions. The iteration function will be created
  554. * to iterate over only objects if the first argument of `options.args` is
  555. * "object" or `options.inLoop.array` is falsey.
  556. *
  557. * @private
  558. * @param {Object} [options1, options2, ...] The compile options objects.
  559. *
  560. * useHas - A boolean to specify whether or not to use `hasOwnProperty` checks
  561. * in the object loop.
  562. *
  563. * useStrict - A boolean to specify whether or not to include the ES5
  564. * "use strict" directive.
  565. *
  566. * args - A string of comma separated arguments the iteration function will
  567. * accept.
  568. *
  569. * init - A string to specify the initial value of the `result` variable.
  570. *
  571. * exit - A string of code to use in place of the default exit-early check
  572. * of `if (!arguments[0]) return result`.
  573. *
  574. * top - A string of code to execute after the exit-early check but before
  575. * the iteration branches.
  576. *
  577. * beforeLoop - A string or object containing an "array" or "object" property
  578. * of code to execute before the array or object loops.
  579. *
  580. * inLoop - A string or object containing an "array" or "object" property
  581. * of code to execute in the array or object loops.
  582. *
  583. * bottom - A string of code to execute after the iteration branches but
  584. * before the `result` is returned.
  585. *
  586. * @returns {Function} Returns the compiled function.
  587. */
  588. function createIterator() {
  589. var object,
  590. prop,
  591. value,
  592. index = -1,
  593. length = arguments.length;
  594. // merge options into a template data object
  595. var data = {
  596. 'bottom': '',
  597. 'exit': '',
  598. 'init': '',
  599. 'top': '',
  600. 'arrayBranch': { 'beforeLoop': '' },
  601. 'objectBranch': { 'beforeLoop': '' }
  602. };
  603. while (++index < length) {
  604. object = arguments[index];
  605. for (prop in object) {
  606. value = (value = object[prop]) == null ? '' : value;
  607. // keep this regexp explicit for the build pre-process
  608. if (/beforeLoop|inLoop/.test(prop)) {
  609. if (typeof value == 'string') {
  610. value = { 'array': value, 'object': value };
  611. }
  612. data.arrayBranch[prop] = value.array || '';
  613. data.objectBranch[prop] = value.object || '';
  614. } else {
  615. data[prop] = value;
  616. }
  617. }
  618. }
  619. // set additional template `data` values
  620. var args = data.args,
  621. firstArg = /^[^,]+/.exec(args)[0],
  622. useStrict = data.useStrict;
  623. data.firstArg = firstArg;
  624. data.hasDontEnumBug = hasDontEnumBug;
  625. data.isKeysFast = isKeysFast;
  626. data.noArgsEnum = noArgsEnum;
  627. data.shadowed = shadowed;
  628. data.useHas = data.useHas !== false;
  629. data.useStrict = useStrict == null ? isStrictFast : useStrict;
  630. if (data.noCharByIndex == null) {
  631. data.noCharByIndex = noCharByIndex;
  632. }
  633. if (!data.exit) {
  634. data.exit = 'if (!' + firstArg + ') return result';
  635. }
  636. if (firstArg != 'collection' || !data.arrayBranch.inLoop) {
  637. data.arrayBranch = null;
  638. }
  639. // create the function factory
  640. var factory = Function(
  641. 'arrayLikeClasses, ArrayProto, bind, compareAscending, concat, forIn, ' +
  642. 'hasOwnProperty, identity, indexOf, isArguments, isArray, isFunction, ' +
  643. 'isPlainObject, iteratorBind, objectClass, objectTypes, nativeKeys, ' +
  644. 'propertyIsEnumerable, slice, stringClass, toString',
  645. 'var callee = function(' + args + ') {\n' + iteratorTemplate(data) + '\n};\n' +
  646. 'return callee'
  647. );
  648. // return the compiled function
  649. return factory(
  650. arrayLikeClasses, ArrayProto, bind, compareAscending, concat, forIn,
  651. hasOwnProperty, identity, indexOf, isArguments, isArray, isFunction,
  652. isPlainObject, iteratorBind, objectClass, objectTypes, nativeKeys,
  653. propertyIsEnumerable, slice, stringClass, toString
  654. );
  655. }
  656. /**
  657. * Used by `sortBy` to compare transformed `collection` values, stable sorting
  658. * them in ascending order.
  659. *
  660. * @private
  661. * @param {Object} a The object to compare to `b`.
  662. * @param {Object} b The object to compare to `a`.
  663. * @returns {Number} Returns the sort order indicator of `1` or `-1`.
  664. */
  665. function compareAscending(a, b) {
  666. var ai = a.index,
  667. bi = b.index;
  668. a = a.criteria;
  669. b = b.criteria;
  670. // ensure a stable sort in V8 and other engines
  671. // http://code.google.com/p/v8/issues/detail?id=90
  672. if (a !== b) {
  673. if (a > b || a === undefined) {
  674. return 1;
  675. }
  676. if (a < b || b === undefined) {
  677. return -1;
  678. }
  679. }
  680. return ai < bi ? -1 : 1;
  681. }
  682. /**
  683. * Used by `template` to replace tokens with their corresponding code snippets.
  684. *
  685. * @private
  686. * @param {String} match The matched token.
  687. * @param {String} index The `tokenized` index of the code snippet.
  688. * @returns {String} Returns the code snippet.
  689. */
  690. function detokenize(match, index) {
  691. return tokenized[index];
  692. }
  693. /**
  694. * Used by `template` to escape characters for inclusion in compiled
  695. * string literals.
  696. *
  697. * @private
  698. * @param {String} match The matched character to escape.
  699. * @returns {String} Returns the escaped character.
  700. */
  701. function escapeStringChar(match) {
  702. return '\\' + stringEscapes[match];
  703. }
  704. /**
  705. * Used by `escape` to convert characters to HTML entities.
  706. *
  707. * @private
  708. * @param {String} match The matched character to escape.
  709. * @returns {String} Returns the escaped character.
  710. */
  711. function escapeHtmlChar(match) {
  712. return htmlEscapes[match];
  713. }
  714. /**
  715. * Creates a new function that, when called, invokes `func` with the `this`
  716. * binding of `thisArg` and the arguments (value, index, object).
  717. *
  718. * @private
  719. * @param {Function} func The function to bind.
  720. * @param {Mixed} [thisArg] The `this` binding of `func`.
  721. * @returns {Function} Returns the new bound function.
  722. */
  723. function iteratorBind(func, thisArg) {
  724. return function(value, index, object) {
  725. return func.call(thisArg, value, index, object);
  726. };
  727. }
  728. /**
  729. * A no-operation function.
  730. *
  731. * @private
  732. */
  733. function noop() {
  734. // no operation performed
  735. }
  736. /**
  737. * Used by `template` to replace "escape" template delimiters with tokens.
  738. *
  739. * @private
  740. * @param {String} match The matched template delimiter.
  741. * @param {String} value The delimiter value.
  742. * @returns {String} Returns a token.
  743. */
  744. function tokenizeEscape(match, value) {
  745. if (match && reComplexDelimiter.test(value)) {
  746. return '<e%-' + value + '%>';
  747. }
  748. var index = tokenized.length;
  749. tokenized[index] = "' +\n__e(" + value + ") +\n'";
  750. return tokenHead + index + tokenFoot;
  751. }
  752. /**
  753. * Used by `template` to replace "evaluate" template delimiters, or complex
  754. * "escape" and "interpolate" delimiters, with tokens.
  755. *
  756. * @private
  757. * @param {String} match The matched template delimiter.
  758. * @param {String} escapeValue The complex "escape" delimiter value.
  759. * @param {String} interpolateValue The complex "interpolate" delimiter value.
  760. * @param {String} [evaluateValue] The "evaluate" delimiter value.
  761. * @returns {String} Returns a token.
  762. */
  763. function tokenizeEvaluate(match, escapeValue, interpolateValue, evaluateValue) {
  764. if (evaluateValue) {
  765. var index = tokenized.length;
  766. tokenized[index] = "';\n" + evaluateValue + ";\n__p += '";
  767. return tokenHead + index + tokenFoot;
  768. }
  769. return escapeValue
  770. ? tokenizeEscape(null, escapeValue)
  771. : tokenizeInterpolate(null, interpolateValue);
  772. }
  773. /**
  774. * Used by `template` to replace "interpolate" template delimiters with tokens.
  775. *
  776. * @private
  777. * @param {String} match The matched template delimiter.
  778. * @param {String} value The delimiter value.
  779. * @returns {String} Returns a token.
  780. */
  781. function tokenizeInterpolate(match, value) {
  782. if (match && reComplexDelimiter.test(value)) {
  783. return '<e%=' + value + '%>';
  784. }
  785. var index = tokenized.length;
  786. tokenized[index] = "' +\n((__t = (" + value + ")) == null ? '' : __t) +\n'";
  787. return tokenHead + index + tokenFoot;
  788. }
  789. /**
  790. * Used by `unescape` to convert HTML entities to characters.
  791. *
  792. * @private
  793. * @param {String} match The matched character to unescape.
  794. * @returns {String} Returns the unescaped character.
  795. */
  796. function unescapeHtmlChar(match) {
  797. return htmlUnescapes[match];
  798. }
  799. /*--------------------------------------------------------------------------*/
  800. /**
  801. * Checks if `value` is an `arguments` object.
  802. *
  803. * @static
  804. * @memberOf _
  805. * @category Objects
  806. * @param {Mixed} value The value to check.
  807. * @returns {Boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
  808. * @example
  809. *
  810. * (function() { return _.isArguments(arguments); })(1, 2, 3);
  811. * // => true
  812. *
  813. * _.isArguments([1, 2, 3]);
  814. * // => false
  815. */
  816. function isArguments(value) {
  817. return toString.call(value) == argsClass;
  818. }
  819. // fallback for browsers that can't detect `arguments` objects by [[Class]]
  820. if (noArgsClass) {
  821. isArguments = function(value) {
  822. return !!(value && hasOwnProperty.call(value, 'callee'));
  823. };
  824. }
  825. /**
  826. * Checks if `value` is an array.
  827. *
  828. * @static
  829. * @memberOf _
  830. * @category Objects
  831. * @param {Mixed} value The value to check.
  832. * @returns {Boolean} Returns `true` if the `value` is an array, else `false`.
  833. * @example
  834. *
  835. * (function() { return _.isArray(arguments); })();
  836. * // => false
  837. *
  838. * _.isArray([1, 2, 3]);
  839. * // => true
  840. */
  841. var isArray = nativeIsArray || function(value) {
  842. return toString.call(value) == arrayClass;
  843. };
  844. /**
  845. * Checks if `value` is a function.
  846. *
  847. * @static
  848. * @memberOf _
  849. * @category Objects
  850. * @param {Mixed} value The value to check.
  851. * @returns {Boolean} Returns `true` if the `value` is a function, else `false`.
  852. * @example
  853. *
  854. * _.isFunction(''.concat);
  855. * // => true
  856. */
  857. function isFunction(value) {
  858. return typeof value == 'function';
  859. }
  860. // fallback for older versions of Chrome and Safari
  861. if (isFunction(/x/)) {
  862. isFunction = function(value) {
  863. return toString.call(value) == funcClass;
  864. };
  865. }
  866. /**
  867. * A fallback implementation of `isPlainObject` that checks if a given `value`
  868. * is an object created by the `Object` constructor, assuming objects created
  869. * by the `Object` constructor have no inherited enumerable properties and that
  870. * there are no `Object.prototype` extensions.
  871. *
  872. * @private
  873. * @param {Mixed} value The value to check.
  874. * @param {Boolean} [skipArgsCheck=false] Internally used to skip checks for
  875. * `arguments` objects.
  876. * @returns {Boolean} Returns `true` if `value` is a plain object, else `false`.
  877. */
  878. function isPlainFallback(value, skipArgsCheck) {
  879. // avoid non-objects and false positives for `arguments` objects
  880. var result = false;
  881. if (!(value && typeof value == 'object') || (!skipArgsCheck && isArguments(value))) {
  882. return result;
  883. }
  884. // IE < 9 presents DOM nodes as `Object` objects except they have `toString`
  885. // methods that are `typeof` "string" and still can coerce nodes to strings.
  886. // Also check that the constructor is `Object` (i.e. `Object instanceof Object`)
  887. var ctor = value.constructor;
  888. if ((!noNodeClass || !(typeof value.toString != 'function' && typeof (value + '') == 'string')) &&
  889. (!isFunction(ctor) || ctor instanceof ctor)) {
  890. // IE < 9 iterates inherited properties before own properties. If the first
  891. // iterated property is an object's own property then there are no inherited
  892. // enumerable properties.
  893. if (iteratesOwnLast) {
  894. forIn(value, function(value, key, object) {
  895. result = !hasOwnProperty.call(object, key);
  896. return false;
  897. });
  898. return result === false;
  899. }
  900. // In most environments an object's own properties are iterated before
  901. // its inherited properties. If the last iterated property is an object's
  902. // own property then there are no inherited enumerable properties.
  903. forIn(value, function(value, key) {
  904. result = key;
  905. });
  906. return result === false || hasOwnProperty.call(value, result);
  907. }
  908. return result;
  909. }
  910. /**
  911. * Checks if a given `value` is an object created by the `Object` constructor.
  912. *
  913. * @private
  914. * @param {Mixed} value The value to check.
  915. * @param {Boolean} [skipArgsCheck=false] Internally used to skip checks for
  916. * `arguments` objects.
  917. * @returns {Boolean} Returns `true` if `value` is a plain object, else `false`.
  918. */
  919. var isPlainObject = objectTypes.__proto__ != ObjectProto ? isPlainFallback : function(value, skipArgsCheck) {
  920. if (!value) {
  921. return false;
  922. }
  923. var valueOf = value.valueOf,
  924. objProto = typeof valueOf == 'function' && (objProto = valueOf.__proto__) && objProto.__proto__;
  925. return objProto
  926. ? value == objProto || (value.__proto__ == objProto && (skipArgsCheck || !isArguments(value)))
  927. : isPlainFallback(value);
  928. };
  929. /**
  930. * A shim implementation of `Object.keys` that produces an array of the given
  931. * object's own enumerable property names.
  932. *
  933. * @private
  934. * @param {Object} object The object to inspect.
  935. * @returns {Array} Returns a new array of property names.
  936. */
  937. var shimKeys = createIterator({
  938. 'args': 'object',
  939. 'init': '[]',
  940. 'inLoop': 'result.push(index)'
  941. });
  942. /*--------------------------------------------------------------------------*/
  943. /**
  944. * Creates a clone of `value`. If `deep` is `true`, all nested objects will
  945. * also be cloned otherwise they will be assigned by reference. If a value has
  946. * a `clone` method it will be used to perform the clone. Functions, DOM nodes,
  947. * `arguments` objects, and objects created by constructors other than `Object`
  948. * are **not** cloned unless they have a custom `clone` method.
  949. *
  950. * @static
  951. * @memberOf _
  952. * @category Objects
  953. * @param {Mixed} value The value to clone.
  954. * @param {Boolean} deep A flag to indicate a deep clone.
  955. * @param {Object} [guard] Internally used to allow this method to work with
  956. * others like `_.map` without using their callback `index` argument for `deep`.
  957. * @param {Object} [data={}] Internally used to track traversed objects to avoid
  958. * circular references and indicate whether to perform a more thorough clone
  959. * of non-object values.
  960. * @returns {Mixed} Returns the cloned `value`.
  961. * @example
  962. *
  963. * var stooges = [
  964. * { 'name': 'moe', 'age': 40 },
  965. * { 'name': 'larry', 'age': 50 },
  966. * { 'name': 'curly', 'age': 60 }
  967. * ];
  968. *
  969. * _.clone({ 'name': 'moe' });
  970. * // => { 'name': 'moe' }
  971. *
  972. * var shallow = _.clone(stooges);
  973. * shallow[0] === stooges[0];
  974. * // => true
  975. *
  976. * var deep = _.clone(stooges, true);
  977. * shallow[0] === stooges[0];
  978. * // => false
  979. */
  980. function clone(value, deep, guard, data) {
  981. if (value == null) {
  982. return value;
  983. }
  984. if (guard) {
  985. deep = false;
  986. }
  987. // init internal data
  988. data || (data = { 'thorough': null });
  989. // avoid slower checks on primitives
  990. if (data.thorough == null) {
  991. // primitives passed from iframes use the primary document's native prototypes
  992. data.thorough = !!(BoolProto.clone || NumberProto.clone || StringProto.clone);
  993. }
  994. // use custom `clone` method if available
  995. var isObj = objectTypes[typeof value];
  996. if ((isObj || data.thorough) && value.clone && isFunction(value.clone)) {
  997. data.thorough = null;
  998. return value.clone(deep);
  999. }
  1000. // inspect [[Class]]
  1001. if (isObj) {
  1002. // don't clone `arguments` objects, functions, or non-object Objects
  1003. var className = toString.call(value);
  1004. if (!cloneableClasses[className] || (noArgsClass && isArguments(value))) {
  1005. return value;
  1006. }
  1007. var isArr = className == arrayClass;
  1008. isObj = isArr || (className == objectClass ? isPlainObject(value, true) : isObj);
  1009. }
  1010. // shallow clone
  1011. if (!isObj || !deep) {
  1012. // don't clone functions
  1013. return isObj
  1014. ? (isArr ? slice.call(value) : extend({}, value))
  1015. : value;
  1016. }
  1017. var ctor = value.constructor;
  1018. switch (className) {
  1019. case boolClass:
  1020. return new ctor(value == true);
  1021. case dateClass:
  1022. return new ctor(+value);
  1023. case numberClass:
  1024. case stringClass:
  1025. return new ctor(value);
  1026. case regexpClass:
  1027. return ctor(value.source, reFlags.exec(value));
  1028. }
  1029. var clones = data.clones || (data.clones = []),
  1030. sources = data.sources || (data.sources = []),
  1031. length = clones.length;
  1032. // check for circular references and return corresponding clone
  1033. while (length--) {
  1034. if (sources[length] == value) {
  1035. return clones[length];
  1036. }
  1037. }
  1038. // init cloned object
  1039. var result = isArr ? ctor(length = value.length) : {};
  1040. // add current clone and original source value to the stack of traversed objects
  1041. clones.push(result);
  1042. sources.push(value);
  1043. // recursively populate clone (susceptible to call stack limits)
  1044. if (isArr) {
  1045. var index = -1;
  1046. while (++index < length) {
  1047. result[index] = clone(value[index], deep, null, data);
  1048. }
  1049. } else {
  1050. forOwn(value, function(objValue, key) {
  1051. result[key] = clone(objValue, deep, null, data);
  1052. });
  1053. }
  1054. return result;
  1055. }
  1056. /**
  1057. * Assigns enumerable properties of the default object(s) to the `destination`
  1058. * object for all `destination` properties that resolve to `null`/`undefined`.
  1059. * Once a property is set, additional defaults of the same property will be
  1060. * ignored.
  1061. *
  1062. * @static
  1063. * @memberOf _
  1064. * @category Objects
  1065. * @param {Object} object The destination object.
  1066. * @param {Object} [default1, default2, ...] The default objects.
  1067. * @returns {Object} Returns the destination object.
  1068. * @example
  1069. *
  1070. * var iceCream = { 'flavor': 'chocolate' };
  1071. * _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'rainbow' });
  1072. * // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' }
  1073. */
  1074. var defaults = createIterator(extendIteratorOptions, {
  1075. 'inLoop': 'if (result[index] == null) ' + extendIteratorOptions.inLoop
  1076. });
  1077. /**
  1078. * Assigns enumerable properties of the source object(s) to the `destination`
  1079. * object. Subsequent sources will overwrite propery assignments of previous
  1080. * sources.
  1081. *
  1082. * @static
  1083. * @memberOf _
  1084. * @category Objects
  1085. * @param {Object} object The destination object.
  1086. * @param {Object} [source1, source2, ...] The source objects.
  1087. * @returns {Object} Returns the destination object.
  1088. * @example
  1089. *
  1090. * _.extend({ 'name': 'moe' }, { 'age': 40 });
  1091. * // => { 'name': 'moe', 'age': 40 }
  1092. */
  1093. var extend = createIterator(extendIteratorOptions);
  1094. /**
  1095. * Iterates over `object`'s own and inherited enumerable properties, executing
  1096. * the `callback` for each property. The `callback` is bound to `thisArg` and
  1097. * invoked with 3 arguments; (value, key, object). Callbacks may exit iteration
  1098. * early by explicitly returning `false`.
  1099. *
  1100. * @static
  1101. * @memberOf _
  1102. * @category Objects
  1103. * @param {Object} object The object to iterate over.
  1104. * @param {Function} callback The function called per iteration.
  1105. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1106. * @returns {Object} Returns `object`.
  1107. * @example
  1108. *
  1109. * function Dog(name) {
  1110. * this.name = name;
  1111. * }
  1112. *
  1113. * Dog.prototype.bark = function() {
  1114. * alert('Woof, woof!');
  1115. * };
  1116. *
  1117. * _.forIn(new Dog('Dagny'), function(value, key) {
  1118. * alert(key);
  1119. * });
  1120. * // => alerts 'name' and 'bark' (order is not guaranteed)
  1121. */
  1122. var forIn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions, {
  1123. 'useHas': false
  1124. });
  1125. /**
  1126. * Iterates over `object`'s own enumerable properties, executing the `callback`
  1127. * for each property. The `callback` is bound to `thisArg` and invoked with 3
  1128. * arguments; (value, key, object). Callbacks may exit iteration early by
  1129. * explicitly returning `false`.
  1130. *
  1131. * @static
  1132. * @memberOf _
  1133. * @category Objects
  1134. * @param {Object} object The object to iterate over.
  1135. * @param {Function} callback The function called per iteration.
  1136. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1137. * @returns {Object} Returns `object`.
  1138. * @example
  1139. *
  1140. * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  1141. * alert(key);
  1142. * });
  1143. * // => alerts '0', '1', and 'length' (order is not guaranteed)
  1144. */
  1145. var forOwn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions);
  1146. /**
  1147. * Creates a sorted array of all enumerable properties, own and inherited,
  1148. * of `object` that have function values.
  1149. *
  1150. * @static
  1151. * @memberOf _
  1152. * @alias methods
  1153. * @category Objects
  1154. * @param {Object} object The object to inspect.
  1155. * @returns {Array} Returns a new array of property names that have function values.
  1156. * @example
  1157. *
  1158. * _.functions(_);
  1159. * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
  1160. */
  1161. var functions = createIterator({
  1162. 'useHas': false,
  1163. 'args': 'object',
  1164. 'init': '[]',
  1165. 'inLoop': 'if (isFunction(value)) result.push(index)',
  1166. 'bottom': 'result.sort()'
  1167. });
  1168. /**
  1169. * Checks if the specified object `property` exists and is a direct property,
  1170. * instead of an inherited property.
  1171. *
  1172. * @static
  1173. * @memberOf _
  1174. * @category Objects
  1175. * @param {Object} object The object to check.
  1176. * @param {String} property The property to check for.
  1177. * @returns {Boolean} Returns `true` if key is a direct property, else `false`.
  1178. * @example
  1179. *
  1180. * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
  1181. * // => true
  1182. */
  1183. function has(object, property) {
  1184. return object ? hasOwnProperty.call(object, property) : false;
  1185. }
  1186. /**
  1187. * Creates an object composed of the inverted keys and values of the given `object`.
  1188. *
  1189. * @static
  1190. * @memberOf _
  1191. * @category Objects
  1192. * @param {Object} object The object to invert.
  1193. * @returns {Object} Returns the created inverted object.
  1194. * @example
  1195. *
  1196. * _.invert({ 'first': 'Moe', 'second': 'Larry', 'third': 'Curly' });
  1197. * // => { 'Moe': 'first', 'Larry': 'second', 'Curly': 'third' } (order is not guaranteed)
  1198. */
  1199. var invert = createIterator({
  1200. 'args': 'object',
  1201. 'init': '{}',
  1202. 'inLoop': 'result[value] = index'
  1203. });
  1204. /**
  1205. * Checks if `value` is a boolean (`true` or `false`) value.
  1206. *
  1207. * @static
  1208. * @memberOf _
  1209. * @category Objects
  1210. * @param {Mixed} value The value to check.
  1211. * @returns {Boolean} Returns `true` if the `value` is a boolean value, else `false`.
  1212. * @example
  1213. *
  1214. * _.isBoolean(null);
  1215. * // => false
  1216. */
  1217. function isBoolean(value) {
  1218. return value === true || value === false || toString.call(value) == boolClass;
  1219. }
  1220. /**
  1221. * Checks if `value` is a date.
  1222. *
  1223. * @static
  1224. * @memberOf _
  1225. * @category Objects
  1226. * @param {Mixed} value The value to check.
  1227. * @returns {Boolean} Returns `true` if the `value` is a date, else `false`.
  1228. * @example
  1229. *
  1230. * _.isDate(new Date);
  1231. * // => true
  1232. */
  1233. function isDate(value) {
  1234. return toString.call(value) == dateClass;
  1235. }
  1236. /**
  1237. * Checks if `value` is a DOM element.
  1238. *
  1239. * @static
  1240. * @memberOf _
  1241. * @category Objects
  1242. * @param {Mixed} value The value to check.
  1243. * @returns {Boolean} Returns `true` if the `value` is a DOM element, else `false`.
  1244. * @example
  1245. *
  1246. * _.isElement(document.body);
  1247. * // => true
  1248. */
  1249. function isElement(value) {
  1250. return value ? value.nodeType === 1 : false;
  1251. }
  1252. /**
  1253. * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
  1254. * length of `0` and objects with no own enumerable properties are considered
  1255. * "empty".
  1256. *
  1257. * @static
  1258. * @memberOf _
  1259. * @category Objects
  1260. * @param {Array|Object|String} value The value to inspect.
  1261. * @returns {Boolean} Returns `true` if the `value` is empty, else `false`.
  1262. * @example
  1263. *
  1264. * _.isEmpty([1, 2, 3]);
  1265. * // => false
  1266. *
  1267. * _.isEmpty({});
  1268. * // => true
  1269. *
  1270. * _.isEmpty('');
  1271. * // => true
  1272. */
  1273. var isEmpty = createIterator({
  1274. 'args': 'value',
  1275. 'init': 'true',
  1276. 'top':
  1277. 'var className = toString.call(value),\n' +
  1278. ' length = value.length;\n' +
  1279. 'if (arrayLikeClasses[className]' +
  1280. (noArgsClass ? ' || isArguments(value)' : '') + ' ||\n' +
  1281. ' (className == objectClass && length > -1 && length === length >>> 0 &&\n' +
  1282. ' isFunction(value.splice))' +
  1283. ') return !length',
  1284. 'inLoop': {
  1285. 'object': 'return false'
  1286. }
  1287. });
  1288. /**
  1289. * Performs a deep comparison between two values to determine if they are
  1290. * equivalent to each other. If a value has an `isEqual` method it will be
  1291. * used to perform the comparison.
  1292. *
  1293. * @static
  1294. * @memberOf _
  1295. * @category Objects
  1296. * @param {Mixed} a The value to compare.
  1297. * @param {Mixed} b The other value to compare.
  1298. * @param {Object} [data={}] Internally used track traversed objects to avoid
  1299. * circular references and indicate whether to perform a more thorough comparison
  1300. * of non-object values.
  1301. * @returns {Boolean} Returns `true` if the values are equvalent, else `false`.
  1302. * @example
  1303. *
  1304. * var moe = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
  1305. * var clone = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
  1306. *
  1307. * moe == clone;
  1308. * // => false
  1309. *
  1310. * _.isEqual(moe, clone);
  1311. * // => true
  1312. */
  1313. function isEqual(a, b, data) {
  1314. // a strict comparison is necessary because `null == undefined`
  1315. if (a == null || b == null) {
  1316. return a === b;
  1317. }
  1318. // init internal data
  1319. data || (data = { 'thorough': null });
  1320. // avoid slower checks on non-objects
  1321. if (data.thorough == null) {
  1322. // primitives passed from iframes use the primary document's native prototypes
  1323. data.thorough = !!(BoolProto.isEqual || NumberProto.isEqual || StringProto.isEqual);
  1324. }
  1325. if (objectTypes[typeof a] || objectTypes[typeof b] || data.thorough) {
  1326. // unwrap any LoDash wrapped values
  1327. a = a.__wrapped__ || a;
  1328. b = b.__wrapped__ || b;
  1329. // use custom `isEqual` method if available
  1330. if (a.isEqual && isFunction(a.isEqual)) {
  1331. data.thorough = null;
  1332. return a.isEqual(b);
  1333. }
  1334. if (b.isEqual && isFunction(b.isEqual)) {
  1335. data.thorough = null;
  1336. return b.isEqual(a);
  1337. }
  1338. }
  1339. // exit early for identical values
  1340. if (a === b) {
  1341. // treat `+0` vs. `-0` as not equal
  1342. return a !== 0 || (1 / a == 1 / b);
  1343. }
  1344. // compare [[Class]] names
  1345. var className = toString.call(a);
  1346. if (className != toString.call(b)) {
  1347. return false;
  1348. }
  1349. switch (className) {
  1350. case boolClass:
  1351. case dateClass:
  1352. // coerce dates and booleans to numbers, dates to milliseconds and booleans
  1353. // to `1` or `0`, treating invalid dates coerced to `NaN` as not equal
  1354. return +a == +b;
  1355. case numberClass:
  1356. // treat `NaN` vs. `NaN` as equal
  1357. return a != +a
  1358. ? b != +b
  1359. // but treat `+0` vs. `-0` as not equal
  1360. : (a == 0 ? (1 / a == 1 / b) : a == +b);
  1361. case regexpClass:
  1362. case stringClass:
  1363. // coerce regexes to strings (http://es5.github.com/#x15.10.6.4)
  1364. // treat string primitives and their corresponding object instances as equal
  1365. return a == b + '';
  1366. }
  1367. // exit early, in older browsers, if `a` is array-like but not `b`
  1368. var isArr = arrayLikeClasses[className];
  1369. if (noArgsClass && !isArr && (isArr = isArguments(a)) && !isArguments(b)) {
  1370. return false;
  1371. }
  1372. // exit for functions and DOM nodes
  1373. if (!isArr && (className != objectClass || (noNodeClass && (
  1374. (typeof a.toString != 'function' && typeof (a + '') == 'string') ||
  1375. (typeof b.toString != 'function' && typeof (b + '') == 'string'))))) {
  1376. return false;
  1377. }
  1378. // assume cyclic structures are equal
  1379. // the algorithm for detecting cyclic structures is adapted from ES 5.1
  1380. // section 15.12.3, abstract operation `JO` (http://es5.github.com/#x15.12.3)
  1381. var stackA = data.stackA || (data.stackA = []),
  1382. stackB = data.stackB || (data.stackB = []),
  1383. length = stackA.length;
  1384. while (length--) {
  1385. if (stackA[length] == a) {
  1386. return stackB[length] == b;
  1387. }
  1388. }
  1389. var index = -1,
  1390. result = true,
  1391. size = 0;
  1392. // add `a` and `b` to the stack of traversed objects
  1393. stackA.push(a);
  1394. stackB.push(b);
  1395. // recursively compare objects and arrays (susceptible to call stack limits)
  1396. if (isArr) {
  1397. // compare lengths to determine if a deep comparison is necessary
  1398. size = a.length;
  1399. result = size == b.length;
  1400. if (result) {
  1401. // deep compare the contents, ignoring non-numeric properties
  1402. while (size--) {
  1403. if (!(result = isEqual(a[size], b[size], data))) {
  1404. break;
  1405. }
  1406. }
  1407. }
  1408. return result;
  1409. }
  1410. var ctorA = a.constructor,
  1411. ctorB = b.constructor;
  1412. // non `Object` object instances with different constructors are not equal
  1413. if (ctorA != ctorB && !(
  1414. isFunction(ctorA) && ctorA instanceof ctorA &&
  1415. isFunction(ctorB) && ctorB instanceof ctorB
  1416. )) {
  1417. return false;
  1418. }
  1419. // deep compare objects
  1420. for (var prop in a) {
  1421. if (hasOwnProperty.call(a, prop)) {
  1422. // count the number of properties.
  1423. size++;
  1424. // deep compare each property value.
  1425. if (!(hasOwnProperty.call(b, prop) && isEqual(a[prop], b[prop], data))) {
  1426. return false;
  1427. }
  1428. }
  1429. }
  1430. // ensure both objects have the same number of properties
  1431. for (prop in b) {
  1432. // The JS engine in Adobe products, like InDesign, has a bug that causes
  1433. // `!size--` to throw an error so it must be wrapped in parentheses.
  1434. // https://github.com/documentcloud/underscore/issues/355
  1435. if (hasOwnProperty.call(b, prop) && !(size--)) {
  1436. // `size` will be `-1` if `b` has more properties than `a`
  1437. return false;
  1438. }
  1439. }
  1440. // handle JScript [[DontEnum]] bug
  1441. if (hasDontEnumBug) {
  1442. while (++index < 7) {
  1443. prop = shadowed[index];
  1444. if (hasOwnProperty.call(a, prop) &&
  1445. !(hasOwnProperty.call(b, prop) && isEqual(a[prop], b[prop], data))) {
  1446. return false;
  1447. }
  1448. }
  1449. }
  1450. return true;
  1451. }
  1452. /**
  1453. * Checks if `value` is a finite number.
  1454. *
  1455. * Note: This is not the same as native `isFinite`, which will return true for
  1456. * booleans and other values. See http://es5.github.com/#x15.1.2.5.
  1457. *
  1458. * @deprecated
  1459. * @static
  1460. * @memberOf _
  1461. * @category Objects
  1462. * @param {Mixed} value The value to check.
  1463. * @returns {Boolean} Returns `true` if the `value` is a finite number, else `false`.
  1464. * @example
  1465. *
  1466. * _.isFinite(-101);
  1467. * // => true
  1468. *
  1469. * _.isFinite('10');
  1470. * // => false
  1471. *
  1472. * _.isFinite(Infinity);
  1473. * // => false
  1474. */
  1475. function isFinite(value) {
  1476. return nativeIsFinite(value) && toString.call(value) == numberClass;
  1477. }
  1478. /**
  1479. * Checks if `value` is the language type of Object.
  1480. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  1481. *
  1482. * @static
  1483. * @memberOf _
  1484. * @category Objects
  1485. * @param {Mixed} value The value to check.
  1486. * @returns {Boolean} Returns `true` if the `value` is an object, else `false`.
  1487. * @example
  1488. *
  1489. * _.isObject({});
  1490. * // => true
  1491. *
  1492. * _.isObject(1);
  1493. * // => false
  1494. */
  1495. function isObject(value) {
  1496. // check if the value is the ECMAScript language type of Object
  1497. // http://es5.github.com/#x8
  1498. // and avoid a V8 bug
  1499. // http://code.google.com/p/v8/issues/detail?id=2291
  1500. return value ? objectTypes[typeof value] : false;
  1501. }
  1502. /**
  1503. * Checks if `value` is `NaN`.
  1504. *
  1505. * Note: This is not the same as native `isNaN`, which will return true for
  1506. * `undefined` and other values. See http://es5.github.com/#x15.1.2.4.
  1507. *
  1508. * @deprecated
  1509. * @static
  1510. * @memberOf _
  1511. * @category Objects
  1512. * @param {Mixed} value The value to check.
  1513. * @returns {Boolean} Returns `true` if the `value` is `NaN`, else `false`.
  1514. * @example
  1515. *
  1516. * _.isNaN(NaN);
  1517. * // => true
  1518. *
  1519. * _.isNaN(new Number(NaN));
  1520. * // => true
  1521. *
  1522. * isNaN(undefined);
  1523. * // => true
  1524. *
  1525. * _.isNaN(undefined);
  1526. * // => false
  1527. */
  1528. function isNaN(value) {
  1529. // `NaN` as a primitive is the only value that is not equal to itself
  1530. // (perform the [[Class]] check first to avoid errors with some host objects in IE)
  1531. return toString.call(value) == numberClass && value != +value
  1532. }
  1533. /**
  1534. * Checks if `value` is `null`.
  1535. *
  1536. * @deprecated
  1537. * @static
  1538. * @memberOf _
  1539. * @category Objects
  1540. * @param {Mixed} value The value to check.
  1541. * @returns {Boolean} Returns `true` if the `value` is `null`, else `false`.
  1542. * @example
  1543. *
  1544. * _.isNull(null);
  1545. * // => true
  1546. *
  1547. * _.isNull(undefined);
  1548. * // => false
  1549. */
  1550. function isNull(value) {
  1551. return value === null;
  1552. }
  1553. /**
  1554. * Checks if `value` is a number.
  1555. *
  1556. * @static
  1557. * @memberOf _
  1558. * @category Objects
  1559. * @param {Mixed} value The value to check.
  1560. * @returns {Boolean} Returns `true` if the `value` is a number, else `false`.
  1561. * @example
  1562. *
  1563. * _.isNumber(8.4 * 5;
  1564. * // => true
  1565. */
  1566. function isNumber(value) {
  1567. return toString.call(value) == numberClass;
  1568. }
  1569. /**
  1570. * Checks if `value` is a regular expression.
  1571. *
  1572. * @static
  1573. * @memberOf _
  1574. * @category Objects
  1575. * @param {Mixed} value The value to check.
  1576. * @returns {Boolean} Returns `true` if the `value` is a regular expression, else `false`.
  1577. * @example
  1578. *
  1579. * _.isRegExp(/moe/);
  1580. * // => true
  1581. */
  1582. function isRegExp(value) {
  1583. return toString.call(value) == regexpClass;
  1584. }
  1585. /**
  1586. * Checks if `value` is a string.
  1587. *
  1588. * @static
  1589. * @memberOf _
  1590. * @category Objects
  1591. * @param {Mixed} value The value to check.
  1592. * @returns {Boolean} Returns `true` if the `value` is a string, else `false`.
  1593. * @example
  1594. *
  1595. * _.isString('moe');
  1596. * // => true
  1597. */
  1598. function isString(value) {
  1599. return toString.call(value) == stringClass;
  1600. }
  1601. /**
  1602. * Checks if `value` is `undefined`.
  1603. *
  1604. * @deprecated
  1605. * @static
  1606. * @memberOf _
  1607. * @category Objects
  1608. * @param {Mixed} value The value to check.
  1609. * @returns {Boolean} Returns `true` if the `value` is `undefined`, else `false`.
  1610. * @example
  1611. *
  1612. * _.isUndefined(void 0);
  1613. * // => true
  1614. */
  1615. function isUndefined(value) {
  1616. return value === undefined;
  1617. }
  1618. /**
  1619. * Creates an array composed of the own enumerable property names of `object`.
  1620. *
  1621. * @static
  1622. * @memberOf _
  1623. * @category Objects
  1624. * @param {Object} object The object to inspect.
  1625. * @returns {Array} Returns a new array of property names.
  1626. * @example
  1627. *
  1628. * _.keys({ 'one': 1, 'two': 2, 'three': 3 });
  1629. * // => ['one', 'two', 'three'] (order is not guaranteed)
  1630. */
  1631. var keys = !nativeKeys ? shimKeys : function(object) {
  1632. var type = typeof object;
  1633. // avoid iterating over the `prototype` property
  1634. if (type == 'function' && propertyIsEnumerable.call(object, 'prototype')) {
  1635. return shimKeys(object);
  1636. }
  1637. return object && objectTypes[type]
  1638. ? nativeKeys(object)
  1639. : [];
  1640. };
  1641. /**
  1642. * Merges enumerable properties of the source object(s) into the `destination`
  1643. * object. Subsequent sources will overwrite propery assignments of previous
  1644. * sources.
  1645. *
  1646. * @static
  1647. * @memberOf _
  1648. * @category Objects
  1649. * @param {Object} object The destination object.
  1650. * @param {Object} [source1, source2, ...] The source objects.
  1651. * @param {Object} [indicator] Internally used to indicate that the `stack`
  1652. * argument is an array of traversed objects instead of another source object.
  1653. * @param {Object} [data={}] Internally used to track traversed objects to avoid
  1654. * circular references.
  1655. * @returns {Object} Returns the destination object.
  1656. * @example
  1657. *
  1658. * var stooges = [
  1659. * { 'name': 'moe' },
  1660. * { 'name': 'larry' }
  1661. * ];
  1662. *
  1663. * var ages = [
  1664. * { 'age': 40 },
  1665. * { 'age': 50 }
  1666. * ];
  1667. *
  1668. * _.merge(stooges, ages);
  1669. * // => [{ 'name': 'moe', 'age': 40 }, { 'name': 'larry', 'age': 50 }]
  1670. */
  1671. var merge = createIterator(extendIteratorOptions, {
  1672. 'args': 'object, source, indicator',
  1673. 'top':
  1674. 'var isArr, recursive = indicator == isPlainObject,\n' +
  1675. ' data = recursive ? arguments[3] : { values: [], sources: [] };\n' +
  1676. 'for (var argsIndex = 1, argsLength = recursive ? 2 : arguments.length; argsIndex < argsLength; argsIndex++) {\n' +
  1677. ' if (iteratee = arguments[argsIndex]) {',
  1678. 'inLoop':
  1679. 'if ((source = value) && ((isArr = isArray(source)) || isPlainObject(source))) {\n' +
  1680. ' var found = false, values = data.values, sources = data.sources, stackLength = sources.length;\n' +
  1681. ' while (stackLength--) {\n' +
  1682. ' if (found = sources[stackLength] == source) break\n' +
  1683. ' }\n' +
  1684. ' if (found) {\n' +
  1685. ' result[index] = values[stackLength]\n' +
  1686. ' } else {\n' +
  1687. ' values.push(value = (value = result[index]) && isArr\n' +
  1688. ' ? (isArray(value) ? value : [])\n' +
  1689. ' : (isPlainObject(value) ? value : {})\n' +
  1690. ' );\n' +
  1691. ' sources.push(source);\n' +
  1692. ' result[index] = callee(value, source, isPlainObject, data)\n' +
  1693. ' }\n' +
  1694. '} else if (source != null) {\n' +
  1695. ' result[index] = source\n' +
  1696. '}'
  1697. });
  1698. /**
  1699. * Creates a shallow clone of `object` excluding the specified properties.
  1700. * Property names may be specified as individual arguments or as arrays of
  1701. * property names. If `callback` is passed, it will be executed for each property
  1702. * in the `object`, omitting the properties `callback` returns truthy for. The
  1703. * `callback` is bound to `thisArg` and invoked with 3 arguments; (value, key, object).
  1704. *
  1705. * @static
  1706. * @memberOf _
  1707. * @category Objects
  1708. * @param {Object} object The source object.
  1709. * @param {Function|String} callback|[prop1, prop2, ...] The properties to omit
  1710. * or the function called per iteration.
  1711. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1712. * @returns {Object} Returns an object without the omitted properties.
  1713. * @example
  1714. *
  1715. * _.omit({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'userid');
  1716. * // => { 'name': 'moe', 'age': 40 }
  1717. *
  1718. * _.omit({ 'name': 'moe', '_hint': 'knucklehead', '_seed': '96c4eb' }, function(value, key) {
  1719. * return key.charAt(0) == '_';
  1720. * });
  1721. * // => { 'name': 'moe' }
  1722. */
  1723. var omit = createIterator(omitIteratorOptions);
  1724. /**
  1725. * Creates a two dimensional array of the given object's key-value pairs,
  1726. * i.e. `[[key1, value1], [key2, value2]]`.
  1727. *
  1728. * @static
  1729. * @memberOf _
  1730. * @category Objects
  1731. * @param {Object} object The object to inspect.
  1732. * @returns {Array} Returns new array of key-value pairs.
  1733. * @example
  1734. *
  1735. * _.pairs({ 'moe': 30, 'larry': 40, 'curly': 50 });
  1736. * // => [['moe', 30], ['larry', 40], ['curly', 50]] (order is not guaranteed)
  1737. */
  1738. var pairs = createIterator({
  1739. 'args': 'object',
  1740. 'init':'[]',
  1741. 'inLoop': 'result' + (isKeysFast ? '[ownIndex] = ' : '.push') + '([index, value])'
  1742. });
  1743. /**
  1744. * Creates a shallow clone of `object` composed of the specified properties.
  1745. * Property names may be specified as individual arguments or as arrays of
  1746. * property names. If `callback` is passed, it will be executed for each property
  1747. * in the `object`, picking the properties `callback` returns truthy for. The
  1748. * `callback` is bound to `thisArg` and invoked with 3 arguments; (value, key, object).
  1749. *
  1750. * @static
  1751. * @memberOf _
  1752. * @category Objects
  1753. * @param {Object} object The source object.
  1754. * @param {Function|String} callback|[prop1, prop2, ...] The properties to pick
  1755. * or the function called per iteration.
  1756. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1757. * @returns {Object} Returns an object composed of the picked properties.
  1758. * @example
  1759. *
  1760. * _.pick({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'name', 'age');
  1761. * // => { 'name': 'moe', 'age': 40 }
  1762. *
  1763. * _.pick({ 'name': 'moe', '_hint': 'knucklehead', '_seed': '96c4eb' }, function(value, key) {
  1764. * return key.charAt(0) != '_';
  1765. * });
  1766. * // => { 'name': 'moe' }
  1767. */
  1768. var pick = createIterator(omitIteratorOptions, {
  1769. 'top':
  1770. 'if (typeof callback != \'function\') {\n' +
  1771. ' var prop,\n' +
  1772. ' props = concat.apply(ArrayProto, arguments),\n' +
  1773. ' length = props.length;\n' +
  1774. ' for (index = 1; index < length; index++) {\n' +
  1775. ' prop = props[index];\n' +
  1776. ' if (prop in object) result[prop] = object[prop]\n' +
  1777. ' }\n' +
  1778. '} else {\n' +
  1779. ' if (thisArg) callback = iteratorBind(callback, thisArg)',
  1780. 'inLoop':
  1781. 'if (callback(value, index, object)) result[index] = value',
  1782. 'bottom': '}'
  1783. });
  1784. /**
  1785. * Creates an array composed of the own enumerable property values of `object`.
  1786. *
  1787. * @static
  1788. * @memberOf _
  1789. * @category Objects
  1790. * @param {Object} object The object to inspect.
  1791. * @returns {Array} Returns a new array of property values.
  1792. * @example
  1793. *
  1794. * _.values({ 'one': 1, 'two': 2, 'three': 3 });
  1795. * // => [1, 2, 3]
  1796. */
  1797. var values = createIterator({
  1798. 'args': 'object',
  1799. 'init': '[]',
  1800. 'inLoop': 'result.push(value)'
  1801. });
  1802. /*--------------------------------------------------------------------------*/
  1803. /**
  1804. * Checks if a given `target` element is present in a `collection` using strict
  1805. * equality for comparisons, i.e. `===`.
  1806. *
  1807. * @static
  1808. * @memberOf _
  1809. * @alias include
  1810. * @category Collections
  1811. * @param {Array|Object|String} collection The collection to iterate over.
  1812. * @param {Mixed} target The value to check for.
  1813. * @returns {Boolean} Returns `true` if the `target` element is found, else `false`.
  1814. * @example
  1815. *
  1816. * _.contains([1, 2, 3], 3);
  1817. * // => true
  1818. *
  1819. * _.contains({ 'name': 'moe', 'age': 40 }, 'moe');
  1820. * // => true
  1821. *
  1822. * _.contains('curly', 'ur');
  1823. * // => true
  1824. */
  1825. var contains = createIterator({
  1826. 'args': 'collection, target',
  1827. 'init': 'false',
  1828. 'noCharByIndex': false,
  1829. 'beforeLoop': {
  1830. 'array': 'if (toString.call(collection) == stringClass) return collection.indexOf(target) > -1'
  1831. },
  1832. 'inLoop': 'if (value === target) return true'
  1833. });
  1834. /**
  1835. * Creates an object composed of keys returned from running each element of
  1836. * `collection` through a `callback`. The corresponding value of each key is
  1837. * the number of times the key was returned by `callback`. The `callback` is
  1838. * bound to `thisArg` and invoked with 3 arguments; (value, index|key, collection).
  1839. * The `callback` argument may also be the name of a property to count by (e.g. 'length').
  1840. *
  1841. * @static
  1842. * @memberOf _
  1843. * @category Collections
  1844. * @param {Array|Object|String} collection The collection to iterate over.
  1845. * @param {Function|String} callback|property The function called per iteration
  1846. * or property name to count by.
  1847. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1848. * @returns {Object} Returns the composed aggregate object.
  1849. * @example
  1850. *
  1851. * _.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); });
  1852. * // => { '4': 1, '6': 2 }
  1853. *
  1854. * _.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
  1855. * // => { '4': 1, '6': 2 }
  1856. *
  1857. * _.countBy(['one', 'two', 'three'], 'length');
  1858. * // => { '3': 2, '5': 1 }
  1859. */
  1860. var countBy = createIterator(baseIteratorOptions, countByIteratorOptions);
  1861. /**
  1862. * Checks if the `callback` returns a truthy value for **all** elements of a
  1863. * `collection`. The `callback` is bound to `thisArg` and invoked with 3
  1864. * arguments; (value, index|key, collection).
  1865. *
  1866. * @static
  1867. * @memberOf _
  1868. * @alias all
  1869. * @category Collections
  1870. * @param {Array|Object|String} collection The collection to iterate over.
  1871. * @param {Function} [callback=identity] The function called per iteration.
  1872. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1873. * @returns {Boolean} Returns `true` if all elements pass the callback check, else `false`.
  1874. * @example
  1875. *
  1876. * _.every([true, 1, null, 'yes'], Boolean);
  1877. * // => false
  1878. */
  1879. var every = createIterator(baseIteratorOptions, everyIteratorOptions);
  1880. /**
  1881. * Examines each element in a `collection`, returning an array of all elements
  1882. * the `callback` returns truthy for. The `callback` is bound to `thisArg` and
  1883. * invoked with 3 arguments; (value, index|key, collection).
  1884. *
  1885. * @static
  1886. * @memberOf _
  1887. * @alias select
  1888. * @category Collections
  1889. * @param {Array|Object|String} collection The collection to iterate over.
  1890. * @param {Function} [callback=identity] The function called per iteration.
  1891. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1892. * @returns {Array} Returns a new array of elements that passed callback check.
  1893. * @example
  1894. *
  1895. * var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  1896. * // => [2, 4, 6]
  1897. */
  1898. var filter = createIterator(baseIteratorOptions, filterIteratorOptions);
  1899. /**
  1900. * Examines each element in a `collection`, returning the first one the `callback`
  1901. * returns truthy for. The function returns as soon as it finds an acceptable
  1902. * element, and does not iterate over the entire `collection`. The `callback` is
  1903. * bound to `thisArg` and invoked with 3 arguments; (value, index|key, collection).
  1904. *
  1905. * @static
  1906. * @memberOf _
  1907. * @alias detect
  1908. * @category Collections
  1909. * @param {Array|Object|String} collection The collection to iterate over.
  1910. * @param {Function} callback The function called per iteration.
  1911. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1912. * @returns {Mixed} Returns the element that passed the callback check, else `undefined`.
  1913. * @example
  1914. *
  1915. * var even = _.find([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  1916. * // => 2
  1917. */
  1918. var find = createIterator(baseIteratorOptions, forEachIteratorOptions, {
  1919. 'init': '',
  1920. 'inLoop': 'if (callback(value, index, collection)) return value'
  1921. });
  1922. /**
  1923. * Iterates over a `collection`, executing the `callback` for each element in
  1924. * the `collection`. The `callback` is bound to `thisArg` and invoked with 3
  1925. * arguments; (value, index|key, collection). Callbacks may exit iteration
  1926. * early by explicitly returning `false`.
  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 The function called per iteration.
  1934. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1935. * @returns {Array|Object} Returns `collection`.
  1936. * @example
  1937. *
  1938. * _([1, 2, 3]).forEach(alert).join(',');
  1939. * // => alerts each number and returns '1,2,3'
  1940. *
  1941. * _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, alert);
  1942. * // => alerts each number (order is not guaranteed)
  1943. */
  1944. var forEach = createIterator(baseIteratorOptions, forEachIteratorOptions);
  1945. /**
  1946. * Creates an object composed of keys returned from running each element of
  1947. * `collection` through a `callback`. The corresponding value of each key is an
  1948. * array of elements passed to `callback` that returned the key. The `callback`
  1949. * is bound to `thisArg` and invoked with 3 arguments; (value, index|key, collection).
  1950. * The `callback` argument may also be the name of a property to count by (e.g. 'length').
  1951. *
  1952. * @static
  1953. * @memberOf _
  1954. * @category Collections
  1955. * @param {Array|Object|String} collection The collection to iterate over.
  1956. * @param {Function|String} callback|property The function called per iteration
  1957. * or property name to group by.
  1958. * @param {Mixed} [thisArg] The `this` binding for the callback.
  1959. * @returns {Object} Returns the composed aggregate object.
  1960. * @example
  1961. *
  1962. * _.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); });
  1963. * // => { '4': [4.2], '6': [6.1, 6.4] }
  1964. *
  1965. * _.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
  1966. * // => { '4': [4.2], '6': [6.1, 6.4] }
  1967. *
  1968. * _.groupBy(['one', 'two', 'three'], 'length');
  1969. * // => { '3': ['one', 'two'], '5': ['three'] }
  1970. */
  1971. var groupBy = createIterator(baseIteratorOptions, countByIteratorOptions, {
  1972. 'inLoop':
  1973. 'prop = callback(value, index, collection);\n' +
  1974. '(hasOwnProperty.call(result, prop) ? result[prop] : result[prop] = []).push(value)'
  1975. });
  1976. /**
  1977. * Invokes the method named by `methodName` on each element in the `collection`.
  1978. * Additional arguments will be passed to each invoked method. If `methodName`
  1979. * is a function it will be invoked for, and `this` bound to, each element
  1980. * in the `collection`.
  1981. *
  1982. * @static
  1983. * @memberOf _
  1984. * @category Collections
  1985. * @param {Array|Object|String} collection The collection to iterate over.
  1986. * @param {Function|String} methodName The name of the method to invoke or
  1987. * the function invoked per iteration.
  1988. * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the method with.
  1989. * @returns {Array} Returns a new array of values returned from each invoked method.
  1990. * @example
  1991. *
  1992. * _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
  1993. * // => [[1, 5, 7], [1, 2, 3]]
  1994. *
  1995. * _.invoke([123, 456], String.prototype.split, '');
  1996. * // => [['1', '2', '3'], ['4', '5', '6']]
  1997. */
  1998. var invoke = createIterator(mapIteratorOptions, {
  1999. 'args': 'collection, methodName',
  2000. 'top':
  2001. 'var args = slice.call(arguments, 2),\n' +
  2002. ' isFunc = typeof methodName == \'function\'',
  2003. 'inLoop': {
  2004. 'array':
  2005. 'result[index] = (isFunc ? methodName : value[methodName]).apply(value, args)',
  2006. 'object':
  2007. 'result' + (isKeysFast ? '[ownIndex] = ' : '.push') +
  2008. '((isFunc ? methodName : value[methodName]).apply(value, args))'
  2009. }
  2010. });
  2011. /**
  2012. * Creates a new array of values by running each element in the `collection`
  2013. * through a `callback`. The `callback` is bound to `thisArg` and invoked with
  2014. * 3 arguments; (value, index|key, collection).
  2015. *
  2016. * @static
  2017. * @memberOf _
  2018. * @alias collect
  2019. * @category Collections
  2020. * @param {Array|Object|String} collection The collection to iterate over.
  2021. * @param {Function} [callback=identity] The function called per iteration.
  2022. * @param {Mixed} [thisArg] The `this` binding for the callback.
  2023. * @returns {Array} Returns a new array of elements returned by the callback.
  2024. * @example
  2025. *
  2026. * _.map([1, 2, 3], function(num) { return num * 3; });
  2027. * // => [3, 6, 9]
  2028. *
  2029. * _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
  2030. * // => [3, 6, 9] (order is not guaranteed)
  2031. */
  2032. var map = createIterator(baseIteratorOptions, mapIteratorOptions);
  2033. /**
  2034. * Retrieves the value of a specified property from all elements in
  2035. * the `collection`.
  2036. *
  2037. * @static
  2038. * @memberOf _
  2039. * @category Collections
  2040. * @param {Array|Object|String} collection The collection to iterate over.
  2041. * @param {String} property The property to pluck.
  2042. * @returns {Array} Returns a new array of property values.
  2043. * @example
  2044. *
  2045. * var stooges = [
  2046. * { 'name': 'moe', 'age': 40 },
  2047. * { 'name': 'larry', 'age': 50 },
  2048. * { 'name': 'curly', 'age': 60 }
  2049. * ];
  2050. *
  2051. * _.pluck(stooges, 'name');
  2052. * // => ['moe', 'larry', 'curly']
  2053. */
  2054. var pluck = createIterator(mapIteratorOptions, {
  2055. 'args': 'collection, property',
  2056. 'inLoop': {
  2057. 'array': 'result[index] = value[property]',
  2058. 'object': 'result' + (isKeysFast ? '[ownIndex] = ' : '.push') + '(value[property])'
  2059. }
  2060. });
  2061. /**
  2062. * Boils down a `collection` to a single value. The initial state of the
  2063. * reduction is `accumulator` and each successive step of it should be returned
  2064. * by the `callback`. The `callback` is bound to `thisArg` and invoked with 4
  2065. * arguments; for arrays they are (accumulator, value, index|key, collection).
  2066. *
  2067. * @static
  2068. * @memberOf _
  2069. * @alias foldl, inject
  2070. * @category Collections
  2071. * @param {Array|Object|String} collection The collection to iterate over.
  2072. * @param {Function} callback The function called per iteration.
  2073. * @param {Mixed} [accumulator] Initial value of the accumulator.
  2074. * @param {Mixed} [thisArg] The `this` binding for the callback.
  2075. * @returns {Mixed} Returns the accumulated value.
  2076. * @example
  2077. *
  2078. * var sum = _.reduce([1, 2, 3], function(memo, num) { return memo + num; });
  2079. * // => 6
  2080. */
  2081. var reduce = createIterator({
  2082. 'args': 'collection, callback, accumulator, thisArg',
  2083. 'init': 'accumulator',
  2084. 'top':
  2085. 'var noaccum = arguments.length < 3;\n' +
  2086. 'if (thisArg) callback = iteratorBind(callback, thisArg)',
  2087. 'beforeLoop': {
  2088. 'array': 'if (noaccum) result = iteratee[++index]'
  2089. },
  2090. 'inLoop': {
  2091. 'array':
  2092. 'result = callback(result, value, index, collection)',
  2093. 'object':
  2094. 'result = noaccum\n' +
  2095. ' ? (noaccum = false, value)\n' +
  2096. ' : callback(result, value, index, collection)'
  2097. }
  2098. });
  2099. /**
  2100. * The right-associative version of `_.reduce`.
  2101. *
  2102. * @static
  2103. * @memberOf _
  2104. * @alias foldr
  2105. * @category Collections
  2106. * @param {Array|Object|String} collection The collection to iterate over.
  2107. * @param {Function} callback The function called per iteration.
  2108. * @param {Mixed} [accumulator] Initial value of the accumulator.
  2109. * @param {Mixed} [thisArg] The `this` binding for the callback.
  2110. * @returns {Mixed} Returns the accumulated value.
  2111. * @example
  2112. *
  2113. * var list = [[0, 1], [2, 3], [4, 5]];
  2114. * var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
  2115. * // => [4, 5, 2, 3, 0, 1]
  2116. */
  2117. function reduceRight(collection, callback, accumulator, thisArg) {
  2118. if (!collection) {
  2119. return accumulator;
  2120. }
  2121. var length = collection.length,
  2122. noaccum = arguments.length < 3;
  2123. if(thisArg) {
  2124. callback = iteratorBind(callback, thisArg);
  2125. }
  2126. // Opera 10.53-10.60 JITted `length >>> 0` returns the wrong value for negative numbers
  2127. if (length > -1 && length === length >>> 0) {
  2128. var iteratee = noCharByIndex && toString.call(collection) == stringClass
  2129. ? collection.split('')
  2130. : collection;
  2131. if (length && noaccum) {
  2132. accumulator = iteratee[--length];
  2133. }
  2134. while (length--) {
  2135. accumulator = callback(accumulator, iteratee[length], length, collection);
  2136. }
  2137. return accumulator;
  2138. }
  2139. var prop,
  2140. props = keys(collection);
  2141. length = props.length;
  2142. if (length && noaccum) {
  2143. accumulator = collection[props[--length]];
  2144. }
  2145. while (length--) {
  2146. prop = props[length];
  2147. accumulator = callback(accumulator, collection[prop], prop, collection);
  2148. }
  2149. return accumulator;
  2150. }
  2151. /**
  2152. * The opposite of `_.filter`, this method returns the values of a
  2153. * `collection` that `callback` does **not** return truthy for.
  2154. *
  2155. * @static
  2156. * @memberOf _
  2157. * @category Collections
  2158. * @param {Array|Object|String} collection The collection to iterate over.
  2159. * @param {Function} [callback=identity] The function called per iteration.
  2160. * @param {Mixed} [thisArg] The `this` binding for the callback.
  2161. * @returns {Array} Returns a new array of elements that did **not** pass the callback check.
  2162. * @example
  2163. *
  2164. * var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  2165. * // => [1, 3, 5]
  2166. */
  2167. var reject = createIterator(baseIteratorOptions, filterIteratorOptions, {
  2168. 'inLoop': '!' + filterIteratorOptions.inLoop
  2169. });
  2170. /**
  2171. * Gets the size of the `collection` by returning `collection.length` for arrays
  2172. * and array-like objects or the number of own enumerable properties for objects.
  2173. *
  2174. * @static
  2175. * @memberOf _
  2176. * @category Collections
  2177. * @param {Array|Object|String} collection The collection to inspect.
  2178. * @returns {Number} Returns `collection.length` or number of own enumerable properties.
  2179. * @example
  2180. *
  2181. * _.size([1, 2]);
  2182. * // => 2
  2183. *
  2184. * _.size({ 'one': 1, 'two': 2, 'three': 3 });
  2185. * // => 3
  2186. *
  2187. * _.size('curly');
  2188. * // => 5
  2189. */
  2190. function size(collection) {
  2191. if (!collection) {
  2192. return 0;
  2193. }
  2194. var length = collection.length;
  2195. return length > -1 && length === length >>> 0 ? length : keys(collection).length;
  2196. }
  2197. /**
  2198. * Checks if the `callback` returns a truthy value for **any** element of a
  2199. * `collection`. The function returns as soon as it finds passing value, and
  2200. * does not iterate over the entire `collection`. The `callback` is bound to
  2201. * `thisArg` and invoked with 3 arguments; (value, index|key, collection).
  2202. *
  2203. * @static
  2204. * @memberOf _
  2205. * @alias any
  2206. * @category Collections
  2207. * @param {Array|Object|String} collection The collection to iterate over.
  2208. * @param {Function} [callback=identity] The function called per iteration.
  2209. * @param {Mixed} [thisArg] The `this` binding for the callback.
  2210. * @returns {Boolean} Returns `true` if any element passes the callback check, else `false`.
  2211. * @example
  2212. *
  2213. * _.some([null, 0, 'yes', false]);
  2214. * // => true
  2215. */
  2216. var some = createIterator(baseIteratorOptions, everyIteratorOptions, {
  2217. 'init': 'false',
  2218. 'inLoop': everyIteratorOptions.inLoop.replace('!', '')
  2219. });
  2220. /**
  2221. * Creates a new array, stable sorted in ascending order by the results of
  2222. * running each element of `collection` through a `callback`. The `callback`
  2223. * is bound to `thisArg` and invoked with 3 arguments; (value, index|key, collection).
  2224. * The `callback` argument may also be the name of a property to sort by (e.g. 'length').
  2225. *
  2226. * @static
  2227. * @memberOf _
  2228. * @category Collections
  2229. * @param {Array|Object|String} collection The collection to iterate over.
  2230. * @param {Function|String} callback|property The function called per iteration
  2231. * or property name to sort by.
  2232. * @param {Mixed} [thisArg] The `this` binding for the callback.
  2233. * @returns {Array} Returns a new array of sorted elements.
  2234. * @example
  2235. *
  2236. * _.sortBy([1, 2, 3], function(num) { return Math.sin(num); });
  2237. * // => [3, 1, 2]
  2238. *
  2239. * _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
  2240. * // => [3, 1, 2]
  2241. *
  2242. * _.sortBy(['larry', 'brendan', 'moe'], 'length');
  2243. * // => ['moe', 'larry', 'brendan']
  2244. */
  2245. var sortBy = createIterator(baseIteratorOptions, countByIteratorOptions, mapIteratorOptions, {
  2246. 'inLoop': {
  2247. 'array':
  2248. 'result[index] = {\n' +
  2249. ' criteria: callback(value, index, collection),\n' +
  2250. ' index: index,\n' +
  2251. ' value: value\n' +
  2252. '}',
  2253. 'object':
  2254. 'result' + (isKeysFast ? '[ownIndex] = ' : '.push') + '({\n' +
  2255. ' criteria: callback(value, index, collection),\n' +
  2256. ' index: index,\n' +
  2257. ' value: value\n' +
  2258. '})'
  2259. },
  2260. 'bottom':
  2261. 'result.sort(compareAscending);\n' +
  2262. 'length = result.length;\n' +
  2263. 'while (length--) {\n' +
  2264. ' result[length] = result[length].value\n' +
  2265. '}'
  2266. });
  2267. /**
  2268. * Converts the `collection`, to an array. Useful for converting the
  2269. * `arguments` object.
  2270. *
  2271. * @static
  2272. * @memberOf _
  2273. * @category Collections
  2274. * @param {Array|Object|String} collection The collection to convert.
  2275. * @returns {Array} Returns the new converted array.
  2276. * @example
  2277. *
  2278. * (function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
  2279. * // => [2, 3, 4]
  2280. */
  2281. function toArray(collection) {
  2282. if (!collection) {
  2283. return [];
  2284. }
  2285. if (collection.toArray && isFunction(collection.toArray)) {
  2286. return collection.toArray();
  2287. }
  2288. var length = collection.length;
  2289. if (length > -1 && length === length >>> 0) {
  2290. return (noArraySliceOnStrings ? toString.call(collection) == stringClass : typeof collection == 'string')
  2291. ? collection.split('')
  2292. : slice.call(collection);
  2293. }
  2294. return values(collection);
  2295. }
  2296. /**
  2297. * Examines each element in a `collection`, returning an array of all elements
  2298. * that contain the given `properties`.
  2299. *
  2300. * @static
  2301. * @memberOf _
  2302. * @category Collections
  2303. * @param {Array|Object|String} collection The collection to iterate over.
  2304. * @param {Object} properties The object of properties/values to filter by.
  2305. * @returns {Array} Returns a new array of elements that contain the given `properties`.
  2306. * @example
  2307. *
  2308. * var stooges = [
  2309. * { 'name': 'moe', 'age': 40 },
  2310. * { 'name': 'larry', 'age': 50 },
  2311. * { 'name': 'curly', 'age': 60 }
  2312. * ];
  2313. *
  2314. * _.where(stooges, { 'age': 40 });
  2315. * // => [{ 'name': 'moe', 'age': 40 }]
  2316. */
  2317. var where = createIterator(filterIteratorOptions, {
  2318. 'args': 'collection, properties',
  2319. 'top':
  2320. 'var props = [];\n' +
  2321. 'forIn(properties, function(value, prop) { props.push(prop) });\n' +
  2322. 'var propsLength = props.length',
  2323. 'inLoop':
  2324. 'for (var prop, pass = true, propIndex = 0; propIndex < propsLength; propIndex++) {\n' +
  2325. ' prop = props[propIndex];\n' +
  2326. ' if (!(pass = value[prop] === properties[prop])) break\n' +
  2327. '}\n' +
  2328. 'pass && result.push(value)'
  2329. });
  2330. /*--------------------------------------------------------------------------*/
  2331. /**
  2332. * Creates a new array with all falsey values of `array` removed. The values
  2333. * `false`, `null`, `0`, `""`, `undefined` and `NaN` are all falsey.
  2334. *
  2335. * @static
  2336. * @memberOf _
  2337. * @category Arrays
  2338. * @param {Array} array The array to compact.
  2339. * @returns {Array} Returns a new filtered array.
  2340. * @example
  2341. *
  2342. * _.compact([0, 1, false, 2, '', 3]);
  2343. * // => [1, 2, 3]
  2344. */
  2345. function compact(array) {
  2346. var result = [];
  2347. if (!array) {
  2348. return result;
  2349. }
  2350. var index = -1,
  2351. length = array.length;
  2352. while (++index < length) {
  2353. if (array[index]) {
  2354. result.push(array[index]);
  2355. }
  2356. }
  2357. return result;
  2358. }
  2359. /**
  2360. * Creates a new array of `array` elements not present in the other arrays
  2361. * using strict equality for comparisons, i.e. `===`.
  2362. *
  2363. * @static
  2364. * @memberOf _
  2365. * @category Arrays
  2366. * @param {Array} array The array to process.
  2367. * @param {Array} [array1, array2, ...] Arrays to check.
  2368. * @returns {Array} Returns a new array of `array` elements not present in the
  2369. * other arrays.
  2370. * @example
  2371. *
  2372. * _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
  2373. * // => [1, 3, 4]
  2374. */
  2375. function difference(array) {
  2376. var result = [];
  2377. if (!array) {
  2378. return result;
  2379. }
  2380. var index = -1,
  2381. length = array.length,
  2382. flattened = concat.apply(result, arguments),
  2383. contains = cachedContains(flattened, length);
  2384. while (++index < length) {
  2385. if (!contains(array[index])) {
  2386. result.push(array[index]);
  2387. }
  2388. }
  2389. return result;
  2390. }
  2391. /**
  2392. * Gets the first element of the `array`. Pass `n` to return the first `n`
  2393. * elements of the `array`.
  2394. *
  2395. * @static
  2396. * @memberOf _
  2397. * @alias head, take
  2398. * @category Arrays
  2399. * @param {Array} array The array to query.
  2400. * @param {Number} [n] The number of elements to return.
  2401. * @param {Object} [guard] Internally used to allow this method to work with
  2402. * others like `_.map` without using their callback `index` argument for `n`.
  2403. * @returns {Mixed} Returns the first element or an array of the first `n`
  2404. * elements of `array`.
  2405. * @example
  2406. *
  2407. * _.first([5, 4, 3, 2, 1]);
  2408. * // => 5
  2409. */
  2410. function first(array, n, guard) {
  2411. if (array) {
  2412. return (n == null || guard) ? array[0] : slice.call(array, 0, n);
  2413. }
  2414. }
  2415. /**
  2416. * Flattens a nested array (the nesting can be to any depth). If `shallow` is
  2417. * truthy, `array` will only be flattened a single level.
  2418. *
  2419. * @static
  2420. * @memberOf _
  2421. * @category Arrays
  2422. * @param {Array} array The array to compact.
  2423. * @param {Boolean} shallow A flag to indicate only flattening a single level.
  2424. * @returns {Array} Returns a new flattened array.
  2425. * @example
  2426. *
  2427. * _.flatten([1, [2], [3, [[4]]]]);
  2428. * // => [1, 2, 3, 4];
  2429. *
  2430. * _.flatten([1, [2], [3, [[4]]]], true);
  2431. * // => [1, 2, 3, [[4]]];
  2432. */
  2433. function flatten(array, shallow) {
  2434. var result = [];
  2435. if (!array) {
  2436. return result;
  2437. }
  2438. var value,
  2439. index = -1,
  2440. length = array.length;
  2441. while (++index < length) {
  2442. value = array[index];
  2443. if (isArray(value)) {
  2444. push.apply(result, shallow ? value : flatten(value));
  2445. } else {
  2446. result.push(value);
  2447. }
  2448. }
  2449. return result;
  2450. }
  2451. /**
  2452. * Gets the index at which the first occurrence of `value` is found using
  2453. * strict equality for comparisons, i.e. `===`. If the `array` is already
  2454. * sorted, passing `true` for `isSorted` will run a faster binary search.
  2455. *
  2456. * @static
  2457. * @memberOf _
  2458. * @category Arrays
  2459. * @param {Array} array The array to search.
  2460. * @param {Mixed} value The value to search for.
  2461. * @param {Boolean|Number} [fromIndex=0] The index to start searching from or
  2462. * `true` to perform a binary search on a sorted `array`.
  2463. * @returns {Number} Returns the index of the matched value or `-1`.
  2464. * @example
  2465. *
  2466. * _.indexOf([1, 2, 3, 1, 2, 3], 2);
  2467. * // => 1
  2468. *
  2469. * _.indexOf([1, 2, 3, 1, 2, 3], 2, 3);
  2470. * // => 4
  2471. *
  2472. * _.indexOf([1, 1, 2, 2, 3, 3], 2, true);
  2473. * // => 2
  2474. */
  2475. function indexOf(array, value, fromIndex) {
  2476. if (!array) {
  2477. return -1;
  2478. }
  2479. var index = -1,
  2480. length = array.length;
  2481. if (fromIndex) {
  2482. if (typeof fromIndex == 'number') {
  2483. index = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) - 1;
  2484. } else {
  2485. index = sortedIndex(array, value);
  2486. return array[index] === value ? index : -1;
  2487. }
  2488. }
  2489. while (++index < length) {
  2490. if (array[index] === value) {
  2491. return index;
  2492. }
  2493. }
  2494. return -1;
  2495. }
  2496. /**
  2497. * Gets all but the last element of `array`. Pass `n` to exclude the last `n`
  2498. * elements from the result.
  2499. *
  2500. * @static
  2501. * @memberOf _
  2502. * @category Arrays
  2503. * @param {Array} array The array to query.
  2504. * @param {Number} [n] The number of elements to return.
  2505. * @param {Object} [guard] Internally used to allow this method to work with
  2506. * others like `_.map` without using their callback `index` argument for `n`.
  2507. * @returns {Array} Returns all but the last element or `n` elements of `array`.
  2508. * @example
  2509. *
  2510. * _.initial([3, 2, 1]);
  2511. * // => [3, 2]
  2512. */
  2513. function initial(array, n, guard) {
  2514. if (!array) {
  2515. return [];
  2516. }
  2517. return slice.call(array, 0, -((n == null || guard) ? 1 : n));
  2518. }
  2519. /**
  2520. * Computes the intersection of all the passed-in arrays using strict equality
  2521. * for comparisons, i.e. `===`.
  2522. *
  2523. * @static
  2524. * @memberOf _
  2525. * @category Arrays
  2526. * @param {Array} [array1, array2, ...] Arrays to process.
  2527. * @returns {Array} Returns a new array of unique elements, in order, that are
  2528. * present in **all** of the arrays.
  2529. * @example
  2530. *
  2531. * _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  2532. * // => [1, 2]
  2533. */
  2534. function intersection(array) {
  2535. var result = [];
  2536. if (!array) {
  2537. return result;
  2538. }
  2539. var value,
  2540. argsLength = arguments.length,
  2541. cache = [],
  2542. index = -1,
  2543. length = array.length;
  2544. array: while (++index < length) {
  2545. value = array[index];
  2546. if (indexOf(result, value) < 0) {
  2547. for (var argsIndex = 1; argsIndex < argsLength; argsIndex++) {
  2548. if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(arguments[argsIndex])))(value)) {
  2549. continue array;
  2550. }
  2551. }
  2552. result.push(value);
  2553. }
  2554. }
  2555. return result;
  2556. }
  2557. /**
  2558. * Gets the last element of the `array`. Pass `n` to return the lasy `n`
  2559. * elementsvof the `array`.
  2560. *
  2561. * @static
  2562. * @memberOf _
  2563. * @category Arrays
  2564. * @param {Array} array The array to query.
  2565. * @param {Number} [n] The number of elements to return.
  2566. * @param {Object} [guard] Internally used to allow this method to work with
  2567. * others like `_.map` without using their callback `index` argument for `n`.
  2568. * @returns {Mixed} Returns the last element or an array of the last `n`
  2569. * elements of `array`.
  2570. * @example
  2571. *
  2572. * _.last([3, 2, 1]);
  2573. * // => 1
  2574. */
  2575. function last(array, n, guard) {
  2576. if (array) {
  2577. var length = array.length;
  2578. return (n == null || guard) ? array[length - 1] : slice.call(array, -n || length);
  2579. }
  2580. }
  2581. /**
  2582. * Gets the index at which the last occurrence of `value` is found using
  2583. * strict equality for comparisons, i.e. `===`.
  2584. *
  2585. * @static
  2586. * @memberOf _
  2587. * @category Arrays
  2588. * @param {Array} array The array to search.
  2589. * @param {Mixed} value The value to search for.
  2590. * @param {Number} [fromIndex=array.length-1] The index to start searching from.
  2591. * @returns {Number} Returns the index of the matched value or `-1`.
  2592. * @example
  2593. *
  2594. * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  2595. * // => 4
  2596. *
  2597. * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);
  2598. * // => 1
  2599. */
  2600. function lastIndexOf(array, value, fromIndex) {
  2601. if (!array) {
  2602. return -1;
  2603. }
  2604. var index = array.length;
  2605. if (fromIndex && typeof fromIndex == 'number') {
  2606. index = (fromIndex < 0 ? nativeMax(0, index + fromIndex) : nativeMin(fromIndex, index - 1)) + 1;
  2607. }
  2608. while (index--) {
  2609. if (array[index] === value) {
  2610. return index;
  2611. }
  2612. }
  2613. return -1;
  2614. }
  2615. /**
  2616. * Retrieves the maximum value of an `array`. If `callback` is passed,
  2617. * it will be executed for each value in the `array` to generate the
  2618. * criterion by which the value is ranked. The `callback` is bound to
  2619. * `thisArg` and invoked with 3 arguments; (value, index, array).
  2620. *
  2621. * @static
  2622. * @memberOf _
  2623. * @category Arrays
  2624. * @param {Array} array The array to iterate over.
  2625. * @param {Function} [callback] The function called per iteration.
  2626. * @param {Mixed} [thisArg] The `this` binding for the callback.
  2627. * @returns {Mixed} Returns the maximum value.
  2628. * @example
  2629. *
  2630. * var stooges = [
  2631. * { 'name': 'moe', 'age': 40 },
  2632. * { 'name': 'larry', 'age': 50 },
  2633. * { 'name': 'curly', 'age': 60 }
  2634. * ];
  2635. *
  2636. * _.max(stooges, function(stooge) { return stooge.age; });
  2637. * // => { 'name': 'curly', 'age': 60 };
  2638. */
  2639. function max(array, callback, thisArg) {
  2640. var computed = -Infinity,
  2641. result = computed;
  2642. if (!array) {
  2643. return result;
  2644. }
  2645. var current,
  2646. index = -1,
  2647. length = array.length;
  2648. if (!callback) {
  2649. while (++index < length) {
  2650. if (array[index] > result) {
  2651. result = array[index];
  2652. }
  2653. }
  2654. return result;
  2655. }
  2656. if (thisArg) {
  2657. callback = iteratorBind(callback, thisArg);
  2658. }
  2659. while (++index < length) {
  2660. current = callback(array[index], index, array);
  2661. if (current > computed) {
  2662. computed = current;
  2663. result = array[index];
  2664. }
  2665. }
  2666. return result;
  2667. }
  2668. /**
  2669. * Retrieves the minimum value of an `array`. If `callback` is passed,
  2670. * it will be executed for each value in the `array` to generate the
  2671. * criterion by which the value is ranked. The `callback` is bound to `thisArg`
  2672. * and invoked with 3 arguments; (value, index, array).
  2673. *
  2674. * @static
  2675. * @memberOf _
  2676. * @category Arrays
  2677. * @param {Array} array The array to iterate over.
  2678. * @param {Function} [callback] The function called per iteration.
  2679. * @param {Mixed} [thisArg] The `this` binding for the callback.
  2680. * @returns {Mixed} Returns the minimum value.
  2681. * @example
  2682. *
  2683. * _.min([10, 5, 100, 2, 1000]);
  2684. * // => 2
  2685. */
  2686. function min(array, callback, thisArg) {
  2687. var computed = Infinity,
  2688. result = computed;
  2689. if (!array) {
  2690. return result;
  2691. }
  2692. var current,
  2693. index = -1,
  2694. length = array.length;
  2695. if (!callback) {
  2696. while (++index < length) {
  2697. if (array[index] < result) {
  2698. result = array[index];
  2699. }
  2700. }
  2701. return result;
  2702. }
  2703. if (thisArg) {
  2704. callback = iteratorBind(callback, thisArg);
  2705. }
  2706. while (++index < length) {
  2707. current = callback(array[index], index, array);
  2708. if (current < computed) {
  2709. computed = current;
  2710. result = array[index];
  2711. }
  2712. }
  2713. return result;
  2714. }
  2715. /**
  2716. * Creates an object composed from arrays of `keys` and `values`. Pass either
  2717. * a single two dimensional array, i.e. `[[key1, value1], [key2, value2]]`, or
  2718. * two arrays, one of `keys` and one of corresponding `values`.
  2719. *
  2720. * @static
  2721. * @memberOf _
  2722. * @category Arrays
  2723. * @param {Array} keys The array of keys.
  2724. * @param {Array} [values=[]] The array of values.
  2725. * @returns {Object} Returns an object composed of the given keys and
  2726. * corresponding values.
  2727. * @example
  2728. *
  2729. * _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
  2730. * // => { 'moe': 30, 'larry': 40, 'curly': 50 }
  2731. */
  2732. function object(keys, values) {
  2733. if (!keys) {
  2734. return {};
  2735. }
  2736. var index = -1,
  2737. length = keys.length,
  2738. result = {};
  2739. while (++index < length) {
  2740. if (values) {
  2741. result[keys[index]] = values[index];
  2742. } else {
  2743. result[keys[index][0]] = keys[index][1];
  2744. }
  2745. }
  2746. return result;
  2747. }
  2748. /**
  2749. * Creates an array of numbers (positive and/or negative) progressing from
  2750. * `start` up to but not including `stop`. This method is a port of Python's
  2751. * `range()` function. See http://docs.python.org/library/functions.html#range.
  2752. *
  2753. * @static
  2754. * @memberOf _
  2755. * @category Arrays
  2756. * @param {Number} [start=0] The start of the range.
  2757. * @param {Number} end The end of the range.
  2758. * @param {Number} [step=1] The value to increment or descrement by.
  2759. * @returns {Array} Returns a new range array.
  2760. * @example
  2761. *
  2762. * _.range(10);
  2763. * // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  2764. *
  2765. * _.range(1, 11);
  2766. * // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  2767. *
  2768. * _.range(0, 30, 5);
  2769. * // => [0, 5, 10, 15, 20, 25]
  2770. *
  2771. * _.range(0, -10, -1);
  2772. * // => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  2773. *
  2774. * _.range(0);
  2775. * // => []
  2776. */
  2777. function range(start, end, step) {
  2778. start = +start || 0;
  2779. step = +step || 1;
  2780. if (end == null) {
  2781. end = start;
  2782. start = 0;
  2783. }
  2784. // use `Array(length)` so V8 will avoid the slower "dictionary" mode
  2785. // http://www.youtube.com/watch?v=XAqIpGU8ZZk#t=16m27s
  2786. var index = -1,
  2787. length = nativeMax(0, Math.ceil((end - start) / step)),
  2788. result = Array(length);
  2789. while (++index < length) {
  2790. result[index] = start;
  2791. start += step;
  2792. }
  2793. return result;
  2794. }
  2795. /**
  2796. * The opposite of `_.initial`, this method gets all but the first value of
  2797. * `array`. Pass `n` to exclude the first `n` values from the result.
  2798. *
  2799. * @static
  2800. * @memberOf _
  2801. * @alias drop, tail
  2802. * @category Arrays
  2803. * @param {Array} array The array to query.
  2804. * @param {Number} [n] The number of elements to return.
  2805. * @param {Object} [guard] Internally used to allow this method to work with
  2806. * others like `_.map` without using their callback `index` argument for `n`.
  2807. * @returns {Array} Returns all but the first value or `n` values of `array`.
  2808. * @example
  2809. *
  2810. * _.rest([3, 2, 1]);
  2811. * // => [2, 1]
  2812. */
  2813. function rest(array, n, guard) {
  2814. if (!array) {
  2815. return [];
  2816. }
  2817. return slice.call(array, (n == null || guard) ? 1 : n);
  2818. }
  2819. /**
  2820. * Creates a new array of shuffled `array` values, using a version of the
  2821. * Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle.
  2822. *
  2823. * @static
  2824. * @memberOf _
  2825. * @category Arrays
  2826. * @param {Array} array The array to shuffle.
  2827. * @returns {Array} Returns a new shuffled array.
  2828. * @example
  2829. *
  2830. * _.shuffle([1, 2, 3, 4, 5, 6]);
  2831. * // => [4, 1, 6, 3, 5, 2]
  2832. */
  2833. function shuffle(array) {
  2834. if (!array) {
  2835. return [];
  2836. }
  2837. var rand,
  2838. index = -1,
  2839. length = array.length,
  2840. result = Array(length);
  2841. while (++index < length) {
  2842. rand = nativeFloor(nativeRandom() * (index + 1));
  2843. result[index] = result[rand];
  2844. result[rand] = array[index];
  2845. }
  2846. return result;
  2847. }
  2848. /**
  2849. * Uses a binary search to determine the smallest index at which the `value`
  2850. * should be inserted into `array` in order to maintain the sort order of the
  2851. * sorted `array`. If `callback` is passed, it will be executed for `value` and
  2852. * each element in `array` to compute their sort ranking. The `callback` is
  2853. * bound to `thisArg` and invoked with 1 argument; (value).
  2854. *
  2855. * @static
  2856. * @memberOf _
  2857. * @category Arrays
  2858. * @param {Array} array The array to iterate over.
  2859. * @param {Mixed} value The value to evaluate.
  2860. * @param {Function} [callback=identity] The function called per iteration.
  2861. * @param {Mixed} [thisArg] The `this` binding for the callback.
  2862. * @returns {Number} Returns the index at which the value should be inserted
  2863. * into `array`.
  2864. * @example
  2865. *
  2866. * _.sortedIndex([20, 30, 40], 35);
  2867. * // => 2
  2868. *
  2869. * var dict = {
  2870. * 'wordToNumber': { 'twenty': 20, 'thirty': 30, 'thirty-five': 35, 'fourty': 40 }
  2871. * };
  2872. *
  2873. * _.sortedIndex(['twenty', 'thirty', 'fourty'], 'thirty-five', function(word) {
  2874. * return dict.wordToNumber[word];
  2875. * });
  2876. * // => 2
  2877. *
  2878. * _.sortedIndex(['twenty', 'thirty', 'fourty'], 'thirty-five', function(word) {
  2879. * return this.wordToNumber[word];
  2880. * }, dict);
  2881. * // => 2
  2882. */
  2883. function sortedIndex(array, value, callback, thisArg) {
  2884. if (!array) {
  2885. return 0;
  2886. }
  2887. var mid,
  2888. low = 0,
  2889. high = array.length;
  2890. if (callback) {
  2891. if (thisArg) {
  2892. callback = bind(callback, thisArg);
  2893. }
  2894. value = callback(value);
  2895. while (low < high) {
  2896. mid = (low + high) >>> 1;
  2897. callback(array[mid]) < value ? low = mid + 1 : high = mid;
  2898. }
  2899. } else {
  2900. while (low < high) {
  2901. mid = (low + high) >>> 1;
  2902. array[mid] < value ? low = mid + 1 : high = mid;
  2903. }
  2904. }
  2905. return low;
  2906. }
  2907. /**
  2908. * Computes the union of the passed-in arrays using strict equality for
  2909. * comparisons, i.e. `===`.
  2910. *
  2911. * @static
  2912. * @memberOf _
  2913. * @category Arrays
  2914. * @param {Array} [array1, array2, ...] Arrays to process.
  2915. * @returns {Array} Returns a new array of unique values, in order, that are
  2916. * present in one or more of the arrays.
  2917. * @example
  2918. *
  2919. * _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
  2920. * // => [1, 2, 3, 101, 10]
  2921. */
  2922. function union() {
  2923. var index = -1,
  2924. result = [],
  2925. flattened = concat.apply(result, arguments),
  2926. length = flattened.length;
  2927. while (++index < length) {
  2928. if (indexOf(result, flattened[index]) < 0) {
  2929. result.push(flattened[index]);
  2930. }
  2931. }
  2932. return result;
  2933. }
  2934. /**
  2935. * Creates a duplicate-value-free version of the `array` using strict equality
  2936. * for comparisons, i.e. `===`. If the `array` is already sorted, passing `true`
  2937. * for `isSorted` will run a faster algorithm. If `callback` is passed, each
  2938. * element of `array` is passed through a callback` before uniqueness is computed.
  2939. * The `callback` is bound to `thisArg` and invoked with 3 arguments; (value, index, array).
  2940. *
  2941. * @static
  2942. * @memberOf _
  2943. * @alias unique
  2944. * @category Arrays
  2945. * @param {Array} array The array to process.
  2946. * @param {Boolean} [isSorted=false] A flag to indicate that the `array` is already sorted.
  2947. * @param {Function} [callback=identity] The function called per iteration.
  2948. * @param {Mixed} [thisArg] The `this` binding for the callback.
  2949. * @returns {Array} Returns a duplicate-value-free array.
  2950. * @example
  2951. *
  2952. * _.uniq([1, 2, 1, 3, 1]);
  2953. * // => [1, 2, 3]
  2954. *
  2955. * _.uniq([1, 1, 2, 2, 3], true);
  2956. * // => [1, 2, 3]
  2957. *
  2958. * _.uniq([1, 2, 1.5, 3, 2.5], function(num) { return Math.floor(num); });
  2959. * // => [1, 2, 3]
  2960. *
  2961. * _.uniq([1, 2, 1.5, 3, 2.5], function(num) { return this.floor(num); }, Math);
  2962. * // => [1, 2, 3]
  2963. */
  2964. function uniq(array, isSorted, callback, thisArg) {
  2965. var result = [];
  2966. if (!array) {
  2967. return result;
  2968. }
  2969. var computed,
  2970. index = -1,
  2971. length = array.length,
  2972. seen = [];
  2973. // juggle arguments
  2974. if (typeof isSorted == 'function') {
  2975. thisArg = callback;
  2976. callback = isSorted;
  2977. isSorted = false;
  2978. }
  2979. if (!callback) {
  2980. callback = identity;
  2981. } else if (thisArg) {
  2982. callback = iteratorBind(callback, thisArg);
  2983. }
  2984. while (++index < length) {
  2985. computed = callback(array[index], index, array);
  2986. if (isSorted
  2987. ? !index || seen[seen.length - 1] !== computed
  2988. : indexOf(seen, computed) < 0
  2989. ) {
  2990. seen.push(computed);
  2991. result.push(array[index]);
  2992. }
  2993. }
  2994. return result;
  2995. }
  2996. /**
  2997. * Creates a new array with all occurrences of the passed values removed using
  2998. * strict equality for comparisons, i.e. `===`.
  2999. *
  3000. * @static
  3001. * @memberOf _
  3002. * @category Arrays
  3003. * @param {Array} array The array to filter.
  3004. * @param {Mixed} [value1, value2, ...] Values to remove.
  3005. * @returns {Array} Returns a new filtered array.
  3006. * @example
  3007. *
  3008. * _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  3009. * // => [2, 3, 4]
  3010. */
  3011. function without(array) {
  3012. var result = [];
  3013. if (!array) {
  3014. return result;
  3015. }
  3016. var index = -1,
  3017. length = array.length,
  3018. contains = cachedContains(arguments, 1, 20);
  3019. while (++index < length) {
  3020. if (!contains(array[index])) {
  3021. result.push(array[index]);
  3022. }
  3023. }
  3024. return result;
  3025. }
  3026. /**
  3027. * Groups the elements of each array at their corresponding indexes. Useful for
  3028. * separate data sources that are coordinated through matching array indexes.
  3029. * For a matrix of nested arrays, `_.zip.apply(...)` can transpose the matrix
  3030. * in a similar fashion.
  3031. *
  3032. * @static
  3033. * @memberOf _
  3034. * @category Arrays
  3035. * @param {Array} [array1, array2, ...] Arrays to process.
  3036. * @returns {Array} Returns a new array of grouped elements.
  3037. * @example
  3038. *
  3039. * _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
  3040. * // => [['moe', 30, true], ['larry', 40, false], ['curly', 50, false]]
  3041. */
  3042. function zip(array) {
  3043. if (!array) {
  3044. return [];
  3045. }
  3046. var index = -1,
  3047. length = max(pluck(arguments, 'length')),
  3048. result = Array(length);
  3049. while (++index < length) {
  3050. result[index] = pluck(arguments, index);
  3051. }
  3052. return result;
  3053. }
  3054. /*--------------------------------------------------------------------------*/
  3055. /**
  3056. * Creates a new function that is restricted to executing only after it is
  3057. * called `n` times.
  3058. *
  3059. * @static
  3060. * @memberOf _
  3061. * @category Functions
  3062. * @param {Number} n The number of times the function must be called before
  3063. * it is executed.
  3064. * @param {Function} func The function to restrict.
  3065. * @returns {Function} Returns the new restricted function.
  3066. * @example
  3067. *
  3068. * var renderNotes = _.after(notes.length, render);
  3069. * _.forEach(notes, function(note) {
  3070. * note.asyncSave({ 'success': renderNotes });
  3071. * });
  3072. * // `renderNotes` is run once, after all notes have saved
  3073. */
  3074. function after(n, func) {
  3075. if (n < 1) {
  3076. return func();
  3077. }
  3078. return function() {
  3079. if (--n < 1) {
  3080. return func.apply(this, arguments);
  3081. }
  3082. };
  3083. }
  3084. /**
  3085. * Creates a new function that, when called, invokes `func` with the `this`
  3086. * binding of `thisArg` and prepends any additional `bind` arguments to those
  3087. * passed to the bound function. Lazy defined methods may be bound by passing
  3088. * the object they are bound to as `func` and the method name as `thisArg`.
  3089. *
  3090. * @static
  3091. * @memberOf _
  3092. * @category Functions
  3093. * @param {Function|Object} func The function to bind or the object the method belongs to.
  3094. * @param {Mixed} [thisArg] The `this` binding of `func` or the method name.
  3095. * @param {Mixed} [arg1, arg2, ...] Arguments to be partially applied.
  3096. * @returns {Function} Returns the new bound function.
  3097. * @example
  3098. *
  3099. * // basic bind
  3100. * var func = function(greeting) {
  3101. * return greeting + ' ' + this.name;
  3102. * };
  3103. *
  3104. * func = _.bind(func, { 'name': 'moe' }, 'hi');
  3105. * func();
  3106. * // => 'hi moe'
  3107. *
  3108. * // lazy bind
  3109. * var object = {
  3110. * 'name': 'moe',
  3111. * 'greet': function(greeting) {
  3112. * return greeting + ' ' + this.name;
  3113. * }
  3114. * };
  3115. *
  3116. * var func = _.bind(object, 'greet', 'hi');
  3117. * func();
  3118. * // => 'hi moe'
  3119. *
  3120. * object.greet = function(greeting) {
  3121. * return greeting + ', ' + this.name + '!';
  3122. * };
  3123. *
  3124. * func();
  3125. * // => 'hi, moe!'
  3126. */
  3127. function bind(func, thisArg) {
  3128. var methodName,
  3129. isFunc = isFunction(func);
  3130. // juggle arguments
  3131. if (!isFunc) {
  3132. methodName = thisArg;
  3133. thisArg = func;
  3134. }
  3135. // use `Function#bind` if it exists and is fast
  3136. // (in V8 `Function#bind` is slower except when partially applied)
  3137. else if (isBindFast || (nativeBind && arguments.length > 2)) {
  3138. return nativeBind.call.apply(nativeBind, arguments);
  3139. }
  3140. var partialArgs = slice.call(arguments, 2);
  3141. function bound() {
  3142. // `Function#bind` spec
  3143. // http://es5.github.com/#x15.3.4.5
  3144. var args = arguments,
  3145. thisBinding = thisArg;
  3146. if (!isFunc) {
  3147. func = thisArg[methodName];
  3148. }
  3149. if (partialArgs.length) {
  3150. args = args.length
  3151. ? partialArgs.concat(slice.call(args))
  3152. : partialArgs;
  3153. }
  3154. if (this instanceof bound) {
  3155. // get `func` instance if `bound` is invoked in a `new` expression
  3156. noop.prototype = func.prototype;
  3157. thisBinding = new noop;
  3158. // mimic the constructor's `return` behavior
  3159. // http://es5.github.com/#x13.2.2
  3160. var result = func.apply(thisBinding, args);
  3161. return result && objectTypes[typeof result]
  3162. ? result
  3163. : thisBinding
  3164. }
  3165. return func.apply(thisBinding, args);
  3166. }
  3167. return bound;
  3168. }
  3169. /**
  3170. * Binds methods on `object` to `object`, overwriting the existing method.
  3171. * If no method names are provided, all the function properties of `object`
  3172. * will be bound.
  3173. *
  3174. * @static
  3175. * @memberOf _
  3176. * @category Functions
  3177. * @param {Object} object The object to bind and assign the bound methods to.
  3178. * @param {String} [methodName1, methodName2, ...] Method names on the object to bind.
  3179. * @returns {Object} Returns `object`.
  3180. * @example
  3181. *
  3182. * var buttonView = {
  3183. * 'label': 'lodash',
  3184. * 'onClick': function() { alert('clicked: ' + this.label); }
  3185. * };
  3186. *
  3187. * _.bindAll(buttonView);
  3188. * jQuery('#lodash_button').on('click', buttonView.onClick);
  3189. * // => When the button is clicked, `this.label` will have the correct value
  3190. */
  3191. var bindAll = createIterator({
  3192. 'useHas': false,
  3193. 'useStrict': false,
  3194. 'args': 'object',
  3195. 'init': 'object',
  3196. 'top':
  3197. 'var funcs = arguments,\n' +
  3198. ' length = funcs.length;\n' +
  3199. 'if (length > 1) {\n' +
  3200. ' for (var index = 1; index < length; index++) {\n' +
  3201. ' result[funcs[index]] = bind(result[funcs[index]], result)\n' +
  3202. ' }\n' +
  3203. ' return result\n' +
  3204. '}',
  3205. 'inLoop':
  3206. 'if (isFunction(result[index])) {\n' +
  3207. ' result[index] = bind(result[index], result)\n' +
  3208. '}'
  3209. });
  3210. /**
  3211. * Creates a new function that is the composition of the passed functions,
  3212. * where each function consumes the return value of the function that follows.
  3213. * In math terms, composing the functions `f()`, `g()`, and `h()` produces `f(g(h()))`.
  3214. *
  3215. * @static
  3216. * @memberOf _
  3217. * @category Functions
  3218. * @param {Function} [func1, func2, ...] Functions to compose.
  3219. * @returns {Function} Returns the new composed function.
  3220. * @example
  3221. *
  3222. * var greet = function(name) { return 'hi: ' + name; };
  3223. * var exclaim = function(statement) { return statement + '!'; };
  3224. * var welcome = _.compose(exclaim, greet);
  3225. * welcome('moe');
  3226. * // => 'hi: moe!'
  3227. */
  3228. function compose() {
  3229. var funcs = arguments;
  3230. return function() {
  3231. var args = arguments,
  3232. length = funcs.length;
  3233. while (length--) {
  3234. args = [funcs[length].apply(this, args)];
  3235. }
  3236. return args[0];
  3237. };
  3238. }
  3239. /**
  3240. * Creates a new function that will delay the execution of `func` until after
  3241. * `wait` milliseconds have elapsed since the last time it was invoked. Pass
  3242. * `true` for `immediate` to cause debounce to invoke `func` on the leading,
  3243. * instead of the trailing, edge of the `wait` timeout. Subsequent calls to
  3244. * the debounced function will return the result of the last `func` call.
  3245. *
  3246. * @static
  3247. * @memberOf _
  3248. * @category Functions
  3249. * @param {Function} func The function to debounce.
  3250. * @param {Number} wait The number of milliseconds to delay.
  3251. * @param {Boolean} immediate A flag to indicate execution is on the leading
  3252. * edge of the timeout.
  3253. * @returns {Function} Returns the new debounced function.
  3254. * @example
  3255. *
  3256. * var lazyLayout = _.debounce(calculateLayout, 300);
  3257. * jQuery(window).on('resize', lazyLayout);
  3258. */
  3259. function debounce(func, wait, immediate) {
  3260. var args,
  3261. result,
  3262. thisArg,
  3263. timeoutId;
  3264. function delayed() {
  3265. timeoutId = null;
  3266. if (!immediate) {
  3267. result = func.apply(thisArg, args);
  3268. }
  3269. }
  3270. return function() {
  3271. var isImmediate = immediate && !timeoutId;
  3272. args = arguments;
  3273. thisArg = this;
  3274. clearTimeout(timeoutId);
  3275. timeoutId = setTimeout(delayed, wait);
  3276. if (isImmediate) {
  3277. result = func.apply(thisArg, args);
  3278. }
  3279. return result;
  3280. };
  3281. }
  3282. /**
  3283. * Executes the `func` function after `wait` milliseconds. Additional arguments
  3284. * will be passed to `func` when it is invoked.
  3285. *
  3286. * @static
  3287. * @memberOf _
  3288. * @category Functions
  3289. * @param {Function} func The function to delay.
  3290. * @param {Number} wait The number of milliseconds to delay execution.
  3291. * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with.
  3292. * @returns {Number} Returns the `setTimeout` timeout id.
  3293. * @example
  3294. *
  3295. * var log = _.bind(console.log, console);
  3296. * _.delay(log, 1000, 'logged later');
  3297. * // => 'logged later' (Appears after one second.)
  3298. */
  3299. function delay(func, wait) {
  3300. var args = slice.call(arguments, 2);
  3301. return setTimeout(function() { return func.apply(undefined, args); }, wait);
  3302. }
  3303. /**
  3304. * Defers executing the `func` function until the current call stack has cleared.
  3305. * Additional arguments will be passed to `func` when it is invoked.
  3306. *
  3307. * @static
  3308. * @memberOf _
  3309. * @category Functions
  3310. * @param {Function} func The function to defer.
  3311. * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with.
  3312. * @returns {Number} Returns the `setTimeout` timeout id.
  3313. * @example
  3314. *
  3315. * _.defer(function() { alert('deferred'); });
  3316. * // returns from the function before `alert` is called
  3317. */
  3318. function defer(func) {
  3319. var args = slice.call(arguments, 1);
  3320. return setTimeout(function() { return func.apply(undefined, args); }, 1);
  3321. }
  3322. /**
  3323. * Creates a new function that memoizes the result of `func`. If `resolver` is
  3324. * passed, it will be used to determine the cache key for storing the result
  3325. * based on the arguments passed to the memoized function. By default, the first
  3326. * argument passed to the memoized function is used as the cache key.
  3327. *
  3328. * @static
  3329. * @memberOf _
  3330. * @category Functions
  3331. * @param {Function} func The function to have its output memoized.
  3332. * @param {Function} [resolver] A function used to resolve the cache key.
  3333. * @returns {Function} Returns the new memoizing function.
  3334. * @example
  3335. *
  3336. * var fibonacci = _.memoize(function(n) {
  3337. * return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
  3338. * });
  3339. */
  3340. function memoize(func, resolver) {
  3341. var cache = {};
  3342. return function() {
  3343. var prop = resolver ? resolver.apply(this, arguments) : arguments[0];
  3344. return hasOwnProperty.call(cache, prop)
  3345. ? cache[prop]
  3346. : (cache[prop] = func.apply(this, arguments));
  3347. };
  3348. }
  3349. /**
  3350. * Creates a new function that is restricted to one execution. Repeat calls to
  3351. * the function will return the value of the first call.
  3352. *
  3353. * @static
  3354. * @memberOf _
  3355. * @category Functions
  3356. * @param {Function} func The function to restrict.
  3357. * @returns {Function} Returns the new restricted function.
  3358. * @example
  3359. *
  3360. * var initialize = _.once(createApplication);
  3361. * initialize();
  3362. * initialize();
  3363. * // Application is only created once.
  3364. */
  3365. function once(func) {
  3366. var result,
  3367. ran = false;
  3368. return function() {
  3369. if (ran) {
  3370. return result;
  3371. }
  3372. ran = true;
  3373. result = func.apply(this, arguments);
  3374. // clear the `func` variable so the function may be garbage collected
  3375. func = null;
  3376. return result;
  3377. };
  3378. }
  3379. /**
  3380. * Creates a new function that, when called, invokes `func` with any additional
  3381. * `partial` arguments prepended to those passed to the new function. This method
  3382. * is similar `bind`, except it does **not** alter the `this` binding.
  3383. *
  3384. * @static
  3385. * @memberOf _
  3386. * @category Functions
  3387. * @param {Function} func The function to partially apply arguments to.
  3388. * @param {Mixed} [arg1, arg2, ...] Arguments to be partially applied.
  3389. * @returns {Function} Returns the new partially applied function.
  3390. * @example
  3391. *
  3392. * var greet = function(greeting, name) { return greeting + ': ' + name; };
  3393. * var hi = _.partial(greet, 'hi');
  3394. * hi('moe');
  3395. * // => 'hi: moe'
  3396. */
  3397. function partial(func) {
  3398. var args = slice.call(arguments, 1),
  3399. argsLength = args.length;
  3400. return function() {
  3401. var result,
  3402. others = arguments;
  3403. if (others.length) {
  3404. args.length = argsLength;
  3405. push.apply(args, others);
  3406. }
  3407. result = args.length == 1 ? func.call(this, args[0]) : func.apply(this, args);
  3408. args.length = argsLength;
  3409. return result;
  3410. };
  3411. }
  3412. /**
  3413. * Creates a new function that, when executed, will only call the `func`
  3414. * function at most once per every `wait` milliseconds. If the throttled
  3415. * function is invoked more than once during the `wait` timeout, `func` will
  3416. * also be called on the trailing edge of the timeout. Subsequent calls to the
  3417. * throttled function will return the result of the last `func` call.
  3418. *
  3419. * @static
  3420. * @memberOf _
  3421. * @category Functions
  3422. * @param {Function} func The function to throttle.
  3423. * @param {Number} wait The number of milliseconds to throttle executions to.
  3424. * @returns {Function} Returns the new throttled function.
  3425. * @example
  3426. *
  3427. * var throttled = _.throttle(updatePosition, 100);
  3428. * jQuery(window).on('scroll', throttled);
  3429. */
  3430. function throttle(func, wait) {
  3431. var args,
  3432. result,
  3433. thisArg,
  3434. timeoutId,
  3435. lastCalled = 0;
  3436. function trailingCall() {
  3437. lastCalled = new Date;
  3438. timeoutId = null;
  3439. result = func.apply(thisArg, args);
  3440. }
  3441. return function() {
  3442. var now = new Date,
  3443. remain = wait - (now - lastCalled);
  3444. args = arguments;
  3445. thisArg = this;
  3446. if (remain <= 0) {
  3447. lastCalled = now;
  3448. result = func.apply(thisArg, args);
  3449. }
  3450. else if (!timeoutId) {
  3451. timeoutId = setTimeout(trailingCall, remain);
  3452. }
  3453. return result;
  3454. };
  3455. }
  3456. /**
  3457. * Creates a new function that passes `value` to the `wrapper` function as its
  3458. * first argument. Additional arguments passed to the new function are appended
  3459. * to those passed to the `wrapper` function.
  3460. *
  3461. * @static
  3462. * @memberOf _
  3463. * @category Functions
  3464. * @param {Mixed} value The value to wrap.
  3465. * @param {Function} wrapper The wrapper function.
  3466. * @returns {Function} Returns the new function.
  3467. * @example
  3468. *
  3469. * var hello = function(name) { return 'hello: ' + name; };
  3470. * hello = _.wrap(hello, function(func) {
  3471. * return 'before, ' + func('moe') + ', after';
  3472. * });
  3473. * hello();
  3474. * // => 'before, hello: moe, after'
  3475. */
  3476. function wrap(value, wrapper) {
  3477. return function() {
  3478. var args = [value];
  3479. if (arguments.length) {
  3480. push.apply(args, arguments);
  3481. }
  3482. return wrapper.apply(this, args);
  3483. };
  3484. }
  3485. /*--------------------------------------------------------------------------*/
  3486. /**
  3487. * Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their
  3488. * corresponding HTML entities.
  3489. *
  3490. * @static
  3491. * @memberOf _
  3492. * @category Utilities
  3493. * @param {String} string The string to escape.
  3494. * @returns {String} Returns the escaped string.
  3495. * @example
  3496. *
  3497. * _.escape('Moe, Larry & Curly');
  3498. * // => "Moe, Larry &amp; Curly"
  3499. */
  3500. function escape(string) {
  3501. return string == null ? '' : (string + '').replace(reUnescapedHtml, escapeHtmlChar);
  3502. }
  3503. /**
  3504. * This function returns the first argument passed to it.
  3505. *
  3506. * Note: It is used throughout Lo-Dash as a default callback.
  3507. *
  3508. * @static
  3509. * @memberOf _
  3510. * @category Utilities
  3511. * @param {Mixed} value Any value.
  3512. * @returns {Mixed} Returns `value`.
  3513. * @example
  3514. *
  3515. * var moe = { 'name': 'moe' };
  3516. * moe === _.identity(moe);
  3517. * // => true
  3518. */
  3519. function identity(value) {
  3520. return value;
  3521. }
  3522. /**
  3523. * Adds functions properties of `object` to the `lodash` function and chainable
  3524. * wrapper.
  3525. *
  3526. * @static
  3527. * @memberOf _
  3528. * @category Utilities
  3529. * @param {Object} object The object of function properties to add to `lodash`.
  3530. * @example
  3531. *
  3532. * _.mixin({
  3533. * 'capitalize': function(string) {
  3534. * return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
  3535. * }
  3536. * });
  3537. *
  3538. * _.capitalize('larry');
  3539. * // => 'Larry'
  3540. *
  3541. * _('curly').capitalize();
  3542. * // => 'Curly'
  3543. */
  3544. function mixin(object) {
  3545. forEach(functions(object), function(methodName) {
  3546. var func = lodash[methodName] = object[methodName];
  3547. LoDash.prototype[methodName] = function() {
  3548. var args = [this.__wrapped__];
  3549. if (arguments.length) {
  3550. push.apply(args, arguments);
  3551. }
  3552. var result = func.apply(lodash, args);
  3553. if (this.__chain__) {
  3554. result = new LoDash(result);
  3555. result.__chain__ = true;
  3556. }
  3557. return result;
  3558. };
  3559. });
  3560. }
  3561. /**
  3562. * Reverts the '_' variable to its previous value and returns a reference to
  3563. * the `lodash` function.
  3564. *
  3565. * @static
  3566. * @memberOf _
  3567. * @category Utilities
  3568. * @returns {Function} Returns the `lodash` function.
  3569. * @example
  3570. *
  3571. * var lodash = _.noConflict();
  3572. */
  3573. function noConflict() {
  3574. window._ = oldDash;
  3575. return this;
  3576. }
  3577. /**
  3578. * Produces a random number between `min` and `max` (inclusive). If only one
  3579. * argument is passed, a number between `0` and the given number will be returned.
  3580. * If no arguments are passed `_.random` will act as `Math.random`.
  3581. *
  3582. * @static
  3583. * @memberOf _
  3584. * @category Utilities
  3585. * @param {Number} min The minimum possible value.
  3586. * @param {Number} max The maximum possible value.
  3587. * @returns {Number} Returns a random number.
  3588. * @example
  3589. *
  3590. * _.random(0, 5);
  3591. * // => a number between 1 and 5
  3592. *
  3593. * _.random(5);
  3594. * // => also a number between 1 and 5
  3595. *
  3596. * _.random();
  3597. * // => an integer between 0 and less than 1
  3598. */
  3599. function random(min, max) {
  3600. if (min == null && max == null) {
  3601. return nativeRandom();
  3602. }
  3603. min = +min || 0;
  3604. if (max == null) {
  3605. max = min;
  3606. min = 0;
  3607. }
  3608. return min + nativeFloor(nativeRandom() * ((+max || 0) - min + 1));
  3609. }
  3610. /**
  3611. * Resolves the value of `property` on `object`. If `property` is a function
  3612. * it will be invoked and its result returned, else the property value is
  3613. * returned. If `object` is falsey, then `null` is returned.
  3614. *
  3615. * @deprecated
  3616. * @static
  3617. * @memberOf _
  3618. * @category Utilities
  3619. * @param {Object} object The object to inspect.
  3620. * @param {String} property The property to get the result of.
  3621. * @returns {Mixed} Returns the resolved value.
  3622. * @example
  3623. *
  3624. * var object = {
  3625. * 'cheese': 'crumpets',
  3626. * 'stuff': function() {
  3627. * return 'nonsense';
  3628. * }
  3629. * };
  3630. *
  3631. * _.result(object, 'cheese');
  3632. * // => 'crumpets'
  3633. *
  3634. * _.result(object, 'stuff');
  3635. * // => 'nonsense'
  3636. */
  3637. function result(object, property) {
  3638. // based on Backbone's private `getValue` function
  3639. // https://github.com/documentcloud/backbone/blob/0.9.2/backbone.js#L1419-1424
  3640. if (!object) {
  3641. return null;
  3642. }
  3643. var value = object[property];
  3644. return isFunction(value) ? object[property]() : value;
  3645. }
  3646. /**
  3647. * A micro-templating method that handles arbitrary delimiters, preserves
  3648. * whitespace, and correctly escapes quotes within interpolated code.
  3649. *
  3650. * Note: In the development build `_.template` utilizes sourceURLs for easier
  3651. * debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
  3652. *
  3653. * Note: Lo-Dash may be used in Chrome extensions by either creating a `lodash csp`
  3654. * build and avoiding `_.template` use, or loading Lo-Dash in a sandboxed page.
  3655. * See http://developer.chrome.com/trunk/extensions/sandboxingEval.html
  3656. *
  3657. * @static
  3658. * @memberOf _
  3659. * @category Utilities
  3660. * @param {String} text The template text.
  3661. * @param {Obect} data The data object used to populate the text.
  3662. * @param {Object} options The options object.
  3663. * @returns {Function|String} Returns a compiled function when no `data` object
  3664. * is given, else it returns the interpolated text.
  3665. * @example
  3666. *
  3667. * // using a compiled template
  3668. * var compiled = _.template('hello: <%= name %>');
  3669. * compiled({ 'name': 'moe' });
  3670. * // => 'hello: moe'
  3671. *
  3672. * var list = '<% _.forEach(people, function(name) { %> <li><%= name %></li> <% }); %>';
  3673. * _.template(list, { 'people': ['moe', 'larry', 'curly'] });
  3674. * // => '<li>moe</li><li>larry</li><li>curly</li>'
  3675. *
  3676. * // using the "escape" delimiter to escape HTML in data property values
  3677. * _.template('<b><%- value %></b>', { 'value': '<script>' });
  3678. * // => '<b>&lt;script></b>'
  3679. *
  3680. * // using the internal `print` function in "evaluate" delimiters
  3681. * _.template('<% print("Hello " + epithet); %>', { 'epithet': 'stooge' });
  3682. * // => 'Hello stooge.'
  3683. *
  3684. * // using custom template delimiter settings
  3685. * _.templateSettings = {
  3686. * 'interpolate': /\{\{(.+?)\}\}/g
  3687. * };
  3688. *
  3689. * _.template('Hello {{ name }}!', { 'name': 'Mustache' });
  3690. * // => 'Hello Mustache!'
  3691. *
  3692. * // using the `variable` option to ensure a with-statement isn't used in the compiled template
  3693. * var compiled = _.template('hello: <%= data.name %>', null, { 'variable': 'data' });
  3694. * compiled.source;
  3695. * // => function(data) {
  3696. * var __t, __p = '', __e = _.escape;
  3697. * __p += 'hello: ' + ((__t = ( data.name )) == null ? '' : __t);
  3698. * return __p;
  3699. * }
  3700. *
  3701. * // using the `source` property to inline compiled templates for meaningful
  3702. * // line numbers in error messages and a stack trace
  3703. * fs.writeFileSync(path.join(cwd, 'jst.js'), '\
  3704. * var JST = {\
  3705. * "main": ' + _.template(mainText).source + '\
  3706. * };\
  3707. * ');
  3708. */
  3709. function template(text, data, options) {
  3710. // based on John Resig's `tmpl` implementation
  3711. // http://ejohn.org/blog/javascript-micro-templating/
  3712. // and Laura Doktorova's doT.js
  3713. // https://github.com/olado/doT
  3714. options || (options = {});
  3715. text += '';
  3716. var isEvaluating,
  3717. result,
  3718. escapeDelimiter = options.escape,
  3719. evaluateDelimiter = options.evaluate,
  3720. interpolateDelimiter = options.interpolate,
  3721. settings = lodash.templateSettings,
  3722. variable = options.variable || settings.variable,
  3723. hasVariable = variable;
  3724. // use default settings if no options object is provided
  3725. if (escapeDelimiter == null) {
  3726. escapeDelimiter = settings.escape;
  3727. }
  3728. if (evaluateDelimiter == null) {
  3729. // use `false` as the fallback value, instead of leaving it `undefined`,
  3730. // so the initial assignment of `reEvaluateDelimiter` will still occur
  3731. evaluateDelimiter = settings.evaluate || false;
  3732. }
  3733. if (interpolateDelimiter == null) {
  3734. interpolateDelimiter = settings.interpolate;
  3735. }
  3736. // tokenize delimiters to avoid escaping them
  3737. if (escapeDelimiter) {
  3738. text = text.replace(escapeDelimiter, tokenizeEscape);
  3739. }
  3740. if (interpolateDelimiter) {
  3741. text = text.replace(interpolateDelimiter, tokenizeInterpolate);
  3742. }
  3743. if (evaluateDelimiter != lastEvaluateDelimiter) {
  3744. // generate `reEvaluateDelimiter` to match `_.templateSettings.evaluate`
  3745. // and internal `<e%- %>`, `<e%= %>` delimiters
  3746. lastEvaluateDelimiter = evaluateDelimiter;
  3747. reEvaluateDelimiter = RegExp(
  3748. '<e%-([\\s\\S]+?)%>|<e%=([\\s\\S]+?)%>' +
  3749. (evaluateDelimiter ? '|' + evaluateDelimiter.source : '')
  3750. , 'g');
  3751. }
  3752. isEvaluating = tokenized.length;
  3753. text = text.replace(reEvaluateDelimiter, tokenizeEvaluate);
  3754. isEvaluating = isEvaluating != tokenized.length;
  3755. // escape characters that cannot be included in string literals and
  3756. // detokenize delimiter code snippets
  3757. text = "__p += '" + text
  3758. .replace(reUnescapedString, escapeStringChar)
  3759. .replace(reToken, detokenize) + "';\n";
  3760. // clear stored code snippets
  3761. tokenized.length = 0;
  3762. // if `variable` is not specified and the template contains "evaluate"
  3763. // delimiters, wrap a with-statement around the generated code to add the
  3764. // data object to the top of the scope chain
  3765. if (!hasVariable) {
  3766. variable = lastVariable || 'obj';
  3767. if (isEvaluating) {
  3768. text = 'with (' + variable + ') {\n' + text + '\n}\n';
  3769. }
  3770. else {
  3771. if (variable != lastVariable) {
  3772. // generate `reDoubleVariable` to match references like `obj.obj` inside
  3773. // transformed "escape" and "interpolate" delimiters
  3774. lastVariable = variable;
  3775. reDoubleVariable = RegExp('(\\(\\s*)' + variable + '\\.' + variable + '\\b', 'g');
  3776. }
  3777. // avoid a with-statement by prepending data object references to property names
  3778. text = text
  3779. .replace(reInsertVariable, '$&' + variable + '.')
  3780. .replace(reDoubleVariable, '$1__d');
  3781. }
  3782. }
  3783. // cleanup code by stripping empty strings
  3784. text = ( isEvaluating ? text.replace(reEmptyStringLeading, '') : text)
  3785. .replace(reEmptyStringMiddle, '$1')
  3786. .replace(reEmptyStringTrailing, '$1;');
  3787. // frame code as the function body
  3788. text = 'function(' + variable + ') {\n' +
  3789. (hasVariable ? '' : variable + ' || (' + variable + ' = {});\n') +
  3790. 'var __t, __p = \'\', __e = _.escape' +
  3791. (isEvaluating
  3792. ? ', __j = Array.prototype.join;\n' +
  3793. 'function print() { __p += __j.call(arguments, \'\') }\n'
  3794. : (hasVariable ? '' : ', __d = ' + variable + '.' + variable + ' || ' + variable) + ';\n'
  3795. ) +
  3796. text +
  3797. 'return __p\n}';
  3798. // add a sourceURL for easier debugging
  3799. // http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
  3800. if (useSourceURL) {
  3801. text += '\n//@ sourceURL=/lodash/template/source[' + (templateCounter++) + ']';
  3802. }
  3803. try {
  3804. result = Function('_', 'return ' + text)(lodash);
  3805. } catch(e) {
  3806. e.source = text;
  3807. throw e;
  3808. }
  3809. if (data) {
  3810. return result(data);
  3811. }
  3812. // provide the compiled function's source via its `toString` method, in
  3813. // supported environments, or the `source` property as a convenience for
  3814. // inlining compiled templates during the build process
  3815. result.source = text;
  3816. return result;
  3817. }
  3818. /**
  3819. * Executes the `callback` function `n` times. The `callback` is bound to
  3820. * `thisArg` and invoked with 1 argument; (index).
  3821. *
  3822. * @static
  3823. * @memberOf _
  3824. * @category Utilities
  3825. * @param {Number} n The number of times to execute the callback.
  3826. * @param {Function} callback The function called per iteration.
  3827. * @param {Mixed} [thisArg] The `this` binding for the callback.
  3828. * @example
  3829. *
  3830. * _.times(3, function(n) { genie.grantWish(n); });
  3831. * // => calls `genie.grantWish(n)` three times, passing `n` of `0`, `1`, and `2` respectively
  3832. *
  3833. * _.times(3, function(n) { this.grantWish(n); }, genie);
  3834. * // => also calls `genie.grantWish(n)` three times
  3835. */
  3836. function times(n, callback, thisArg) {
  3837. var index = -1;
  3838. if (thisArg) {
  3839. while (++index < n) {
  3840. callback.call(thisArg, index);
  3841. }
  3842. } else {
  3843. while (++index < n) {
  3844. callback(index);
  3845. }
  3846. }
  3847. }
  3848. /**
  3849. * Converts the HTML entities `&amp;`, `&lt;`, `&gt;`, `&quot;`, and `&#x27;`
  3850. * in `string` to their corresponding characters.
  3851. *
  3852. * @static
  3853. * @memberOf _
  3854. * @category Utilities
  3855. * @param {String} string The string to unescape.
  3856. * @returns {String} Returns the unescaped string.
  3857. * @example
  3858. *
  3859. * _.unescape('Moe, Larry &amp; Curly');
  3860. * // => "Moe, Larry & Curly"
  3861. */
  3862. function unescape(string) {
  3863. return string == null ? '' : (string + '').replace(reEscapedHtml, unescapeHtmlChar);
  3864. }
  3865. /**
  3866. * Generates a unique id. If `prefix` is passed, the id will be appended to it.
  3867. *
  3868. * @static
  3869. * @memberOf _
  3870. * @category Utilities
  3871. * @param {String} [prefix] The value to prefix the id with.
  3872. * @returns {Number|String} Returns a numeric id if no prefix is passed, else
  3873. * a string id may be returned.
  3874. * @example
  3875. *
  3876. * _.uniqueId('contact_');
  3877. * // => 'contact_104'
  3878. */
  3879. function uniqueId(prefix) {
  3880. var id = idCounter++;
  3881. return prefix ? prefix + id : id;
  3882. }
  3883. /*--------------------------------------------------------------------------*/
  3884. /**
  3885. * Wraps the value in a `lodash` wrapper object.
  3886. *
  3887. * @static
  3888. * @memberOf _
  3889. * @category Chaining
  3890. * @param {Mixed} value The value to wrap.
  3891. * @returns {Object} Returns the wrapper object.
  3892. * @example
  3893. *
  3894. * var stooges = [
  3895. * { 'name': 'moe', 'age': 40 },
  3896. * { 'name': 'larry', 'age': 50 },
  3897. * { 'name': 'curly', 'age': 60 }
  3898. * ];
  3899. *
  3900. * var youngest = _.chain(stooges)
  3901. * .sortBy(function(stooge) { return stooge.age; })
  3902. * .map(function(stooge) { return stooge.name + ' is ' + stooge.age; })
  3903. * .first()
  3904. * .value();
  3905. * // => 'moe is 40'
  3906. */
  3907. function chain(value) {
  3908. value = new LoDash(value);
  3909. value.__chain__ = true;
  3910. return value;
  3911. }
  3912. /**
  3913. * Invokes `interceptor` with the `value` as the first argument, and then
  3914. * returns `value`. The purpose of this method is to "tap into" a method chain,
  3915. * in order to perform operations on intermediate results within the chain.
  3916. *
  3917. * @static
  3918. * @memberOf _
  3919. * @category Chaining
  3920. * @param {Mixed} value The value to pass to `interceptor`.
  3921. * @param {Function} interceptor The function to invoke.
  3922. * @returns {Mixed} Returns `value`.
  3923. * @example
  3924. *
  3925. * _.chain([1,2,3,200])
  3926. * .filter(function(num) { return num % 2 == 0; })
  3927. * .tap(alert)
  3928. * .map(function(num) { return num * num })
  3929. * .value();
  3930. * // => // [2, 200] (alerted)
  3931. * // => [4, 40000]
  3932. */
  3933. function tap(value, interceptor) {
  3934. interceptor(value);
  3935. return value;
  3936. }
  3937. /**
  3938. * Enables method chaining on the wrapper object.
  3939. *
  3940. * @name chain
  3941. * @deprecated
  3942. * @memberOf _
  3943. * @category Chaining
  3944. * @returns {Mixed} Returns the wrapper object.
  3945. * @example
  3946. *
  3947. * _([1, 2, 3]).value();
  3948. * // => [1, 2, 3]
  3949. */
  3950. function wrapperChain() {
  3951. this.__chain__ = true;
  3952. return this;
  3953. }
  3954. /**
  3955. * Extracts the wrapped value.
  3956. *
  3957. * @name value
  3958. * @memberOf _
  3959. * @category Chaining
  3960. * @returns {Mixed} Returns the wrapped value.
  3961. * @example
  3962. *
  3963. * _([1, 2, 3]).value();
  3964. * // => [1, 2, 3]
  3965. */
  3966. function wrapperValue() {
  3967. return this.__wrapped__;
  3968. }
  3969. /*--------------------------------------------------------------------------*/
  3970. /**
  3971. * The semantic version number.
  3972. *
  3973. * @static
  3974. * @memberOf _
  3975. * @type String
  3976. */
  3977. lodash.VERSION = '0.7.0';
  3978. // assign static methods
  3979. lodash.after = after;
  3980. lodash.bind = bind;
  3981. lodash.bindAll = bindAll;
  3982. lodash.chain = chain;
  3983. lodash.clone = clone;
  3984. lodash.compact = compact;
  3985. lodash.compose = compose;
  3986. lodash.contains = contains;
  3987. lodash.countBy = countBy;
  3988. lodash.debounce = debounce;
  3989. lodash.defaults = defaults;
  3990. lodash.defer = defer;
  3991. lodash.delay = delay;
  3992. lodash.difference = difference;
  3993. lodash.escape = escape;
  3994. lodash.every = every;
  3995. lodash.extend = extend;
  3996. lodash.filter = filter;
  3997. lodash.find = find;
  3998. lodash.first = first;
  3999. lodash.flatten = flatten;
  4000. lodash.forEach = forEach;
  4001. lodash.forIn = forIn;
  4002. lodash.forOwn = forOwn;
  4003. lodash.functions = functions;
  4004. lodash.groupBy = groupBy;
  4005. lodash.has = has;
  4006. lodash.identity = identity;
  4007. lodash.indexOf = indexOf;
  4008. lodash.initial = initial;
  4009. lodash.intersection = intersection;
  4010. lodash.invert = invert;
  4011. lodash.invoke = invoke;
  4012. lodash.isArguments = isArguments;
  4013. lodash.isArray = isArray;
  4014. lodash.isBoolean = isBoolean;
  4015. lodash.isDate = isDate;
  4016. lodash.isElement = isElement;
  4017. lodash.isEmpty = isEmpty;
  4018. lodash.isEqual = isEqual;
  4019. lodash.isFinite = isFinite;
  4020. lodash.isFunction = isFunction;
  4021. lodash.isNaN = isNaN;
  4022. lodash.isNull = isNull;
  4023. lodash.isNumber = isNumber;
  4024. lodash.isObject = isObject;
  4025. lodash.isRegExp = isRegExp;
  4026. lodash.isString = isString;
  4027. lodash.isUndefined = isUndefined;
  4028. lodash.keys = keys;
  4029. lodash.last = last;
  4030. lodash.lastIndexOf = lastIndexOf;
  4031. lodash.map = map;
  4032. lodash.max = max;
  4033. lodash.memoize = memoize;
  4034. lodash.merge = merge;
  4035. lodash.min = min;
  4036. lodash.mixin = mixin;
  4037. lodash.noConflict = noConflict;
  4038. lodash.object = object;
  4039. lodash.omit = omit;
  4040. lodash.once = once;
  4041. lodash.pairs = pairs;
  4042. lodash.partial = partial;
  4043. lodash.pick = pick;
  4044. lodash.pluck = pluck;
  4045. lodash.random = random;
  4046. lodash.range = range;
  4047. lodash.reduce = reduce;
  4048. lodash.reduceRight = reduceRight;
  4049. lodash.reject = reject;
  4050. lodash.rest = rest;
  4051. lodash.result = result;
  4052. lodash.shuffle = shuffle;
  4053. lodash.size = size;
  4054. lodash.some = some;
  4055. lodash.sortBy = sortBy;
  4056. lodash.sortedIndex = sortedIndex;
  4057. lodash.tap = tap;
  4058. lodash.template = template;
  4059. lodash.throttle = throttle;
  4060. lodash.times = times;
  4061. lodash.toArray = toArray;
  4062. lodash.unescape = unescape;
  4063. lodash.union = union;
  4064. lodash.uniq = uniq;
  4065. lodash.uniqueId = uniqueId;
  4066. lodash.values = values;
  4067. lodash.where = where;
  4068. lodash.without = without;
  4069. lodash.wrap = wrap;
  4070. lodash.zip = zip;
  4071. // assign aliases
  4072. lodash.all = every;
  4073. lodash.any = some;
  4074. lodash.collect = map;
  4075. lodash.detect = find;
  4076. lodash.drop = rest;
  4077. lodash.each = forEach;
  4078. lodash.foldl = reduce;
  4079. lodash.foldr = reduceRight;
  4080. lodash.head = first;
  4081. lodash.include = contains;
  4082. lodash.inject = reduce;
  4083. lodash.methods = functions;
  4084. lodash.select = filter;
  4085. lodash.tail = rest;
  4086. lodash.take = first;
  4087. lodash.unique = uniq;
  4088. // add pseudo private properties used and removed during the build process
  4089. lodash._iteratorTemplate = iteratorTemplate;
  4090. lodash._shimKeys = shimKeys;
  4091. /*--------------------------------------------------------------------------*/
  4092. // assign private `LoDash` constructor's prototype
  4093. LoDash.prototype = lodash.prototype;
  4094. // add all static functions to `LoDash.prototype`
  4095. mixin(lodash);
  4096. // add `LoDash.prototype.chain` after calling `mixin()` to avoid overwriting
  4097. // it with the wrapped `lodash.chain`
  4098. LoDash.prototype.chain = wrapperChain;
  4099. LoDash.prototype.value = wrapperValue;
  4100. // add all mutator Array functions to the wrapper.
  4101. forEach(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
  4102. var func = ArrayProto[methodName];
  4103. LoDash.prototype[methodName] = function() {
  4104. var value = this.__wrapped__;
  4105. func.apply(value, arguments);
  4106. // avoid array-like object bugs with `Array#shift` and `Array#splice` in
  4107. // Firefox < 10 and IE < 9
  4108. if (hasObjectSpliceBug && value.length === 0) {
  4109. delete value[0];
  4110. }
  4111. if (this.__chain__) {
  4112. value = new LoDash(value);
  4113. value.__chain__ = true;
  4114. }
  4115. return value;
  4116. };
  4117. });
  4118. // add all accessor Array functions to the wrapper.
  4119. forEach(['concat', 'join', 'slice'], function(methodName) {
  4120. var func = ArrayProto[methodName];
  4121. LoDash.prototype[methodName] = function() {
  4122. var value = this.__wrapped__,
  4123. result = func.apply(value, arguments);
  4124. if (this.__chain__) {
  4125. result = new LoDash(result);
  4126. result.__chain__ = true;
  4127. }
  4128. return result;
  4129. };
  4130. });
  4131. /*--------------------------------------------------------------------------*/
  4132. // expose Lo-Dash
  4133. // some AMD build optimizers, like r.js, check for specific condition patterns like the following:
  4134. if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  4135. // Expose Lo-Dash to the global object even when an AMD loader is present in
  4136. // case Lo-Dash was injected by a third-party script and not intended to be
  4137. // loaded as a module. The global assignment can be reverted in the Lo-Dash
  4138. // module via its `noConflict()` method.
  4139. window._ = lodash;
  4140. // define as an anonymous module so, through path mapping, it can be
  4141. // referenced as the "underscore" module
  4142. define(function() {
  4143. return lodash;
  4144. });
  4145. }
  4146. // check for `exports` after `define` in case a build optimizer adds an `exports` object
  4147. else if (freeExports) {
  4148. // in Node.js or RingoJS v0.8.0+
  4149. if (typeof module == 'object' && module && module.exports == freeExports) {
  4150. (module.exports = lodash)._ = lodash;
  4151. }
  4152. // in Narwhal or RingoJS v0.7.0-
  4153. else {
  4154. freeExports._ = lodash;
  4155. }
  4156. }
  4157. else {
  4158. // in a browser or Rhino
  4159. window._ = lodash;
  4160. }
  4161. }(this));