lodash.compat.js 162 KB

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