1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184 |
- (function(global, factory) {
- /*jshint -W030 */
- 'use strict';
- typeof exports === 'object' && typeof module !== 'undefined'
- ? factory(exports)
- : typeof define === 'function' && define.amd
- ? define(['exports'], factory)
- : global.async
- ? factory((global.neo_async = global.neo_async || {}))
- : factory((global.async = global.async || {}));
- })(this, function(exports) {
- 'use strict';
- var noop = function noop() {};
- var throwError = function throwError() {
- throw new Error('Callback was already called.');
- };
- var DEFAULT_TIMES = 5;
- var DEFAULT_INTERVAL = 0;
- var obj = 'object';
- var func = 'function';
- var isArray = Array.isArray;
- var nativeKeys = Object.keys;
- var nativePush = Array.prototype.push;
- var iteratorSymbol = typeof Symbol === func && Symbol.iterator;
- var nextTick, asyncNextTick, asyncSetImmediate;
- createImmediate();
- /**
- * @memberof async
- * @namespace each
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done();
- * }, num * 10);
- * };
- * async.each(array, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done();
- * }, num * 10);
- * };
- * async.each(array, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [[1, 0], [2, 2], [3, 1]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done();
- * }, num * 10);
- * };
- * async.each(object, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done();
- * }, num * 10);
- * };
- * async.each(object, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
- * });
- *
- * @example
- *
- * // break
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num !== 2);
- * }, num * 10);
- * };
- * async.each(array, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [1, 2]
- * });
- *
- */
- var each = createEach(arrayEach, baseEach, symbolEach);
- /**
- * @memberof async
- * @namespace map
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.map(array, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2];
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.map(array, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2]
- * console.log(order); // [[1, 0], [2, 2], [3, 1]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.map(object, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2]
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.map(object, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2]
- * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
- * });
- *
- */
- var map = createMap(arrayEachIndex, baseEachIndex, symbolEachIndex, true);
- /**
- * @memberof async
- * @namespace mapValues
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValues(array, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 3, '2': 2 }
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValues(array, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 3, '2': 2 }
- * console.log(order); // [[1, 0], [2, 2], [3, 1]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValues(object, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 3, c: 2 }
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValues(object, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 3, c: 2 }
- * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
- * });
- *
- */
- var mapValues = createMap(arrayEachIndex, baseEachKey, symbolEachKey, false);
- /**
- * @memberof async
- * @namespace filter
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filter(array, iterator, function(err, res) {
- * console.log(res); // [1, 3];
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filter(array, iterator, function(err, res) {
- * console.log(res); // [1, 3];
- * console.log(order); // [[1, 0], [2, 2], [3, 1]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filter(object, iterator, function(err, res) {
- * console.log(res); // [1, 3];
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filter(object, iterator, function(err, res) {
- * console.log(res); // [1, 3];
- * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
- * });
- *
- */
- var filter = createFilter(arrayEachIndexValue, baseEachIndexValue, symbolEachIndexValue, true);
- /**
- * @memberof async
- * @namespace filterSeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filterSeries(array, iterator, function(err, res) {
- * console.log(res); // [1, 3];
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filterSeries(array, iterator, function(err, res) {
- * console.log(res); // [1, 3]
- * console.log(order); // [[1, 0], [3, 1], [2, 2]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filterSeries(object, iterator, function(err, res) {
- * console.log(res); // [1, 3]
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filterSeries(object, iterator, function(err, res) {
- * console.log(res); // [1, 3]
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
- * });
- *
- */
- var filterSeries = createFilterSeries(true);
- /**
- * @memberof async
- * @namespace filterLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filterLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [1, 5, 3]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filterLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [1, 5, 3]
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filterLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // [1, 5, 3]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.filterLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // [1, 5, 3]
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
- * });
- *
- */
- var filterLimit = createFilterLimit(true);
- /**
- * @memberof async
- * @namespace reject
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.reject(array, iterator, function(err, res) {
- * console.log(res); // [2];
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.reject(array, iterator, function(err, res) {
- * console.log(res); // [2];
- * console.log(order); // [[1, 0], [2, 2], [3, 1]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.reject(object, iterator, function(err, res) {
- * console.log(res); // [2];
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.reject(object, iterator, function(err, res) {
- * console.log(res); // [2];
- * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
- * });
- *
- */
- var reject = createFilter(arrayEachIndexValue, baseEachIndexValue, symbolEachIndexValue, false);
- /**
- * @memberof async
- * @namespace rejectSeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.rejectSeries(array, iterator, function(err, res) {
- * console.log(res); // [2];
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.rejectSeries(object, iterator, function(err, res) {
- * console.log(res); // [2];
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.rejectSeries(object, iterator, function(err, res) {
- * console.log(res); // [2];
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
- * });
- *
- */
- var rejectSeries = createFilterSeries(false);
- /**
- * @memberof async
- * @namespace rejectLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.rejectLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [4, 2]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.rejectLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [4, 2]
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.rejectLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // [4, 2]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.rejectLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // [4, 2]
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
- * });
- *
- */
- var rejectLimit = createFilterLimit(false);
- /**
- * @memberof async
- * @namespace detect
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detect(array, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detect(array, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [[1, 0]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detect(object, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detect(object, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [[1, 'a']]
- * });
- *
- */
- var detect = createDetect(arrayEachValue, baseEachValue, symbolEachValue, true);
- /**
- * @memberof async
- * @namespace detectSeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detectSeries(array, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detectSeries(array, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [[1, 0]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detectSeries(object, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detectSeries(object, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [[1, 'a']]
- * });
- *
- */
- var detectSeries = createDetectSeries(true);
- /**
- * @memberof async
- * @namespace detectLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detectLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detectLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [[1, 0]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detectLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.detectLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // 1
- * console.log(order); // [[1, 'a']]
- * });
- *
- */
- var detectLimit = createDetectLimit(true);
- /**
- * @memberof async
- * @namespace every
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.every(array, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [1, 2]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.every(array, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [[1, 0], [2, 2]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.every(object, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [1, 2]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.every(object, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [[1, 'a'], [2, 'c']]
- * });
- *
- */
- var every = createEvery(arrayEachValue, baseEachValue, symbolEachValue);
- /**
- * @memberof async
- * @namespace everySeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.everySeries(array, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.everySeries(array, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [[1, 0], [3, 1], [2, 2]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.everySeries(object, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.everySeries(object, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [[1, 'a'], [3, 'b'] [2, 'c']]
- * });
- *
- */
- var everySeries = createEverySeries();
- /**
- * @memberof async
- * @namespace everyLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.everyLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [1, 3, 5, 2]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.everyLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.everyLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [1, 3, 5, 2]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.everyLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // false
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e']]
- * });
- *
- */
- var everyLimit = createEveryLimit();
- /**
- * @memberof async
- * @namespace pick
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2, 4];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pick(array, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 3 }
- * console.log(order); // [1, 2, 3, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2, 4];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pick(array, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 3 }
- * console.log(order); // [[0, 1], [2, 2], [3, 1], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pick(object, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 3 }
- * console.log(order); // [1, 2, 3, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pick(object, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 3 }
- * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b'], [4, 'd']]
- * });
- *
- */
- var pick = createPick(arrayEachIndexValue, baseEachKeyValue, symbolEachKeyValue, true);
- /**
- * @memberof async
- * @namespace pickSeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2, 4];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pickSeries(array, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 3 }
- * console.log(order); // [1, 3, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2, 4];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pickSeries(array, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 3 }
- * console.log(order); // [[0, 1], [3, 1], [2, 2], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pickSeries(object, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 3 }
- * console.log(order); // [1, 3, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pickSeries(object, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 3 }
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c'], [4, 'd']]
- * });
- *
- */
- var pickSeries = createPickSeries(true);
- /**
- * @memberof async
- * @namespace pickLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pickLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 5, '2': 3 }
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pickLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 5, '2': 3 }
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pickLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 5, c: 3 }
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.pickLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 5, c: 3 }
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
- * });
- *
- */
- var pickLimit = createPickLimit(true);
- /**
- * @memberof async
- * @namespace omit
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2, 4];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omit(array, iterator, function(err, res) {
- * console.log(res); // { '2': 2, '3': 4 }
- * console.log(order); // [1, 2, 3, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2, 4];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omit(array, iterator, function(err, res) {
- * console.log(res); // { '2': 2, '3': 4 }
- * console.log(order); // [[0, 1], [2, 2], [3, 1], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omit(object, iterator, function(err, res) {
- * console.log(res); // { c: 2, d: 4 }
- * console.log(order); // [1, 2, 3, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omit(object, iterator, function(err, res) {
- * console.log(res); // { c: 2, d: 4 }
- * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b'], [4, 'd']]
- * });
- *
- */
- var omit = createPick(arrayEachIndexValue, baseEachKeyValue, symbolEachKeyValue, false);
- /**
- * @memberof async
- * @namespace omitSeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2, 4];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omitSeries(array, iterator, function(err, res) {
- * console.log(res); // { '2': 2, '3': 4 }
- * console.log(order); // [1, 3, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2, 4];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omitSeries(array, iterator, function(err, res) {
- * console.log(res); // { '2': 2, '3': 4 }
- * console.log(order); // [[0, 1], [3, 1], [2, 2], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omitSeries(object, iterator, function(err, res) {
- * console.log(res); // { c: 2, d: 4 }
- * console.log(order); // [1, 3, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omitSeries(object, iterator, function(err, res) {
- * console.log(res); // { c: 2, d: 4 }
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c'], [4, 'd']]
- * });
- *
- */
- var omitSeries = createPickSeries(false);
- /**
- * @memberof async
- * @namespace omitLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omitLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // { '3': 4, '4': 2 }
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omitLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // { '3': 4, '4': 2 }
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omitLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // { d: 4, e: 2 }
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.omitLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // { d: 4, e: 2 }
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
- * });
- *
- */
- var omitLimit = createPickLimit(false);
- /**
- * @memberof async
- * @namespace transform
- * @param {Array|Object} collection
- * @param {Array|Object|Function} [accumulator]
- * @param {Function} [iterator]
- * @param {Function} [callback]
- * @example
- *
- * // array
- * var order = [];
- * var collection = [1, 3, 2, 4];
- * var iterator = function(result, num, done) {
- * setTimeout(function() {
- * order.push(num);
- * result.push(num)
- * done();
- * }, num * 10);
- * };
- * async.transform(collection, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3, 4]
- * console.log(order); // [1, 2, 3, 4]
- * });
- *
- * @example
- *
- * // array with index and accumulator
- * var order = [];
- * var collection = [1, 3, 2, 4];
- * var iterator = function(result, num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * result[index] = num;
- * done();
- * }, num * 10);
- * };
- * async.transform(collection, {}, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 3, '2': 2, '3': 4 }
- * console.log(order); // [[1, 0], [2, 2], [3, 1], [4, 3]]
- * });
- *
- * @example
- *
- * // object with accumulator
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(result, num, done) {
- * setTimeout(function() {
- * order.push(num);
- * result.push(num);
- * done();
- * }, num * 10);
- * };
- * async.transform(collection, [], iterator, function(err, res) {
- * console.log(res); // [1, 2, 3, 4]
- * console.log(order); // [1, 2, 3, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(result, num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * result[key] = num;
- * done();
- * }, num * 10);
- * };
- * async.transform(collection, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 3, c: 2, d: 4 }
- * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b'], [4, 'd']]
- * });
- *
- */
- var transform = createTransform(arrayEachResult, baseEachResult, symbolEachResult);
- /**
- * @memberof async
- * @namespace sortBy
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortBy(array, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3];
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortBy(array, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3]
- * console.log(order); // [[1, 0], [2, 2], [3, 1]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortBy(object, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3]
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortBy(object, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3]
- * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
- * });
- *
- */
- var sortBy = createSortBy(arrayEachIndexValue, baseEachIndexValue, symbolEachIndexValue);
- /**
- * @memberof async
- * @namespace concat
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.concat(array, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3];
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.concat(array, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3]
- * console.log(order); // [[1, 0], [2, 2], [3, 1]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.concat(object, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3]
- * console.log(order); // [1, 2, 3]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.concat(object, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3]
- * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
- * });
- *
- */
- var concat = createConcat(arrayEachIndex, baseEachIndex, symbolEachIndex);
- /**
- * @memberof async
- * @namespace groupBy
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [4.2, 6.4, 6.1];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupBy(array, iterator, function(err, res) {
- * console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
- * console.log(order); // [4.2, 6.1, 6.4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [4.2, 6.4, 6.1];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupBy(array, iterator, function(err, res) {
- * console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
- * console.log(order); // [[4.2, 0], [6.1, 2], [6.4, 1]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 4.2, b: 6.4, c: 6.1 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupBy(object, iterator, function(err, res) {
- * console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
- * console.log(order); // [4.2, 6.1, 6.4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 4.2, b: 6.4, c: 6.1 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupBy(object, iterator, function(err, res) {
- * console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
- * console.log(order); // [[4.2, 'a'], [6.1, 'c'], [6.4, 'b']]
- * });
- *
- */
- var groupBy = createGroupBy(arrayEachValue, baseEachValue, symbolEachValue);
- /**
- * @memberof async
- * @namespace parallel
- * @param {Array|Object} tasks - functions
- * @param {Function} callback
- * @example
- *
- * var order = [];
- * var tasks = [
- * function(done) {
- * setTimeout(function() {
- * order.push(1);
- * done(null, 1);
- * }, 10);
- * },
- * function(done) {
- * setTimeout(function() {
- * order.push(2);
- * done(null, 2);
- * }, 30);
- * },
- * function(done) {
- * setTimeout(function() {
- * order.push(3);
- * done(null, 3);
- * }, 40);
- * },
- * function(done) {
- * setTimeout(function() {
- * order.push(4);
- * done(null, 4);
- * }, 20);
- * }
- * ];
- * async.parallel(tasks, function(err, res) {
- * console.log(res); // [1, 2, 3, 4];
- * console.log(order); // [1, 4, 2, 3]
- * });
- *
- * @example
- *
- * var order = [];
- * var tasks = {
- * 'a': function(done) {
- * setTimeout(function() {
- * order.push(1);
- * done(null, 1);
- * }, 10);
- * },
- * 'b': function(done) {
- * setTimeout(function() {
- * order.push(2);
- * done(null, 2);
- * }, 30);
- * },
- * 'c': function(done) {
- * setTimeout(function() {
- * order.push(3);
- * done(null, 3);
- * }, 40);
- * },
- * 'd': function(done) {
- * setTimeout(function() {
- * order.push(4);
- * done(null, 4);
- * }, 20);
- * }
- * };
- * async.parallel(tasks, function(err, res) {
- * console.log(res); // { a: 1, b: 2, c: 3, d:4 }
- * console.log(order); // [1, 4, 2, 3]
- * });
- *
- */
- var parallel = createParallel(arrayEachFunc, baseEachFunc);
- /**
- * @memberof async
- * @namespace applyEach
- */
- var applyEach = createApplyEach(map);
- /**
- * @memberof async
- * @namespace applyEachSeries
- */
- var applyEachSeries = createApplyEach(mapSeries);
- /**
- * @memberof async
- * @namespace log
- */
- var log = createLogger('log');
- /**
- * @memberof async
- * @namespace dir
- */
- var dir = createLogger('dir');
- /**
- * @version 2.6.2
- * @namespace async
- */
- var index = {
- VERSION: '2.6.2',
- // Collections
- each: each,
- eachSeries: eachSeries,
- eachLimit: eachLimit,
- forEach: each,
- forEachSeries: eachSeries,
- forEachLimit: eachLimit,
- eachOf: each,
- eachOfSeries: eachSeries,
- eachOfLimit: eachLimit,
- forEachOf: each,
- forEachOfSeries: eachSeries,
- forEachOfLimit: eachLimit,
- map: map,
- mapSeries: mapSeries,
- mapLimit: mapLimit,
- mapValues: mapValues,
- mapValuesSeries: mapValuesSeries,
- mapValuesLimit: mapValuesLimit,
- filter: filter,
- filterSeries: filterSeries,
- filterLimit: filterLimit,
- select: filter,
- selectSeries: filterSeries,
- selectLimit: filterLimit,
- reject: reject,
- rejectSeries: rejectSeries,
- rejectLimit: rejectLimit,
- detect: detect,
- detectSeries: detectSeries,
- detectLimit: detectLimit,
- find: detect,
- findSeries: detectSeries,
- findLimit: detectLimit,
- pick: pick,
- pickSeries: pickSeries,
- pickLimit: pickLimit,
- omit: omit,
- omitSeries: omitSeries,
- omitLimit: omitLimit,
- reduce: reduce,
- inject: reduce,
- foldl: reduce,
- reduceRight: reduceRight,
- foldr: reduceRight,
- transform: transform,
- transformSeries: transformSeries,
- transformLimit: transformLimit,
- sortBy: sortBy,
- sortBySeries: sortBySeries,
- sortByLimit: sortByLimit,
- some: some,
- someSeries: someSeries,
- someLimit: someLimit,
- any: some,
- anySeries: someSeries,
- anyLimit: someLimit,
- every: every,
- everySeries: everySeries,
- everyLimit: everyLimit,
- all: every,
- allSeries: everySeries,
- allLimit: everyLimit,
- concat: concat,
- concatSeries: concatSeries,
- concatLimit: concatLimit,
- groupBy: groupBy,
- groupBySeries: groupBySeries,
- groupByLimit: groupByLimit,
- // Control Flow
- parallel: parallel,
- series: series,
- parallelLimit: parallelLimit,
- tryEach: tryEach,
- waterfall: waterfall,
- angelFall: angelFall,
- angelfall: angelFall,
- whilst: whilst,
- doWhilst: doWhilst,
- until: until,
- doUntil: doUntil,
- during: during,
- doDuring: doDuring,
- forever: forever,
- compose: compose,
- seq: seq,
- applyEach: applyEach,
- applyEachSeries: applyEachSeries,
- queue: queue,
- priorityQueue: priorityQueue,
- cargo: cargo,
- auto: auto,
- autoInject: autoInject,
- retry: retry,
- retryable: retryable,
- iterator: iterator,
- times: times,
- timesSeries: timesSeries,
- timesLimit: timesLimit,
- race: race,
- // Utils
- apply: apply,
- nextTick: asyncNextTick,
- setImmediate: asyncSetImmediate,
- memoize: memoize,
- unmemoize: unmemoize,
- ensureAsync: ensureAsync,
- constant: constant,
- asyncify: asyncify,
- wrapSync: asyncify,
- log: log,
- dir: dir,
- reflect: reflect,
- reflectAll: reflectAll,
- timeout: timeout,
- createLogger: createLogger,
- // Mode
- safe: safe,
- fast: fast
- };
- exports['default'] = index;
- baseEachSync(
- index,
- function(func, key) {
- exports[key] = func;
- },
- nativeKeys(index)
- );
- /**
- * @private
- */
- function createImmediate(safeMode) {
- var delay = function delay(fn) {
- var args = slice(arguments, 1);
- setTimeout(function() {
- fn.apply(null, args);
- });
- };
- asyncSetImmediate = typeof setImmediate === func ? setImmediate : delay;
- if (typeof process === obj && typeof process.nextTick === func) {
- nextTick = /^v0.10/.test(process.version) ? asyncSetImmediate : process.nextTick;
- asyncNextTick = /^v0/.test(process.version) ? asyncSetImmediate : process.nextTick;
- } else {
- asyncNextTick = nextTick = asyncSetImmediate;
- }
- if (safeMode === false) {
- nextTick = function(cb) {
- cb();
- };
- }
- }
- /* sync functions based on lodash */
- /**
- * Converts `arguments` to an array.
- *
- * @private
- * @param {Array} array = The array to slice.
- */
- function createArray(array) {
- var index = -1;
- var size = array.length;
- var result = Array(size);
- while (++index < size) {
- result[index] = array[index];
- }
- return result;
- }
- /**
- * Create an array from `start`
- *
- * @private
- * @param {Array} array - The array to slice.
- * @param {number} start - The start position.
- */
- function slice(array, start) {
- var end = array.length;
- var index = -1;
- var size = end - start;
- if (size <= 0) {
- return [];
- }
- var result = Array(size);
- while (++index < size) {
- result[index] = array[index + start];
- }
- return result;
- }
- /**
- * @private
- * @param {Object} object
- */
- function objectClone(object) {
- var keys = nativeKeys(object);
- var size = keys.length;
- var index = -1;
- var result = {};
- while (++index < size) {
- var key = keys[index];
- result[key] = object[key];
- }
- return result;
- }
- /**
- * Create an array with all falsey values removed.
- *
- * @private
- * @param {Array} array - The array to compact.
- */
- function compact(array) {
- var index = -1;
- var size = array.length;
- var result = [];
- while (++index < size) {
- var value = array[index];
- if (value) {
- result[result.length] = value;
- }
- }
- return result;
- }
- /**
- * Create an array of reverse sequence.
- *
- * @private
- * @param {Array} array - The array to reverse.
- */
- function reverse(array) {
- var index = -1;
- var size = array.length;
- var result = Array(size);
- var resIndex = size;
- while (++index < size) {
- result[--resIndex] = array[index];
- }
- return result;
- }
- /**
- * Checks if key exists in object property.
- *
- * @private
- * @param {Object} object - The object to inspect.
- * @param {string} key - The key to check.
- */
- function has(object, key) {
- return object.hasOwnProperty(key);
- }
- /**
- * Check if target exists in array.
- * @private
- * @param {Array} array
- * @param {*} target
- */
- function notInclude(array, target) {
- var index = -1;
- var size = array.length;
- while (++index < size) {
- if (array[index] === target) {
- return false;
- }
- }
- return true;
- }
- /**
- * @private
- * @param {Array} array - The array to iterate over.
- * @param {Function} iterator - The function invoked per iteration.
- */
- function arrayEachSync(array, iterator) {
- var index = -1;
- var size = array.length;
- while (++index < size) {
- iterator(array[index], index);
- }
- return array;
- }
- /**
- * @private
- * @param {Object} object - The object to iterate over.
- * @param {Function} iterator - The function invoked per iteration.
- * @param {Array} keys
- */
- function baseEachSync(object, iterator, keys) {
- var index = -1;
- var size = keys.length;
- while (++index < size) {
- var key = keys[index];
- iterator(object[key], key);
- }
- return object;
- }
- /**
- * @private
- * @param {number} n
- * @param {Function} iterator
- */
- function timesSync(n, iterator) {
- var index = -1;
- while (++index < n) {
- iterator(index);
- }
- }
- /**
- * @private
- * @param {Array} array
- * @param {number[]} criteria
- */
- function sortByCriteria(array, criteria) {
- var l = array.length;
- var indices = Array(l);
- var i;
- for (i = 0; i < l; i++) {
- indices[i] = i;
- }
- quickSort(criteria, 0, l - 1, indices);
- var result = Array(l);
- for (var n = 0; n < l; n++) {
- i = indices[n];
- result[n] = i === undefined ? array[n] : array[i];
- }
- return result;
- }
- function partition(array, i, j, mid, indices) {
- var l = i;
- var r = j;
- while (l <= r) {
- i = l;
- while (l < r && array[l] < mid) {
- l++;
- }
- while (r >= i && array[r] >= mid) {
- r--;
- }
- if (l > r) {
- break;
- }
- swap(array, indices, l++, r--);
- }
- return l;
- }
- function swap(array, indices, l, r) {
- var n = array[l];
- array[l] = array[r];
- array[r] = n;
- var i = indices[l];
- indices[l] = indices[r];
- indices[r] = i;
- }
- function quickSort(array, i, j, indices) {
- if (i === j) {
- return;
- }
- var k = i;
- while (++k <= j && array[i] === array[k]) {
- var l = k - 1;
- if (indices[l] > indices[k]) {
- var index = indices[l];
- indices[l] = indices[k];
- indices[k] = index;
- }
- }
- if (k > j) {
- return;
- }
- var p = array[i] > array[k] ? i : k;
- k = partition(array, i, j, array[p], indices);
- quickSort(array, i, k - 1, indices);
- quickSort(array, k, j, indices);
- }
- /**
- * @Private
- */
- function makeConcatResult(array) {
- var result = [];
- arrayEachSync(array, function(value) {
- if (value === noop) {
- return;
- }
- if (isArray(value)) {
- nativePush.apply(result, value);
- } else {
- result.push(value);
- }
- });
- return result;
- }
- /* async functions */
- /**
- * @private
- */
- function arrayEach(array, iterator, callback) {
- var index = -1;
- var size = array.length;
- if (iterator.length === 3) {
- while (++index < size) {
- iterator(array[index], index, onlyOnce(callback));
- }
- } else {
- while (++index < size) {
- iterator(array[index], onlyOnce(callback));
- }
- }
- }
- /**
- * @private
- */
- function baseEach(object, iterator, callback, keys) {
- var key;
- var index = -1;
- var size = keys.length;
- if (iterator.length === 3) {
- while (++index < size) {
- key = keys[index];
- iterator(object[key], key, onlyOnce(callback));
- }
- } else {
- while (++index < size) {
- iterator(object[keys[index]], onlyOnce(callback));
- }
- }
- }
- /**
- * @private
- */
- function symbolEach(collection, iterator, callback) {
- var iter = collection[iteratorSymbol]();
- var index = 0;
- var item;
- if (iterator.length === 3) {
- while ((item = iter.next()).done === false) {
- iterator(item.value, index++, onlyOnce(callback));
- }
- } else {
- while ((item = iter.next()).done === false) {
- index++;
- iterator(item.value, onlyOnce(callback));
- }
- }
- return index;
- }
- /**
- * @private
- */
- function arrayEachResult(array, result, iterator, callback) {
- var index = -1;
- var size = array.length;
- if (iterator.length === 4) {
- while (++index < size) {
- iterator(result, array[index], index, onlyOnce(callback));
- }
- } else {
- while (++index < size) {
- iterator(result, array[index], onlyOnce(callback));
- }
- }
- }
- /**
- * @private
- */
- function baseEachResult(object, result, iterator, callback, keys) {
- var key;
- var index = -1;
- var size = keys.length;
- if (iterator.length === 4) {
- while (++index < size) {
- key = keys[index];
- iterator(result, object[key], key, onlyOnce(callback));
- }
- } else {
- while (++index < size) {
- iterator(result, object[keys[index]], onlyOnce(callback));
- }
- }
- }
- /**
- * @private
- */
- function symbolEachResult(collection, result, iterator, callback) {
- var item;
- var index = 0;
- var iter = collection[iteratorSymbol]();
- if (iterator.length === 4) {
- while ((item = iter.next()).done === false) {
- iterator(result, item.value, index++, onlyOnce(callback));
- }
- } else {
- while ((item = iter.next()).done === false) {
- index++;
- iterator(result, item.value, onlyOnce(callback));
- }
- }
- return index;
- }
- /**
- * @private
- */
- function arrayEachFunc(array, createCallback) {
- var index = -1;
- var size = array.length;
- while (++index < size) {
- array[index](createCallback(index));
- }
- }
- /**
- * @private
- */
- function baseEachFunc(object, createCallback, keys) {
- var key;
- var index = -1;
- var size = keys.length;
- while (++index < size) {
- key = keys[index];
- object[key](createCallback(key));
- }
- }
- /**
- * @private
- */
- function arrayEachIndex(array, iterator, createCallback) {
- var index = -1;
- var size = array.length;
- if (iterator.length === 3) {
- while (++index < size) {
- iterator(array[index], index, createCallback(index));
- }
- } else {
- while (++index < size) {
- iterator(array[index], createCallback(index));
- }
- }
- }
- /**
- * @private
- */
- function baseEachIndex(object, iterator, createCallback, keys) {
- var key;
- var index = -1;
- var size = keys.length;
- if (iterator.length === 3) {
- while (++index < size) {
- key = keys[index];
- iterator(object[key], key, createCallback(index));
- }
- } else {
- while (++index < size) {
- iterator(object[keys[index]], createCallback(index));
- }
- }
- }
- /**
- * @private
- */
- function symbolEachIndex(collection, iterator, createCallback) {
- var item;
- var index = 0;
- var iter = collection[iteratorSymbol]();
- if (iterator.length === 3) {
- while ((item = iter.next()).done === false) {
- iterator(item.value, index, createCallback(index++));
- }
- } else {
- while ((item = iter.next()).done === false) {
- iterator(item.value, createCallback(index++));
- }
- }
- return index;
- }
- /**
- * @private
- */
- function baseEachKey(object, iterator, createCallback, keys) {
- var key;
- var index = -1;
- var size = keys.length;
- if (iterator.length === 3) {
- while (++index < size) {
- key = keys[index];
- iterator(object[key], key, createCallback(key));
- }
- } else {
- while (++index < size) {
- key = keys[index];
- iterator(object[key], createCallback(key));
- }
- }
- }
- /**
- * @private
- */
- function symbolEachKey(collection, iterator, createCallback) {
- var item;
- var index = 0;
- var iter = collection[iteratorSymbol]();
- if (iterator.length === 3) {
- while ((item = iter.next()).done === false) {
- iterator(item.value, index, createCallback(index++));
- }
- } else {
- while ((item = iter.next()).done === false) {
- iterator(item.value, createCallback(index++));
- }
- }
- return index;
- }
- /**
- * @private
- */
- function arrayEachValue(array, iterator, createCallback) {
- var value;
- var index = -1;
- var size = array.length;
- if (iterator.length === 3) {
- while (++index < size) {
- value = array[index];
- iterator(value, index, createCallback(value));
- }
- } else {
- while (++index < size) {
- value = array[index];
- iterator(value, createCallback(value));
- }
- }
- }
- /**
- * @private
- */
- function baseEachValue(object, iterator, createCallback, keys) {
- var key, value;
- var index = -1;
- var size = keys.length;
- if (iterator.length === 3) {
- while (++index < size) {
- key = keys[index];
- value = object[key];
- iterator(value, key, createCallback(value));
- }
- } else {
- while (++index < size) {
- value = object[keys[index]];
- iterator(value, createCallback(value));
- }
- }
- }
- /**
- * @private
- */
- function symbolEachValue(collection, iterator, createCallback) {
- var value, item;
- var index = 0;
- var iter = collection[iteratorSymbol]();
- if (iterator.length === 3) {
- while ((item = iter.next()).done === false) {
- value = item.value;
- iterator(value, index++, createCallback(value));
- }
- } else {
- while ((item = iter.next()).done === false) {
- index++;
- value = item.value;
- iterator(value, createCallback(value));
- }
- }
- return index;
- }
- /**
- * @private
- */
- function arrayEachIndexValue(array, iterator, createCallback) {
- var value;
- var index = -1;
- var size = array.length;
- if (iterator.length === 3) {
- while (++index < size) {
- value = array[index];
- iterator(value, index, createCallback(index, value));
- }
- } else {
- while (++index < size) {
- value = array[index];
- iterator(value, createCallback(index, value));
- }
- }
- }
- /**
- * @private
- */
- function baseEachIndexValue(object, iterator, createCallback, keys) {
- var key, value;
- var index = -1;
- var size = keys.length;
- if (iterator.length === 3) {
- while (++index < size) {
- key = keys[index];
- value = object[key];
- iterator(value, key, createCallback(index, value));
- }
- } else {
- while (++index < size) {
- value = object[keys[index]];
- iterator(value, createCallback(index, value));
- }
- }
- }
- /**
- * @private
- */
- function symbolEachIndexValue(collection, iterator, createCallback) {
- var value, item;
- var index = 0;
- var iter = collection[iteratorSymbol]();
- if (iterator.length === 3) {
- while ((item = iter.next()).done === false) {
- value = item.value;
- iterator(value, index, createCallback(index++, value));
- }
- } else {
- while ((item = iter.next()).done === false) {
- value = item.value;
- iterator(value, createCallback(index++, value));
- }
- }
- return index;
- }
- /**
- * @private
- */
- function baseEachKeyValue(object, iterator, createCallback, keys) {
- var key, value;
- var index = -1;
- var size = keys.length;
- if (iterator.length === 3) {
- while (++index < size) {
- key = keys[index];
- value = object[key];
- iterator(value, key, createCallback(key, value));
- }
- } else {
- while (++index < size) {
- key = keys[index];
- value = object[key];
- iterator(value, createCallback(key, value));
- }
- }
- }
- /**
- * @private
- */
- function symbolEachKeyValue(collection, iterator, createCallback) {
- var value, item;
- var index = 0;
- var iter = collection[iteratorSymbol]();
- if (iterator.length === 3) {
- while ((item = iter.next()).done === false) {
- value = item.value;
- iterator(value, index, createCallback(index++, value));
- }
- } else {
- while ((item = iter.next()).done === false) {
- value = item.value;
- iterator(value, createCallback(index++, value));
- }
- }
- return index;
- }
- /**
- * @private
- * @param {Function} func
- */
- function onlyOnce(func) {
- return function(err, res) {
- var fn = func;
- func = throwError;
- fn(err, res);
- };
- }
- /**
- * @private
- * @param {Function} func
- */
- function once(func) {
- return function(err, res) {
- var fn = func;
- func = noop;
- fn(err, res);
- };
- }
- /**
- * @private
- * @param {Function} arrayEach
- * @param {Function} baseEach
- */
- function createEach(arrayEach, baseEach, symbolEach) {
- return function each(collection, iterator, callback) {
- callback = once(callback || noop);
- var size, keys;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- arrayEach(collection, iterator, done);
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = symbolEach(collection, iterator, done);
- size && size === completed && callback(null);
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- baseEach(collection, iterator, done, keys);
- }
- if (!size) {
- callback(null);
- }
- function done(err, bool) {
- if (err) {
- callback = once(callback);
- callback(err);
- } else if (++completed === size) {
- callback(null);
- } else if (bool === false) {
- callback = once(callback);
- callback(null);
- }
- }
- };
- }
- /**
- * @private
- * @param {Function} arrayEach
- * @param {Function} baseEach
- * @param {Function} symbolEach
- */
- function createMap(arrayEach, baseEach, symbolEach, useArray) {
- var init, clone;
- if (useArray) {
- init = Array;
- clone = createArray;
- } else {
- init = function() {
- return {};
- };
- clone = objectClone;
- }
- return function(collection, iterator, callback) {
- callback = callback || noop;
- var size, keys, result;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- result = init(size);
- arrayEach(collection, iterator, createCallback);
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- // TODO: size could be changed
- result = init(0);
- size = symbolEach(collection, iterator, createCallback);
- size && size === completed && callback(null, result);
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- result = init(size);
- baseEach(collection, iterator, createCallback, keys);
- }
- if (!size) {
- callback(null, init());
- }
- function createCallback(key) {
- return function done(err, res) {
- if (key === null) {
- throwError();
- }
- if (err) {
- key = null;
- callback = once(callback);
- callback(err, clone(result));
- return;
- }
- result[key] = res;
- key = null;
- if (++completed === size) {
- callback(null, result);
- }
- };
- }
- };
- }
- /**
- * @private
- * @param {Function} arrayEach
- * @param {Function} baseEach
- * @param {Function} symbolEach
- * @param {boolean} bool
- */
- function createFilter(arrayEach, baseEach, symbolEach, bool) {
- return function(collection, iterator, callback) {
- callback = callback || noop;
- var size, keys, result;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- result = Array(size);
- arrayEach(collection, iterator, createCallback);
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- result = [];
- size = symbolEach(collection, iterator, createCallback);
- size && size === completed && callback(null, compact(result));
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- result = Array(size);
- baseEach(collection, iterator, createCallback, keys);
- }
- if (!size) {
- return callback(null, []);
- }
- function createCallback(index, value) {
- return function done(err, res) {
- if (index === null) {
- throwError();
- }
- if (err) {
- index = null;
- callback = once(callback);
- callback(err);
- return;
- }
- if (!!res === bool) {
- result[index] = value;
- }
- index = null;
- if (++completed === size) {
- callback(null, compact(result));
- }
- };
- }
- };
- }
- /**
- * @private
- * @param {boolean} bool
- */
- function createFilterSeries(bool) {
- return function(collection, iterator, callback) {
- callback = onlyOnce(callback || noop);
- var size, key, value, keys, iter, item, iterate;
- var sync = false;
- var completed = 0;
- var result = [];
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null, []);
- }
- iterate();
- function arrayIterator() {
- value = collection[completed];
- iterator(value, done);
- }
- function arrayIteratorWithIndex() {
- value = collection[completed];
- iterator(value, completed, done);
- }
- function symbolIterator() {
- item = iter.next();
- value = item.value;
- item.done ? callback(null, result) : iterator(value, done);
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- value = item.value;
- item.done ? callback(null, result) : iterator(value, completed, done);
- }
- function objectIterator() {
- key = keys[completed];
- value = collection[key];
- iterator(value, done);
- }
- function objectIteratorWithKey() {
- key = keys[completed];
- value = collection[key];
- iterator(value, key, done);
- }
- function done(err, res) {
- if (err) {
- callback(err);
- return;
- }
- if (!!res === bool) {
- result[result.length] = value;
- }
- if (++completed === size) {
- iterate = throwError;
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- };
- }
- /**
- * @private
- * @param {boolean} bool
- */
- function createFilterLimit(bool) {
- return function(collection, limit, iterator, callback) {
- callback = callback || noop;
- var size, index, key, value, keys, iter, item, iterate, result;
- var sync = false;
- var started = 0;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- result = [];
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null, []);
- }
- result = result || Array(size);
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- index = started++;
- if (index < size) {
- value = collection[index];
- iterator(value, createCallback(value, index));
- }
- }
- function arrayIteratorWithIndex() {
- index = started++;
- if (index < size) {
- value = collection[index];
- iterator(value, index, createCallback(value, index));
- }
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done === false) {
- value = item.value;
- iterator(value, createCallback(value, started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, compact(result));
- }
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done === false) {
- value = item.value;
- iterator(value, started, createCallback(value, started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, compact(result));
- }
- }
- function objectIterator() {
- index = started++;
- if (index < size) {
- value = collection[keys[index]];
- iterator(value, createCallback(value, index));
- }
- }
- function objectIteratorWithKey() {
- index = started++;
- if (index < size) {
- key = keys[index];
- value = collection[key];
- iterator(value, key, createCallback(value, index));
- }
- }
- function createCallback(value, index) {
- return function(err, res) {
- if (index === null) {
- throwError();
- }
- if (err) {
- index = null;
- iterate = noop;
- callback = once(callback);
- callback(err);
- return;
- }
- if (!!res === bool) {
- result[index] = value;
- }
- index = null;
- if (++completed === size) {
- callback = onlyOnce(callback);
- callback(null, compact(result));
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- };
- }
- };
- }
- /**
- * @memberof async
- * @namespace eachSeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done();
- * }, num * 10);
- * };
- * async.eachSeries(array, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done();
- * }, num * 10);
- * };
- * async.eachSeries(array, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [[1, 0], [3, 1], [2, 2]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done();
- * }, num * 10);
- * };
- * async.eachSeries(object, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done();
- * }, num * 10);
- * };
- * async.eachSeries(object, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'b']]
- * });
- *
- * @example
- *
- * // break
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num !== 3);
- * }, num * 10);
- * };
- * async.eachSeries(array, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [1, 3]
- * });
- */
- function eachSeries(collection, iterator, callback) {
- callback = onlyOnce(callback || noop);
- var size, key, keys, iter, item, iterate;
- var sync = false;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null);
- }
- iterate();
- function arrayIterator() {
- iterator(collection[completed], done);
- }
- function arrayIteratorWithIndex() {
- iterator(collection[completed], completed, done);
- }
- function symbolIterator() {
- item = iter.next();
- item.done ? callback(null) : iterator(item.value, done);
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- item.done ? callback(null) : iterator(item.value, completed, done);
- }
- function objectIterator() {
- iterator(collection[keys[completed]], done);
- }
- function objectIteratorWithKey() {
- key = keys[completed];
- iterator(collection[key], key, done);
- }
- function done(err, bool) {
- if (err) {
- callback(err);
- } else if (++completed === size || bool === false) {
- iterate = throwError;
- callback(null);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace eachLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done();
- * }, num * 10);
- * };
- * async.eachLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done();
- * }, num * 10);
- * };
- * async.eachLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done();
- * }, num * 10);
- * };
- * async.eachLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done();
- * }, num * 10);
- * };
- * async.eachLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
- * });
- *
- * @example
- *
- * // break
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num !== 5);
- * }, num * 10);
- * };
- * async.eachLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // undefined
- * console.log(order); // [1, 3, 5]
- * });
- *
- */
- function eachLimit(collection, limit, iterator, callback) {
- callback = callback || noop;
- var size, index, key, keys, iter, item, iterate;
- var sync = false;
- var started = 0;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- } else {
- return callback(null);
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null);
- }
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- if (started < size) {
- iterator(collection[started++], done);
- }
- }
- function arrayIteratorWithIndex() {
- index = started++;
- if (index < size) {
- iterator(collection[index], index, done);
- }
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done === false) {
- started++;
- iterator(item.value, done);
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null);
- }
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done === false) {
- iterator(item.value, started++, done);
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null);
- }
- }
- function objectIterator() {
- if (started < size) {
- iterator(collection[keys[started++]], done);
- }
- }
- function objectIteratorWithKey() {
- index = started++;
- if (index < size) {
- key = keys[index];
- iterator(collection[key], key, done);
- }
- }
- function done(err, bool) {
- if (err || bool === false) {
- iterate = noop;
- callback = once(callback);
- callback(err);
- } else if (++completed === size) {
- iterator = noop;
- iterate = throwError;
- callback = onlyOnce(callback);
- callback(null);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace mapSeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapSeries(array, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2];
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapSeries(array, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2]
- * console.log(order); // [[1, 0], [3, 1], [2, 2]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapSeries(object, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2]
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapSeries(object, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2]
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
- * });
- *
- */
- function mapSeries(collection, iterator, callback) {
- callback = callback || noop;
- var size, key, keys, iter, item, result, iterate;
- var sync = false;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- result = [];
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null, []);
- }
- result = result || Array(size);
- iterate();
- function arrayIterator() {
- iterator(collection[completed], done);
- }
- function arrayIteratorWithIndex() {
- iterator(collection[completed], completed, done);
- }
- function symbolIterator() {
- item = iter.next();
- item.done ? callback(null, result) : iterator(item.value, done);
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- item.done ? callback(null, result) : iterator(item.value, completed, done);
- }
- function objectIterator() {
- iterator(collection[keys[completed]], done);
- }
- function objectIteratorWithKey() {
- key = keys[completed];
- iterator(collection[key], key, done);
- }
- function done(err, res) {
- if (err) {
- iterate = throwError;
- callback = onlyOnce(callback);
- callback(err, createArray(result));
- return;
- }
- result[completed] = res;
- if (++completed === size) {
- iterate = throwError;
- callback(null, result);
- callback = throwError;
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace mapLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [1, 5, 3, 4, 2]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [1, 5, 3, 4, 2]
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // [1, 5, 3, 4, 2]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // [1, 5, 3, 4, 2]
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
- * });
- *
- */
- function mapLimit(collection, limit, iterator, callback) {
- callback = callback || noop;
- var size, index, key, keys, iter, item, result, iterate;
- var sync = false;
- var started = 0;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- result = [];
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null, []);
- }
- result = result || Array(size);
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- index = started++;
- if (index < size) {
- iterator(collection[index], createCallback(index));
- }
- }
- function arrayIteratorWithIndex() {
- index = started++;
- if (index < size) {
- iterator(collection[index], index, createCallback(index));
- }
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done === false) {
- iterator(item.value, createCallback(started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, result);
- }
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done === false) {
- iterator(item.value, started, createCallback(started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, result);
- }
- }
- function objectIterator() {
- index = started++;
- if (index < size) {
- iterator(collection[keys[index]], createCallback(index));
- }
- }
- function objectIteratorWithKey() {
- index = started++;
- if (index < size) {
- key = keys[index];
- iterator(collection[key], key, createCallback(index));
- }
- }
- function createCallback(index) {
- return function(err, res) {
- if (index === null) {
- throwError();
- }
- if (err) {
- index = null;
- iterate = noop;
- callback = once(callback);
- callback(err, createArray(result));
- return;
- }
- result[index] = res;
- index = null;
- if (++completed === size) {
- iterate = throwError;
- callback(null, result);
- callback = throwError;
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- };
- }
- }
- /**
- * @memberof async
- * @namespace mapValuesSeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValuesSeries(array, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 3, '2': 2 }
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValuesSeries(array, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 3, '2': 2 }
- * console.log(order); // [[1, 0], [3, 1], [2, 2]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValuesSeries(object, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 3, c: 2 }
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValuesSeries(object, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 3, c: 2 }
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
- * });
- *
- */
- function mapValuesSeries(collection, iterator, callback) {
- callback = callback || noop;
- var size, key, keys, iter, item, iterate;
- var sync = false;
- var result = {};
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null, result);
- }
- iterate();
- function arrayIterator() {
- key = completed;
- iterator(collection[completed], done);
- }
- function arrayIteratorWithIndex() {
- key = completed;
- iterator(collection[completed], completed, done);
- }
- function symbolIterator() {
- key = completed;
- item = iter.next();
- item.done ? callback(null, result) : iterator(item.value, done);
- }
- function symbolIteratorWithKey() {
- key = completed;
- item = iter.next();
- item.done ? callback(null, result) : iterator(item.value, completed, done);
- }
- function objectIterator() {
- key = keys[completed];
- iterator(collection[key], done);
- }
- function objectIteratorWithKey() {
- key = keys[completed];
- iterator(collection[key], key, done);
- }
- function done(err, res) {
- if (err) {
- iterate = throwError;
- callback = onlyOnce(callback);
- callback(err, objectClone(result));
- return;
- }
- result[key] = res;
- if (++completed === size) {
- iterate = throwError;
- callback(null, result);
- callback = throwError;
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace mapValuesLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValuesLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValuesLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValuesLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 5, c: 3, d: 4, e: 2 }
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.mapValuesLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 5, c: 3, d: 4, e: 2 }
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
- * });
- *
- */
- function mapValuesLimit(collection, limit, iterator, callback) {
- callback = callback || noop;
- var size, index, key, keys, iter, item, iterate;
- var sync = false;
- var result = {};
- var started = 0;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null, result);
- }
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- index = started++;
- if (index < size) {
- iterator(collection[index], createCallback(index));
- }
- }
- function arrayIteratorWithIndex() {
- index = started++;
- if (index < size) {
- iterator(collection[index], index, createCallback(index));
- }
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done === false) {
- iterator(item.value, createCallback(started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, result);
- }
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done === false) {
- iterator(item.value, started, createCallback(started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, result);
- }
- }
- function objectIterator() {
- index = started++;
- if (index < size) {
- key = keys[index];
- iterator(collection[key], createCallback(key));
- }
- }
- function objectIteratorWithKey() {
- index = started++;
- if (index < size) {
- key = keys[index];
- iterator(collection[key], key, createCallback(key));
- }
- }
- function createCallback(key) {
- return function(err, res) {
- if (key === null) {
- throwError();
- }
- if (err) {
- key = null;
- iterate = noop;
- callback = once(callback);
- callback(err, objectClone(result));
- return;
- }
- result[key] = res;
- key = null;
- if (++completed === size) {
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- };
- }
- }
- /**
- * @private
- * @param {Function} arrayEach
- * @param {Function} baseEach
- * @param {Function} symbolEach
- * @param {boolean} bool
- */
- function createDetect(arrayEach, baseEach, symbolEach, bool) {
- return function(collection, iterator, callback) {
- callback = callback || noop;
- var size, keys;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- arrayEach(collection, iterator, createCallback);
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = symbolEach(collection, iterator, createCallback);
- size && size === completed && callback(null);
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- baseEach(collection, iterator, createCallback, keys);
- }
- if (!size) {
- callback(null);
- }
- function createCallback(value) {
- var called = false;
- return function done(err, res) {
- if (called) {
- throwError();
- }
- called = true;
- if (err) {
- callback = once(callback);
- callback(err);
- } else if (!!res === bool) {
- callback = once(callback);
- callback(null, value);
- } else if (++completed === size) {
- callback(null);
- }
- };
- }
- };
- }
- /**
- * @private
- * @param {boolean} bool
- */
- function createDetectSeries(bool) {
- return function(collection, iterator, callback) {
- callback = onlyOnce(callback || noop);
- var size, key, value, keys, iter, item, iterate;
- var sync = false;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null);
- }
- iterate();
- function arrayIterator() {
- value = collection[completed];
- iterator(value, done);
- }
- function arrayIteratorWithIndex() {
- value = collection[completed];
- iterator(value, completed, done);
- }
- function symbolIterator() {
- item = iter.next();
- value = item.value;
- item.done ? callback(null) : iterator(value, done);
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- value = item.value;
- item.done ? callback(null) : iterator(value, completed, done);
- }
- function objectIterator() {
- value = collection[keys[completed]];
- iterator(value, done);
- }
- function objectIteratorWithKey() {
- key = keys[completed];
- value = collection[key];
- iterator(value, key, done);
- }
- function done(err, res) {
- if (err) {
- callback(err);
- } else if (!!res === bool) {
- iterate = throwError;
- callback(null, value);
- } else if (++completed === size) {
- iterate = throwError;
- callback(null);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- };
- }
- /**
- * @private
- * @param {boolean} bool
- */
- function createDetectLimit(bool) {
- return function(collection, limit, iterator, callback) {
- callback = callback || noop;
- var size, index, key, value, keys, iter, item, iterate;
- var sync = false;
- var started = 0;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null);
- }
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- index = started++;
- if (index < size) {
- value = collection[index];
- iterator(value, createCallback(value));
- }
- }
- function arrayIteratorWithIndex() {
- index = started++;
- if (index < size) {
- value = collection[index];
- iterator(value, index, createCallback(value));
- }
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done === false) {
- started++;
- value = item.value;
- iterator(value, createCallback(value));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null);
- }
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done === false) {
- value = item.value;
- iterator(value, started++, createCallback(value));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null);
- }
- }
- function objectIterator() {
- index = started++;
- if (index < size) {
- value = collection[keys[index]];
- iterator(value, createCallback(value));
- }
- }
- function objectIteratorWithKey() {
- if (started < size) {
- key = keys[started++];
- value = collection[key];
- iterator(value, key, createCallback(value));
- }
- }
- function createCallback(value) {
- var called = false;
- return function(err, res) {
- if (called) {
- throwError();
- }
- called = true;
- if (err) {
- iterate = noop;
- callback = once(callback);
- callback(err);
- } else if (!!res === bool) {
- iterate = noop;
- callback = once(callback);
- callback(null, value);
- } else if (++completed === size) {
- callback(null);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- };
- }
- };
- }
- /**
- * @private
- * @param {Function} arrayEach
- * @param {Function} baseEach
- * @param {Function} symbolEach
- * @param {boolean} bool
- */
- function createPick(arrayEach, baseEach, symbolEach, bool) {
- return function(collection, iterator, callback) {
- callback = callback || noop;
- var size, keys;
- var completed = 0;
- var result = {};
- if (isArray(collection)) {
- size = collection.length;
- arrayEach(collection, iterator, createCallback);
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = symbolEach(collection, iterator, createCallback);
- size && size === completed && callback(null, result);
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- baseEach(collection, iterator, createCallback, keys);
- }
- if (!size) {
- return callback(null, {});
- }
- function createCallback(key, value) {
- return function done(err, res) {
- if (key === null) {
- throwError();
- }
- if (err) {
- key = null;
- callback = once(callback);
- callback(err, objectClone(result));
- return;
- }
- if (!!res === bool) {
- result[key] = value;
- }
- key = null;
- if (++completed === size) {
- callback(null, result);
- }
- };
- }
- };
- }
- /**
- * @private
- * @param {boolean} bool
- */
- function createPickSeries(bool) {
- return function(collection, iterator, callback) {
- callback = onlyOnce(callback || noop);
- var size, key, value, keys, iter, item, iterate;
- var sync = false;
- var result = {};
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null, {});
- }
- iterate();
- function arrayIterator() {
- key = completed;
- value = collection[completed];
- iterator(value, done);
- }
- function arrayIteratorWithIndex() {
- key = completed;
- value = collection[completed];
- iterator(value, completed, done);
- }
- function symbolIterator() {
- key = completed;
- item = iter.next();
- value = item.value;
- item.done ? callback(null, result) : iterator(value, done);
- }
- function symbolIteratorWithKey() {
- key = completed;
- item = iter.next();
- value = item.value;
- item.done ? callback(null, result) : iterator(value, key, done);
- }
- function objectIterator() {
- key = keys[completed];
- value = collection[key];
- iterator(value, done);
- }
- function objectIteratorWithKey() {
- key = keys[completed];
- value = collection[key];
- iterator(value, key, done);
- }
- function done(err, res) {
- if (err) {
- callback(err, result);
- return;
- }
- if (!!res === bool) {
- result[key] = value;
- }
- if (++completed === size) {
- iterate = throwError;
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- };
- }
- /**
- * @private
- * @param {boolean} bool
- */
- function createPickLimit(bool) {
- return function(collection, limit, iterator, callback) {
- callback = callback || noop;
- var size, index, key, value, keys, iter, item, iterate;
- var sync = false;
- var result = {};
- var started = 0;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null, {});
- }
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- index = started++;
- if (index < size) {
- value = collection[index];
- iterator(value, createCallback(value, index));
- }
- }
- function arrayIteratorWithIndex() {
- index = started++;
- if (index < size) {
- value = collection[index];
- iterator(value, index, createCallback(value, index));
- }
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done === false) {
- value = item.value;
- iterator(value, createCallback(value, started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, result);
- }
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done === false) {
- value = item.value;
- iterator(value, started, createCallback(value, started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, result);
- }
- }
- function objectIterator() {
- if (started < size) {
- key = keys[started++];
- value = collection[key];
- iterator(value, createCallback(value, key));
- }
- }
- function objectIteratorWithKey() {
- if (started < size) {
- key = keys[started++];
- value = collection[key];
- iterator(value, key, createCallback(value, key));
- }
- }
- function createCallback(value, key) {
- return function(err, res) {
- if (key === null) {
- throwError();
- }
- if (err) {
- key = null;
- iterate = noop;
- callback = once(callback);
- callback(err, objectClone(result));
- return;
- }
- if (!!res === bool) {
- result[key] = value;
- }
- key = null;
- if (++completed === size) {
- iterate = throwError;
- callback = onlyOnce(callback);
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- };
- }
- };
- }
- /**
- * @memberof async
- * @namespace reduce
- * @param {Array|Object} collection
- * @param {*} result
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var collection = [1, 3, 2, 4];
- * var iterator = function(result, num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, result + num);
- * }, num * 10);
- * };
- * async.reduce(collection, 0, iterator, function(err, res) {
- * console.log(res); // 10
- * console.log(order); // [1, 3, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var collection = [1, 3, 2, 4];
- * var iterator = function(result, num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, result + num);
- * }, num * 10);
- * };
- * async.reduce(collection, '', iterator, function(err, res) {
- * console.log(res); // '1324'
- * console.log(order); // [[1, 0], [3, 1], [2, 2], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(result, num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, result + num);
- * }, num * 10);
- * };
- * async.reduce(collection, '', iterator, function(err, res) {
- * console.log(res); // '1324'
- * console.log(order); // [1, 3, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(result, num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, result + num);
- * }, num * 10);
- * };
- * async.reduce(collection, 0, iterator, function(err, res) {
- * console.log(res); // 10
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'b'], [4, 'd']]
- * });
- *
- */
- function reduce(collection, result, iterator, callback) {
- callback = onlyOnce(callback || noop);
- var size, key, keys, iter, item, iterate;
- var sync = false;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 4 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null, result);
- }
- iterate(result);
- function arrayIterator(result) {
- iterator(result, collection[completed], done);
- }
- function arrayIteratorWithIndex(result) {
- iterator(result, collection[completed], completed, done);
- }
- function symbolIterator(result) {
- item = iter.next();
- item.done ? callback(null, result) : iterator(result, item.value, done);
- }
- function symbolIteratorWithKey(result) {
- item = iter.next();
- item.done ? callback(null, result) : iterator(result, item.value, completed, done);
- }
- function objectIterator(result) {
- iterator(result, collection[keys[completed]], done);
- }
- function objectIteratorWithKey(result) {
- key = keys[completed];
- iterator(result, collection[key], key, done);
- }
- function done(err, result) {
- if (err) {
- callback(err, result);
- } else if (++completed === size) {
- iterator = throwError;
- callback(null, result);
- } else if (sync) {
- nextTick(function() {
- iterate(result);
- });
- } else {
- sync = true;
- iterate(result);
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace reduceRight
- * @param {Array|Object} collection
- * @param {*} result
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var collection = [1, 3, 2, 4];
- * var iterator = function(result, num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, result + num);
- * }, num * 10);
- * };
- * async.reduceRight(collection, 0, iterator, function(err, res) {
- * console.log(res); // 10
- * console.log(order); // [4, 2, 3, 1]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var collection = [1, 3, 2, 4];
- * var iterator = function(result, num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, result + num);
- * }, num * 10);
- * };
- * async.reduceRight(collection, '', iterator, function(err, res) {
- * console.log(res); // '4231'
- * console.log(order); // [[4, 3], [2, 2], [3, 1], [1, 0]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(result, num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, result + num);
- * }, num * 10);
- * };
- * async.reduceRight(collection, '', iterator, function(err, res) {
- * console.log(res); // '4231'
- * console.log(order); // [4, 2, 3, 1]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(result, num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, result + num);
- * }, num * 10);
- * };
- * async.reduceRight(collection, 0, iterator, function(err, res) {
- * console.log(res); // 10
- * console.log(order); // [[4, 3], [2, 2], [3, 1], [1, 0]]
- * });
- *
- */
- function reduceRight(collection, result, iterator, callback) {
- callback = onlyOnce(callback || noop);
- var resIndex, index, key, keys, iter, item, col, iterate;
- var sync = false;
- if (isArray(collection)) {
- resIndex = collection.length;
- iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- col = [];
- iter = collection[iteratorSymbol]();
- index = -1;
- while ((item = iter.next()).done === false) {
- col[++index] = item.value;
- }
- collection = col;
- resIndex = col.length;
- iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- resIndex = keys.length;
- iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;
- }
- if (!resIndex) {
- return callback(null, result);
- }
- iterate(result);
- function arrayIterator(result) {
- iterator(result, collection[--resIndex], done);
- }
- function arrayIteratorWithIndex(result) {
- iterator(result, collection[--resIndex], resIndex, done);
- }
- function objectIterator(result) {
- iterator(result, collection[keys[--resIndex]], done);
- }
- function objectIteratorWithKey(result) {
- key = keys[--resIndex];
- iterator(result, collection[key], key, done);
- }
- function done(err, result) {
- if (err) {
- callback(err, result);
- } else if (resIndex === 0) {
- iterate = throwError;
- callback(null, result);
- } else if (sync) {
- nextTick(function() {
- iterate(result);
- });
- } else {
- sync = true;
- iterate(result);
- }
- sync = false;
- }
- }
- /**
- * @private
- * @param {Function} arrayEach
- * @param {Function} baseEach
- * @param {Function} symbolEach
- */
- function createTransform(arrayEach, baseEach, symbolEach) {
- return function transform(collection, accumulator, iterator, callback) {
- if (arguments.length === 3) {
- callback = iterator;
- iterator = accumulator;
- accumulator = undefined;
- }
- callback = callback || noop;
- var size, keys, result;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- result = accumulator !== undefined ? accumulator : [];
- arrayEach(collection, result, iterator, done);
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- result = accumulator !== undefined ? accumulator : {};
- size = symbolEach(collection, result, iterator, done);
- size && size === completed && callback(null, result);
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- result = accumulator !== undefined ? accumulator : {};
- baseEach(collection, result, iterator, done, keys);
- }
- if (!size) {
- callback(null, accumulator !== undefined ? accumulator : result || {});
- }
- function done(err, bool) {
- if (err) {
- callback = once(callback);
- callback(err, isArray(result) ? createArray(result) : objectClone(result));
- } else if (++completed === size) {
- callback(null, result);
- } else if (bool === false) {
- callback = once(callback);
- callback(null, isArray(result) ? createArray(result) : objectClone(result));
- }
- }
- };
- }
- /**
- * @memberof async
- * @namespace transformSeries
- * @param {Array|Object} collection
- * @param {Array|Object|Function} [accumulator]
- * @param {Function} [iterator]
- * @param {Function} [callback]
- * @example
- *
- * // array
- * var order = [];
- * var collection = [1, 3, 2, 4];
- * var iterator = function(result, num, done) {
- * setTimeout(function() {
- * order.push(num);
- * result.push(num)
- * done();
- * }, num * 10);
- * };
- * async.transformSeries(collection, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2, 4]
- * console.log(order); // [1, 3, 2, 4]
- * });
- *
- * @example
- *
- * // array with index and accumulator
- * var order = [];
- * var collection = [1, 3, 2, 4];
- * var iterator = function(result, num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * result[index] = num;
- * done();
- * }, num * 10);
- * };
- * async.transformSeries(collection, {}, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 3, '2': 2, '3': 4 }
- * console.log(order); // [[1, 0], [3, 1], [2, 2], [4, 3]]
- * });
- *
- * @example
- *
- * // object with accumulator
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(result, num, done) {
- * setTimeout(function() {
- * order.push(num);
- * result.push(num);
- * done();
- * }, num * 10);
- * };
- * async.transformSeries(collection, [], iterator, function(err, res) {
- * console.log(res); // [1, 3, 2, 4]
- * console.log(order); // [1, 3, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2, d: 4 };
- * var iterator = function(result, num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * result[key] = num;
- * done();
- * }, num * 10);
- * };
- * async.transformSeries(collection, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 3, c: 2, d: 4 }
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'b'], [4, 'd']]
- * });
- *
- */
- function transformSeries(collection, accumulator, iterator, callback) {
- if (arguments.length === 3) {
- callback = iterator;
- iterator = accumulator;
- accumulator = undefined;
- }
- callback = onlyOnce(callback || noop);
- var size, key, keys, iter, item, iterate, result;
- var sync = false;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- result = accumulator !== undefined ? accumulator : [];
- iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- result = accumulator !== undefined ? accumulator : {};
- iterate = iterator.length === 4 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- result = accumulator !== undefined ? accumulator : {};
- iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null, accumulator !== undefined ? accumulator : result || {});
- }
- iterate();
- function arrayIterator() {
- iterator(result, collection[completed], done);
- }
- function arrayIteratorWithIndex() {
- iterator(result, collection[completed], completed, done);
- }
- function symbolIterator() {
- item = iter.next();
- item.done ? callback(null, result) : iterator(result, item.value, done);
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- item.done ? callback(null, result) : iterator(result, item.value, completed, done);
- }
- function objectIterator() {
- iterator(result, collection[keys[completed]], done);
- }
- function objectIteratorWithKey() {
- key = keys[completed];
- iterator(result, collection[key], key, done);
- }
- function done(err, bool) {
- if (err) {
- callback(err, result);
- } else if (++completed === size || bool === false) {
- iterate = throwError;
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace transformLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Array|Object|Function} [accumulator]
- * @param {Function} [iterator]
- * @param {Function} [callback]
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(result, num, done) {
- * setTimeout(function() {
- * order.push(num);
- * result.push(num);
- * done();
- * }, num * 10);
- * };
- * async.transformLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [1, 3, 5, 2, 4]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // array with index and accumulator
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(result, num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * result[index] = key;
- * done();
- * }, num * 10);
- * };
- * async.transformLimit(array, 2, {}, iterator, function(err, res) {
- * console.log(res); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
- * });
- *
- * @example
- *
- * // object with accumulator
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(result, num, done) {
- * setTimeout(function() {
- * order.push(num);
- * result.push(num);
- * done();
- * }, num * 10);
- * };
- * async.transformLimit(object, 2, [], iterator, function(err, res) {
- * console.log(res); // [1, 3, 5, 2, 4]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(result, num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * result[key] = num;
- * done();
- * }, num * 10);
- * };
- * async.transformLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
- * });
- *
- */
- function transformLimit(collection, limit, accumulator, iterator, callback) {
- if (arguments.length === 4) {
- callback = iterator;
- iterator = accumulator;
- accumulator = undefined;
- }
- callback = callback || noop;
- var size, index, key, keys, iter, item, iterate, result;
- var sync = false;
- var started = 0;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- result = accumulator !== undefined ? accumulator : [];
- iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- result = accumulator !== undefined ? accumulator : {};
- iterate = iterator.length === 4 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- result = accumulator !== undefined ? accumulator : {};
- iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null, accumulator !== undefined ? accumulator : result || {});
- }
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- index = started++;
- if (index < size) {
- iterator(result, collection[index], onlyOnce(done));
- }
- }
- function arrayIteratorWithIndex() {
- index = started++;
- if (index < size) {
- iterator(result, collection[index], index, onlyOnce(done));
- }
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done === false) {
- started++;
- iterator(result, item.value, onlyOnce(done));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, result);
- }
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done === false) {
- iterator(result, item.value, started++, onlyOnce(done));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, result);
- }
- }
- function objectIterator() {
- index = started++;
- if (index < size) {
- iterator(result, collection[keys[index]], onlyOnce(done));
- }
- }
- function objectIteratorWithKey() {
- index = started++;
- if (index < size) {
- key = keys[index];
- iterator(result, collection[key], key, onlyOnce(done));
- }
- }
- function done(err, bool) {
- if (err || bool === false) {
- iterate = noop;
- callback(err || null, isArray(result) ? createArray(result) : objectClone(result));
- callback = noop;
- } else if (++completed === size) {
- iterator = noop;
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @private
- * @param {function} arrayEach
- * @param {function} baseEach
- * @param {function} symbolEach
- */
- function createSortBy(arrayEach, baseEach, symbolEach) {
- return function sortBy(collection, iterator, callback) {
- callback = callback || noop;
- var size, array, criteria;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- array = Array(size);
- criteria = Array(size);
- arrayEach(collection, iterator, createCallback);
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- array = [];
- criteria = [];
- size = symbolEach(collection, iterator, createCallback);
- size && size === completed && callback(null, sortByCriteria(array, criteria));
- } else if (typeof collection === obj) {
- var keys = nativeKeys(collection);
- size = keys.length;
- array = Array(size);
- criteria = Array(size);
- baseEach(collection, iterator, createCallback, keys);
- }
- if (!size) {
- callback(null, []);
- }
- function createCallback(index, value) {
- var called = false;
- array[index] = value;
- return function done(err, criterion) {
- if (called) {
- throwError();
- }
- called = true;
- criteria[index] = criterion;
- if (err) {
- callback = once(callback);
- callback(err);
- } else if (++completed === size) {
- callback(null, sortByCriteria(array, criteria));
- }
- };
- }
- };
- }
- /**
- * @memberof async
- * @namespace sortBySeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortBySeries(array, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3];
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortBySeries(array, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3]
- * console.log(order); // [[1, 0], [3, 1], [2, 2]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortBySeries(object, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3]
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortBySeries(object, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3]
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
- * });
- *
- */
- function sortBySeries(collection, iterator, callback) {
- callback = onlyOnce(callback || noop);
- var size, key, value, keys, iter, item, array, criteria, iterate;
- var sync = false;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- array = collection;
- criteria = Array(size);
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- array = [];
- criteria = [];
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- array = Array(size);
- criteria = Array(size);
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null, []);
- }
- iterate();
- function arrayIterator() {
- value = collection[completed];
- iterator(value, done);
- }
- function arrayIteratorWithIndex() {
- value = collection[completed];
- iterator(value, completed, done);
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done) {
- return callback(null, sortByCriteria(array, criteria));
- }
- value = item.value;
- array[completed] = value;
- iterator(value, done);
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done) {
- return callback(null, sortByCriteria(array, criteria));
- }
- value = item.value;
- array[completed] = value;
- iterator(value, completed, done);
- }
- function objectIterator() {
- value = collection[keys[completed]];
- array[completed] = value;
- iterator(value, done);
- }
- function objectIteratorWithKey() {
- key = keys[completed];
- value = collection[key];
- array[completed] = value;
- iterator(value, key, done);
- }
- function done(err, criterion) {
- criteria[completed] = criterion;
- if (err) {
- callback(err);
- } else if (++completed === size) {
- iterate = throwError;
- callback(null, sortByCriteria(array, criteria));
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace sortByLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortByLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3, 4, 5]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortByLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3, 4, 5]
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortByLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3, 4, 5]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.sortByLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // [1, 2, 3, 4, 5]
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
- * });
- *
- */
- function sortByLimit(collection, limit, iterator, callback) {
- callback = callback || noop;
- var size, index, key, value, array, keys, iter, item, criteria, iterate;
- var sync = false;
- var started = 0;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- array = collection;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- array = [];
- criteria = [];
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- array = Array(size);
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null, []);
- }
- criteria = criteria || Array(size);
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- if (started < size) {
- value = collection[started];
- iterator(value, createCallback(value, started++));
- }
- }
- function arrayIteratorWithIndex() {
- index = started++;
- if (index < size) {
- value = collection[index];
- iterator(value, index, createCallback(value, index));
- }
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done === false) {
- value = item.value;
- array[started] = value;
- iterator(value, createCallback(value, started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, sortByCriteria(array, criteria));
- }
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done === false) {
- value = item.value;
- array[started] = value;
- iterator(value, started, createCallback(value, started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, sortByCriteria(array, criteria));
- }
- }
- function objectIterator() {
- if (started < size) {
- value = collection[keys[started]];
- array[started] = value;
- iterator(value, createCallback(value, started++));
- }
- }
- function objectIteratorWithKey() {
- if (started < size) {
- key = keys[started];
- value = collection[key];
- array[started] = value;
- iterator(value, key, createCallback(value, started++));
- }
- }
- function createCallback(value, index) {
- var called = false;
- return function(err, criterion) {
- if (called) {
- throwError();
- }
- called = true;
- criteria[index] = criterion;
- if (err) {
- iterate = noop;
- callback(err);
- callback = noop;
- } else if (++completed === size) {
- callback(null, sortByCriteria(array, criteria));
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- };
- }
- }
- /**
- * @memberof async
- * @namespace some
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.some(array, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.some(array, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [[1, 0]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.some(object, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.some(object, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [[1, 'a']]
- * });
- *
- */
- function some(collection, iterator, callback) {
- callback = callback || noop;
- detect(collection, iterator, done);
- function done(err, res) {
- if (err) {
- return callback(err);
- }
- callback(null, !!res);
- }
- }
- /**
- * @memberof async
- * @namespace someSeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.someSeries(array, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.someSeries(array, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [[1, 0]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.someSeries(object, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.someSeries(object, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [[1, 'a']]
- * });
- *
- */
- function someSeries(collection, iterator, callback) {
- callback = callback || noop;
- detectSeries(collection, iterator, done);
- function done(err, res) {
- if (err) {
- return callback(err);
- }
- callback(null, !!res);
- }
- }
- /**
- * @memberof async
- * @namespace someLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.someLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.someLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [[1, 0]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.someLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [1]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num % 2);
- * }, num * 10);
- * };
- * async.someLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // true
- * console.log(order); // [[1, 'a']]
- * });
- *
- */
- function someLimit(collection, limit, iterator, callback) {
- callback = callback || noop;
- detectLimit(collection, limit, iterator, done);
- function done(err, res) {
- if (err) {
- return callback(err);
- }
- callback(null, !!res);
- }
- }
- /**
- * @private
- * @param {Function} arrayEach
- * @param {Function} baseEach
- * @param {Function} symbolEach
- */
- function createEvery(arrayEach, baseEach, symbolEach) {
- var deny = createDetect(arrayEach, baseEach, symbolEach, false);
- return function every(collection, iterator, callback) {
- callback = callback || noop;
- deny(collection, iterator, done);
- function done(err, res) {
- if (err) {
- return callback(err);
- }
- callback(null, !res);
- }
- };
- }
- /**
- * @private
- */
- function createEverySeries() {
- var denySeries = createDetectSeries(false);
- return function everySeries(collection, iterator, callback) {
- callback = callback || noop;
- denySeries(collection, iterator, done);
- function done(err, res) {
- if (err) {
- return callback(err);
- }
- callback(null, !res);
- }
- };
- }
- /**
- * @private
- */
- function createEveryLimit() {
- var denyLimit = createDetectLimit(false);
- return function everyLimit(collection, limit, iterator, callback) {
- callback = callback || noop;
- denyLimit(collection, limit, iterator, done);
- function done(err, res) {
- if (err) {
- return callback(err);
- }
- callback(null, !res);
- }
- };
- }
- /**
- * @private
- * @param {Function} arrayEach
- * @param {Function} baseEach
- * @param {Function} symbolEach
- */
- function createConcat(arrayEach, baseEach, symbolEach) {
- return function concat(collection, iterator, callback) {
- callback = callback || noop;
- var size, result;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- result = Array(size);
- arrayEach(collection, iterator, createCallback);
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- result = [];
- size = symbolEach(collection, iterator, createCallback);
- size && size === completed && callback(null, result);
- } else if (typeof collection === obj) {
- var keys = nativeKeys(collection);
- size = keys.length;
- result = Array(size);
- baseEach(collection, iterator, createCallback, keys);
- }
- if (!size) {
- callback(null, []);
- }
- function createCallback(index) {
- return function done(err, res) {
- if (index === null) {
- throwError();
- }
- if (err) {
- index = null;
- callback = once(callback);
- arrayEachSync(result, function(array, index) {
- if (array === undefined) {
- result[index] = noop;
- }
- });
- callback(err, makeConcatResult(result));
- return;
- }
- switch (arguments.length) {
- case 0:
- case 1:
- result[index] = noop;
- break;
- case 2:
- result[index] = res;
- break;
- default:
- result[index] = slice(arguments, 1);
- break;
- }
- index = null;
- if (++completed === size) {
- callback(null, makeConcatResult(result));
- }
- };
- }
- };
- }
- /**
- * @memberof async
- * @namespace concatSeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.concatSeries(array, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2];
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 3, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.concatSeries(array, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2]
- * console.log(order); // [[1, 0], [3, 1], [2, 2]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.concatSeries(object, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2]
- * console.log(order); // [1, 3, 2]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 3, c: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.concatSeries(object, iterator, function(err, res) {
- * console.log(res); // [1, 3, 2]
- * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
- * });
- *
- */
- function concatSeries(collection, iterator, callback) {
- callback = onlyOnce(callback || noop);
- var size, key, keys, iter, item, iterate;
- var sync = false;
- var result = [];
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null, result);
- }
- iterate();
- function arrayIterator() {
- iterator(collection[completed], done);
- }
- function arrayIteratorWithIndex() {
- iterator(collection[completed], completed, done);
- }
- function symbolIterator() {
- item = iter.next();
- item.done ? callback(null, result) : iterator(item.value, done);
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- item.done ? callback(null, result) : iterator(item.value, completed, done);
- }
- function objectIterator() {
- iterator(collection[keys[completed]], done);
- }
- function objectIteratorWithKey() {
- key = keys[completed];
- iterator(collection[key], key, done);
- }
- function done(err, array) {
- if (isArray(array)) {
- nativePush.apply(result, array);
- } else if (arguments.length >= 2) {
- nativePush.apply(result, slice(arguments, 1));
- }
- if (err) {
- callback(err, result);
- } else if (++completed === size) {
- iterate = throwError;
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace concatLimit
- * @param {Array|Object} collection
- * @param {number} limit - limit >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.concatLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [1, 3, 5, 2, 4]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1, 5, 3, 4, 2];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.cocnatLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // [1, 3, 5, 2, 4]
- * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, [num]);
- * }, num * 10);
- * };
- * async.concatLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // [1, 3, 5, 2, 4]
- * console.log(order); // [1, 3, 5, 2, 4]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, num);
- * }, num * 10);
- * };
- * async.cocnatLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // [1, 3, 5, 2, 4]
- * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
- * });
- *
- */
- function concatLimit(collection, limit, iterator, callback) {
- callback = callback || noop;
- var size, key, iter, item, iterate, result;
- var sync = false;
- var started = 0;
- var completed = 0;
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- result = [];
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- var keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null, []);
- }
- result = result || Array(size);
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- if (started < size) {
- iterator(collection[started], createCallback(started++));
- }
- }
- function arrayIteratorWithIndex() {
- if (started < size) {
- iterator(collection[started], started, createCallback(started++));
- }
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done === false) {
- iterator(item.value, createCallback(started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, makeConcatResult(result));
- }
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done === false) {
- iterator(item.value, started, createCallback(started++));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, makeConcatResult(result));
- }
- }
- function objectIterator() {
- if (started < size) {
- iterator(collection[keys[started]], createCallback(started++));
- }
- }
- function objectIteratorWithKey() {
- if (started < size) {
- key = keys[started];
- iterator(collection[key], key, createCallback(started++));
- }
- }
- function createCallback(index) {
- return function(err, res) {
- if (index === null) {
- throwError();
- }
- if (err) {
- index = null;
- iterate = noop;
- callback = once(callback);
- arrayEachSync(result, function(array, index) {
- if (array === undefined) {
- result[index] = noop;
- }
- });
- callback(err, makeConcatResult(result));
- return;
- }
- switch (arguments.length) {
- case 0:
- case 1:
- result[index] = noop;
- break;
- case 2:
- result[index] = res;
- break;
- default:
- result[index] = slice(arguments, 1);
- break;
- }
- index = null;
- if (++completed === size) {
- iterate = throwError;
- callback(null, makeConcatResult(result));
- callback = throwError;
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- };
- }
- }
- /**
- * @private
- * @param {Function} arrayEach
- * @param {Function} baseEach
- * @param {Function} symbolEach
- */
- function createGroupBy(arrayEach, baseEach, symbolEach) {
- return function groupBy(collection, iterator, callback) {
- callback = callback || noop;
- var size;
- var completed = 0;
- var result = {};
- if (isArray(collection)) {
- size = collection.length;
- arrayEach(collection, iterator, createCallback);
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = symbolEach(collection, iterator, createCallback);
- size && size === completed && callback(null, result);
- } else if (typeof collection === obj) {
- var keys = nativeKeys(collection);
- size = keys.length;
- baseEach(collection, iterator, createCallback, keys);
- }
- if (!size) {
- callback(null, {});
- }
- function createCallback(value) {
- var called = false;
- return function done(err, key) {
- if (called) {
- throwError();
- }
- called = true;
- if (err) {
- callback = once(callback);
- callback(err, objectClone(result));
- return;
- }
- var array = result[key];
- if (!array) {
- result[key] = [value];
- } else {
- array.push(value);
- }
- if (++completed === size) {
- callback(null, result);
- }
- };
- }
- };
- }
- /**
- * @memberof async
- * @namespace groupBySeries
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [4.2, 6.4, 6.1];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupBySeries(array, iterator, function(err, res) {
- * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }
- * console.log(order); // [4.2, 6.4, 6.1]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [4.2, 6.4, 6.1];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupBySeries(array, iterator, function(err, res) {
- * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }
- * console.log(order); // [[4.2, 0], [6.4, 1], [6.1, 2]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 4.2, b: 6.4, c: 6.1 };
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupBySeries(object, iterator, function(err, res) {
- * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }
- * console.log(order); // [4.2, 6.4, 6.1]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 4.2, b: 6.4, c: 6.1 };
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupBySeries(object, iterator, function(err, res) {
- * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }
- * console.log(order); // [[4.2, 'a'], [6.4, 'b'], [6.1, 'c']]
- * });
- *
- */
- function groupBySeries(collection, iterator, callback) {
- callback = onlyOnce(callback || noop);
- var size, key, value, keys, iter, item, iterate;
- var sync = false;
- var completed = 0;
- var result = {};
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size) {
- return callback(null, result);
- }
- iterate();
- function arrayIterator() {
- value = collection[completed];
- iterator(value, done);
- }
- function arrayIteratorWithIndex() {
- value = collection[completed];
- iterator(value, completed, done);
- }
- function symbolIterator() {
- item = iter.next();
- value = item.value;
- item.done ? callback(null, result) : iterator(value, done);
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- value = item.value;
- item.done ? callback(null, result) : iterator(value, completed, done);
- }
- function objectIterator() {
- value = collection[keys[completed]];
- iterator(value, done);
- }
- function objectIteratorWithKey() {
- key = keys[completed];
- value = collection[key];
- iterator(value, key, done);
- }
- function done(err, key) {
- if (err) {
- iterate = throwError;
- callback = onlyOnce(callback);
- callback(err, objectClone(result));
- return;
- }
- var array = result[key];
- if (!array) {
- result[key] = [value];
- } else {
- array.push(value);
- }
- if (++completed === size) {
- iterate = throwError;
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace groupByLimit
- * @param {Array|Object} collection
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * // array
- * var order = [];
- * var array = [1.1, 5.9, 3.2, 3.9, 2.1];
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupByLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
- * console.log(order); // [1.1, 3.2, 5.9, 2.1, 3.9]
- * });
- *
- * @example
- *
- * // array with index
- * var order = [];
- * var array = [1.1, 5.9, 3.2, 3.9, 2.1];
- * var iterator = function(num, index, done) {
- * setTimeout(function() {
- * order.push([num, index]);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupByLimit(array, 2, iterator, function(err, res) {
- * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
- * console.log(order); // [[1.1, 0], [3.2, 2], [5.9, 1], [2.1, 4], [3.9, 3]]
- * });
- *
- * @example
- *
- * // object
- * var order = [];
- * var object = { a: 1.1, b: 5.9, c: 3.2, d: 3.9, e: 2.1 }
- * var iterator = function(num, done) {
- * setTimeout(function() {
- * order.push(num);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupByLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
- * console.log(order); // [1.1, 3.2, 5.9, 2.1, 3.9]
- * });
- *
- * @example
- *
- * // object with key
- * var order = [];
- * var object = { a: 1.1, b: 5.9, c: 3.2, d: 3.9, e: 2.1 }
- * var iterator = function(num, key, done) {
- * setTimeout(function() {
- * order.push([num, key]);
- * done(null, Math.floor(num));
- * }, num * 10);
- * };
- * async.groupByLimit(object, 2, iterator, function(err, res) {
- * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
- * console.log(order); // [[1.1, 'a'], [3.2, 'c'], [5.9, 'b'], [2.1, 'e'], [3.9, 'd']]
- * });
- *
- */
- function groupByLimit(collection, limit, iterator, callback) {
- callback = callback || noop;
- var size, index, key, value, keys, iter, item, iterate;
- var sync = false;
- var started = 0;
- var completed = 0;
- var result = {};
- if (isArray(collection)) {
- size = collection.length;
- iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
- } else if (!collection) {
- } else if (iteratorSymbol && collection[iteratorSymbol]) {
- size = Infinity;
- iter = collection[iteratorSymbol]();
- iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
- } else if (typeof collection === obj) {
- keys = nativeKeys(collection);
- size = keys.length;
- iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null, result);
- }
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- if (started < size) {
- value = collection[started++];
- iterator(value, createCallback(value));
- }
- }
- function arrayIteratorWithIndex() {
- index = started++;
- if (index < size) {
- value = collection[index];
- iterator(value, index, createCallback(value));
- }
- }
- function symbolIterator() {
- item = iter.next();
- if (item.done === false) {
- started++;
- value = item.value;
- iterator(value, createCallback(value));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, result);
- }
- }
- function symbolIteratorWithKey() {
- item = iter.next();
- if (item.done === false) {
- value = item.value;
- iterator(value, started++, createCallback(value));
- } else if (completed === started && iterator !== noop) {
- iterator = noop;
- callback(null, result);
- }
- }
- function objectIterator() {
- if (started < size) {
- value = collection[keys[started++]];
- iterator(value, createCallback(value));
- }
- }
- function objectIteratorWithKey() {
- if (started < size) {
- key = keys[started++];
- value = collection[key];
- iterator(value, key, createCallback(value));
- }
- }
- function createCallback(value) {
- var called = false;
- return function(err, key) {
- if (called) {
- throwError();
- }
- called = true;
- if (err) {
- iterate = noop;
- callback = once(callback);
- callback(err, objectClone(result));
- return;
- }
- var array = result[key];
- if (!array) {
- result[key] = [value];
- } else {
- array.push(value);
- }
- if (++completed === size) {
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- };
- }
- }
- /**
- * @private
- * @param {Function} arrayEach
- * @param {Function} baseEach
- */
- function createParallel(arrayEach, baseEach) {
- return function parallel(tasks, callback) {
- callback = callback || noop;
- var size, keys, result;
- var completed = 0;
- if (isArray(tasks)) {
- size = tasks.length;
- result = Array(size);
- arrayEach(tasks, createCallback);
- } else if (tasks && typeof tasks === obj) {
- keys = nativeKeys(tasks);
- size = keys.length;
- result = {};
- baseEach(tasks, createCallback, keys);
- }
- if (!size) {
- callback(null, result);
- }
- function createCallback(key) {
- return function(err, res) {
- if (key === null) {
- throwError();
- }
- if (err) {
- key = null;
- callback = once(callback);
- callback(err, result);
- return;
- }
- result[key] = arguments.length <= 2 ? res : slice(arguments, 1);
- key = null;
- if (++completed === size) {
- callback(null, result);
- }
- };
- }
- };
- }
- /**
- * @memberof async
- * @namespace series
- * @param {Array|Object} tasks - functions
- * @param {Function} callback
- * @example
- *
- * var order = [];
- * var tasks = [
- * function(done) {
- * setTimeout(function() {
- * order.push(1);
- * done(null, 1);
- * }, 10);
- * },
- * function(done) {
- * setTimeout(function() {
- * order.push(2);
- * done(null, 2);
- * }, 30);
- * },
- * function(done) {
- * setTimeout(function() {
- * order.push(3);
- * done(null, 3);
- * }, 40);
- * },
- * function(done) {
- * setTimeout(function() {
- * order.push(4);
- * done(null, 4);
- * }, 20);
- * }
- * ];
- * async.series(tasks, function(err, res) {
- * console.log(res); // [1, 2, 3, 4];
- * console.log(order); // [1, 2, 3, 4]
- * });
- *
- * @example
- *
- * var order = [];
- * var tasks = {
- * 'a': function(done) {
- * setTimeout(function() {
- * order.push(1);
- * done(null, 1);
- * }, 10);
- * },
- * 'b': function(done) {
- * setTimeout(function() {
- * order.push(2);
- * done(null, 2);
- * }, 30);
- * },
- * 'c': function(done) {
- * setTimeout(function() {
- * order.push(3);
- * done(null, 3);
- * }, 40);
- * },
- * 'd': function(done) {
- * setTimeout(function() {
- * order.push(4);
- * done(null, 4);
- * }, 20);
- * }
- * };
- * async.series(tasks, function(err, res) {
- * console.log(res); // { a: 1, b: 2, c: 3, d:4 }
- * console.log(order); // [1, 4, 2, 3]
- * });
- *
- */
- function series(tasks, callback) {
- callback = callback || noop;
- var size, key, keys, result, iterate;
- var sync = false;
- var completed = 0;
- if (isArray(tasks)) {
- size = tasks.length;
- result = Array(size);
- iterate = arrayIterator;
- } else if (tasks && typeof tasks === obj) {
- keys = nativeKeys(tasks);
- size = keys.length;
- result = {};
- iterate = objectIterator;
- } else {
- return callback(null);
- }
- if (!size) {
- return callback(null, result);
- }
- iterate();
- function arrayIterator() {
- key = completed;
- tasks[completed](done);
- }
- function objectIterator() {
- key = keys[completed];
- tasks[key](done);
- }
- function done(err, res) {
- if (err) {
- iterate = throwError;
- callback = onlyOnce(callback);
- callback(err, result);
- return;
- }
- result[key] = arguments.length <= 2 ? res : slice(arguments, 1);
- if (++completed === size) {
- iterate = throwError;
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace parallelLimit
- * @param {Array|Object} tasks - functions
- * @param {number} limit - limit >= 1
- * @param {Function} callback
- * @example
- *
- * var order = [];
- * var tasks = [
- * function(done) {
- * setTimeout(function() {
- * order.push(1);
- * done(null, 1);
- * }, 10);
- * },
- * function(done) {
- * setTimeout(function() {
- * order.push(2);
- * done(null, 2);
- * }, 50);
- * },
- * function(done) {
- * setTimeout(function() {
- * order.push(3);
- * done(null, 3);
- * }, 30);
- * },
- * function(done) {
- * setTimeout(function() {
- * order.push(4);
- * done(null, 4);
- * }, 40);
- * }
- * ];
- * async.parallelLimit(tasks, 2, function(err, res) {
- * console.log(res); // [1, 2, 3, 4];
- * console.log(order); // [1, 3, 2, 4]
- * });
- *
- * @example
- *
- * var order = [];
- * var tasks = {
- * 'a': function(done) {
- * setTimeout(function() {
- * order.push(1);
- * done(null, 1);
- * }, 10);
- * },
- * 'b': function(done) {
- * setTimeout(function() {
- * order.push(2);
- * done(null, 2);
- * }, 50);
- * },
- * 'c': function(done) {
- * setTimeout(function() {
- * order.push(3);
- * done(null, 3);
- * }, 20);
- * },
- * 'd': function(done) {
- * setTimeout(function() {
- * order.push(4);
- * done(null, 4);
- * }, 40);
- * }
- * };
- * async.parallelLimit(tasks, 2, function(err, res) {
- * console.log(res); // { a: 1, b: 2, c: 3, d:4 }
- * console.log(order); // [1, 3, 2, 4]
- * });
- *
- */
- function parallelLimit(tasks, limit, callback) {
- callback = callback || noop;
- var size, index, key, keys, result, iterate;
- var sync = false;
- var started = 0;
- var completed = 0;
- if (isArray(tasks)) {
- size = tasks.length;
- result = Array(size);
- iterate = arrayIterator;
- } else if (tasks && typeof tasks === obj) {
- keys = nativeKeys(tasks);
- size = keys.length;
- result = {};
- iterate = objectIterator;
- }
- if (!size || isNaN(limit) || limit < 1) {
- return callback(null, result);
- }
- timesSync(limit > size ? size : limit, iterate);
- function arrayIterator() {
- index = started++;
- if (index < size) {
- tasks[index](createCallback(index));
- }
- }
- function objectIterator() {
- if (started < size) {
- key = keys[started++];
- tasks[key](createCallback(key));
- }
- }
- function createCallback(key) {
- return function(err, res) {
- if (key === null) {
- throwError();
- }
- if (err) {
- key = null;
- iterate = noop;
- callback = once(callback);
- callback(err, result);
- return;
- }
- result[key] = arguments.length <= 2 ? res : slice(arguments, 1);
- key = null;
- if (++completed === size) {
- callback(null, result);
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- };
- }
- }
- /**
- * @memberof async
- * @namespace tryEach
- * @param {Array|Object} tasks - functions
- * @param {Function} callback
- * @example
- *
- * var tasks = [
- * function(done) {
- * setTimeout(function() {
- * done(new Error('error'));
- * }, 10);
- * },
- * function(done) {
- * setTimeout(function() {
- * done(null, 2);
- * }, 10);
- * }
- * ];
- * async.tryEach(tasks, function(err, res) {
- * console.log(res); // 2
- * });
- *
- * @example
- *
- * var tasks = [
- * function(done) {
- * setTimeout(function() {
- * done(new Error('error1'));
- * }, 10);
- * },
- * function(done) {
- * setTimeout(function() {
- * done(new Error('error2');
- * }, 10);
- * }
- * ];
- * async.tryEach(tasks, function(err, res) {
- * console.log(err); // error2
- * console.log(res); // undefined
- * });
- *
- */
- function tryEach(tasks, callback) {
- callback = callback || noop;
- var size, keys, iterate;
- var sync = false;
- var completed = 0;
- if (isArray(tasks)) {
- size = tasks.length;
- iterate = arrayIterator;
- } else if (tasks && typeof tasks === obj) {
- keys = nativeKeys(tasks);
- size = keys.length;
- iterate = objectIterator;
- }
- if (!size) {
- return callback(null);
- }
- iterate();
- function arrayIterator() {
- tasks[completed](done);
- }
- function objectIterator() {
- tasks[keys[completed]](done);
- }
- function done(err, res) {
- if (!err) {
- if (arguments.length <= 2) {
- callback(null, res);
- } else {
- callback(null, slice(arguments, 1));
- }
- } else if (++completed === size) {
- callback(err);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * check for waterfall tasks
- * @private
- * @param {Array} tasks
- * @param {Function} callback
- * @return {boolean}
- */
- function checkWaterfallTasks(tasks, callback) {
- if (!isArray(tasks)) {
- callback(new Error('First argument to waterfall must be an array of functions'));
- return false;
- }
- if (tasks.length === 0) {
- callback(null);
- return false;
- }
- return true;
- }
- /**
- * check for waterfall tasks
- * @private
- * @param {function} func
- * @param {Array|Object} args - arguments
- * @return {function} next
- */
- function waterfallIterator(func, args, next) {
- switch (args.length) {
- case 0:
- case 1:
- return func(next);
- case 2:
- return func(args[1], next);
- case 3:
- return func(args[1], args[2], next);
- case 4:
- return func(args[1], args[2], args[3], next);
- case 5:
- return func(args[1], args[2], args[3], args[4], next);
- case 6:
- return func(args[1], args[2], args[3], args[4], args[5], next);
- default:
- args = slice(args, 1);
- args.push(next);
- return func.apply(null, args);
- }
- }
- /**
- * @memberof async
- * @namespace waterfall
- * @param {Array} tasks - functions
- * @param {Function} callback
- * @example
- *
- * var order = [];
- * var tasks = [
- * function(next) {
- * setTimeout(function() {
- * order.push(1);
- * next(null, 1);
- * }, 10);
- * },
- * function(arg1, next) {
- * setTimeout(function() {
- * order.push(2);
- * next(null, 1, 2);
- * }, 30);
- * },
- * function(arg1, arg2, next) {
- * setTimeout(function() {
- * order.push(3);
- * next(null, 3);
- * }, 20);
- * },
- * function(arg1, next) {
- * setTimeout(function() {
- * order.push(4);
- * next(null, 1, 2, 3, 4);
- * }, 40);
- * }
- * ];
- * async.waterfall(tasks, function(err, arg1, arg2, arg3, arg4) {
- * console.log(arg1, arg2, arg3, arg4); // 1 2 3 4
- * });
- *
- */
- function waterfall(tasks, callback) {
- callback = callback || noop;
- if (!checkWaterfallTasks(tasks, callback)) {
- return;
- }
- var func, args, done, sync;
- var completed = 0;
- var size = tasks.length;
- waterfallIterator(tasks[0], [], createCallback(0));
- function iterate() {
- waterfallIterator(func, args, createCallback(func));
- }
- function createCallback(index) {
- return function next(err, res) {
- if (index === undefined) {
- callback = noop;
- throwError();
- }
- index = undefined;
- if (err) {
- done = callback;
- callback = throwError;
- done(err);
- return;
- }
- if (++completed === size) {
- done = callback;
- callback = throwError;
- if (arguments.length <= 2) {
- done(err, res);
- } else {
- done.apply(null, createArray(arguments));
- }
- return;
- }
- if (sync) {
- args = arguments;
- func = tasks[completed] || throwError;
- nextTick(iterate);
- } else {
- sync = true;
- waterfallIterator(tasks[completed] || throwError, arguments, createCallback(completed));
- }
- sync = false;
- };
- }
- }
- /**
- * `angelFall` is like `waterfall` and inject callback to last argument of next task.
- *
- * @memberof async
- * @namespace angelFall
- * @param {Array} tasks - functions
- * @param {Function} callback
- * @example
- *
- * var order = [];
- * var tasks = [
- * function(next) {
- * setTimeout(function() {
- * order.push(1);
- * next(null, 1);
- * }, 10);
- * },
- * function(arg1, empty, next) {
- * setTimeout(function() {
- * order.push(2);
- * next(null, 1, 2);
- * }, 30);
- * },
- * function(next) {
- * setTimeout(function() {
- * order.push(3);
- * next(null, 3);
- * }, 20);
- * },
- * function(arg1, empty1, empty2, empty3, next) {
- * setTimeout(function() {
- * order.push(4);
- * next(null, 1, 2, 3, 4);
- * }, 40);
- * }
- * ];
- * async.angelFall(tasks, function(err, arg1, arg2, arg3, arg4) {
- * console.log(arg1, arg2, arg3, arg4); // 1 2 3 4
- * });
- *
- */
- function angelFall(tasks, callback) {
- callback = callback || noop;
- if (!checkWaterfallTasks(tasks, callback)) {
- return;
- }
- var completed = 0;
- var sync = false;
- var size = tasks.length;
- var func = tasks[completed];
- var args = [];
- var iterate = function() {
- switch (func.length) {
- case 0:
- try {
- next(null, func());
- } catch (e) {
- next(e);
- }
- return;
- case 1:
- return func(next);
- case 2:
- return func(args[1], next);
- case 3:
- return func(args[1], args[2], next);
- case 4:
- return func(args[1], args[2], args[3], next);
- case 5:
- return func(args[1], args[2], args[3], args[4], next);
- default:
- args = slice(args, 1);
- args[func.length - 1] = next;
- return func.apply(null, args);
- }
- };
- iterate();
- function next(err, res) {
- if (err) {
- iterate = throwError;
- callback = onlyOnce(callback);
- callback(err);
- return;
- }
- if (++completed === size) {
- iterate = throwError;
- var done = callback;
- callback = throwError;
- if (arguments.length === 2) {
- done(err, res);
- } else {
- done.apply(null, createArray(arguments));
- }
- return;
- }
- func = tasks[completed];
- args = arguments;
- if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace whilst
- * @param {Function} test
- * @param {Function} iterator
- * @param {Function} callback
- */
- function whilst(test, iterator, callback) {
- callback = callback || noop;
- var sync = false;
- if (test()) {
- iterate();
- } else {
- callback(null);
- }
- function iterate() {
- if (sync) {
- nextTick(next);
- } else {
- sync = true;
- iterator(done);
- }
- sync = false;
- }
- function next() {
- iterator(done);
- }
- function done(err, arg) {
- if (err) {
- return callback(err);
- }
- if (arguments.length <= 2) {
- if (test(arg)) {
- iterate();
- } else {
- callback(null, arg);
- }
- return;
- }
- arg = slice(arguments, 1);
- if (test.apply(null, arg)) {
- iterate();
- } else {
- callback.apply(null, [null].concat(arg));
- }
- }
- }
- /**
- * @memberof async
- * @namespace doWhilst
- * @param {Function} iterator
- * @param {Function} test
- * @param {Function} callback
- */
- function doWhilst(iterator, test, callback) {
- callback = callback || noop;
- var sync = false;
- next();
- function iterate() {
- if (sync) {
- nextTick(next);
- } else {
- sync = true;
- iterator(done);
- }
- sync = false;
- }
- function next() {
- iterator(done);
- }
- function done(err, arg) {
- if (err) {
- return callback(err);
- }
- if (arguments.length <= 2) {
- if (test(arg)) {
- iterate();
- } else {
- callback(null, arg);
- }
- return;
- }
- arg = slice(arguments, 1);
- if (test.apply(null, arg)) {
- iterate();
- } else {
- callback.apply(null, [null].concat(arg));
- }
- }
- }
- /**
- * @memberof async
- * @namespace until
- * @param {Function} test
- * @param {Function} iterator
- * @param {Function} callback
- */
- function until(test, iterator, callback) {
- callback = callback || noop;
- var sync = false;
- if (!test()) {
- iterate();
- } else {
- callback(null);
- }
- function iterate() {
- if (sync) {
- nextTick(next);
- } else {
- sync = true;
- iterator(done);
- }
- sync = false;
- }
- function next() {
- iterator(done);
- }
- function done(err, arg) {
- if (err) {
- return callback(err);
- }
- if (arguments.length <= 2) {
- if (!test(arg)) {
- iterate();
- } else {
- callback(null, arg);
- }
- return;
- }
- arg = slice(arguments, 1);
- if (!test.apply(null, arg)) {
- iterate();
- } else {
- callback.apply(null, [null].concat(arg));
- }
- }
- }
- /**
- * @memberof async
- * @namespace doUntil
- * @param {Function} iterator
- * @param {Function} test
- * @param {Function} callback
- */
- function doUntil(iterator, test, callback) {
- callback = callback || noop;
- var sync = false;
- next();
- function iterate() {
- if (sync) {
- nextTick(next);
- } else {
- sync = true;
- iterator(done);
- }
- sync = false;
- }
- function next() {
- iterator(done);
- }
- function done(err, arg) {
- if (err) {
- return callback(err);
- }
- if (arguments.length <= 2) {
- if (!test(arg)) {
- iterate();
- } else {
- callback(null, arg);
- }
- return;
- }
- arg = slice(arguments, 1);
- if (!test.apply(null, arg)) {
- iterate();
- } else {
- callback.apply(null, [null].concat(arg));
- }
- }
- }
- /**
- * @memberof async
- * @namespace during
- * @param {Function} test
- * @param {Function} iterator
- * @param {Function} callback
- */
- function during(test, iterator, callback) {
- callback = callback || noop;
- _test();
- function _test() {
- test(iterate);
- }
- function iterate(err, truth) {
- if (err) {
- return callback(err);
- }
- if (truth) {
- iterator(done);
- } else {
- callback(null);
- }
- }
- function done(err) {
- if (err) {
- return callback(err);
- }
- _test();
- }
- }
- /**
- * @memberof async
- * @namespace doDuring
- * @param {Function} test
- * @param {Function} iterator
- * @param {Function} callback
- */
- function doDuring(iterator, test, callback) {
- callback = callback || noop;
- iterate(null, true);
- function iterate(err, truth) {
- if (err) {
- return callback(err);
- }
- if (truth) {
- iterator(done);
- } else {
- callback(null);
- }
- }
- function done(err, res) {
- if (err) {
- return callback(err);
- }
- switch (arguments.length) {
- case 0:
- case 1:
- test(iterate);
- break;
- case 2:
- test(res, iterate);
- break;
- default:
- var args = slice(arguments, 1);
- args.push(iterate);
- test.apply(null, args);
- break;
- }
- }
- }
- /**
- * @memberof async
- * @namespace forever
- */
- function forever(iterator, callback) {
- var sync = false;
- iterate();
- function iterate() {
- iterator(next);
- }
- function next(err) {
- if (err) {
- if (callback) {
- return callback(err);
- }
- throw err;
- }
- if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace compose
- */
- function compose() {
- return seq.apply(null, reverse(arguments));
- }
- /**
- * @memberof async
- * @namespace seq
- */
- function seq(/* functions... */) {
- var fns = createArray(arguments);
- return function() {
- var self = this;
- var args = createArray(arguments);
- var callback = args[args.length - 1];
- if (typeof callback === func) {
- args.pop();
- } else {
- callback = noop;
- }
- reduce(fns, args, iterator, done);
- function iterator(newargs, fn, callback) {
- var func = function(err) {
- var nextargs = slice(arguments, 1);
- callback(err, nextargs);
- };
- newargs.push(func);
- fn.apply(self, newargs);
- }
- function done(err, res) {
- res = isArray(res) ? res : [res];
- res.unshift(err);
- callback.apply(self, res);
- }
- };
- }
- function createApplyEach(func) {
- return function applyEach(fns /* arguments */) {
- var go = function() {
- var self = this;
- var args = createArray(arguments);
- var callback = args.pop() || noop;
- return func(fns, iterator, callback);
- function iterator(fn, done) {
- fn.apply(self, args.concat([done]));
- }
- };
- if (arguments.length > 1) {
- var args = slice(arguments, 1);
- return go.apply(this, args);
- } else {
- return go;
- }
- };
- }
- /**
- * @see https://github.com/caolan/async/blob/master/lib/internal/DoublyLinkedList.js
- */
- function DLL() {
- this.head = null;
- this.tail = null;
- this.length = 0;
- }
- DLL.prototype._removeLink = function(node) {
- var prev = node.prev;
- var next = node.next;
- if (prev) {
- prev.next = next;
- } else {
- this.head = next;
- }
- if (next) {
- next.prev = prev;
- } else {
- this.tail = prev;
- }
- node.prev = null;
- node.next = null;
- this.length--;
- return node;
- };
- DLL.prototype.empty = DLL;
- DLL.prototype._setInitial = function(node) {
- this.length = 1;
- this.head = this.tail = node;
- };
- DLL.prototype.insertBefore = function(node, newNode) {
- newNode.prev = node.prev;
- newNode.next = node;
- if (node.prev) {
- node.prev.next = newNode;
- } else {
- this.head = newNode;
- }
- node.prev = newNode;
- this.length++;
- };
- DLL.prototype.unshift = function(node) {
- if (this.head) {
- this.insertBefore(this.head, node);
- } else {
- this._setInitial(node);
- }
- };
- DLL.prototype.push = function(node) {
- var tail = this.tail;
- if (tail) {
- node.prev = tail;
- node.next = tail.next;
- this.tail = node;
- tail.next = node;
- this.length++;
- } else {
- this._setInitial(node);
- }
- };
- DLL.prototype.shift = function() {
- return this.head && this._removeLink(this.head);
- };
- DLL.prototype.splice = function(end) {
- var task;
- var tasks = [];
- while (end-- && (task = this.shift())) {
- tasks.push(task);
- }
- return tasks;
- };
- DLL.prototype.remove = function(test) {
- var node = this.head;
- while (node) {
- if (test(node)) {
- this._removeLink(node);
- }
- node = node.next;
- }
- return this;
- };
- /**
- * @private
- */
- function baseQueue(isQueue, worker, concurrency, payload) {
- if (concurrency === undefined) {
- concurrency = 1;
- } else if (isNaN(concurrency) || concurrency < 1) {
- throw new Error('Concurrency must not be zero');
- }
- var workers = 0;
- var workersList = [];
- var _callback, _unshift;
- var q = {
- _tasks: new DLL(),
- concurrency: concurrency,
- payload: payload,
- saturated: noop,
- unsaturated: noop,
- buffer: concurrency / 4,
- empty: noop,
- drain: noop,
- error: noop,
- started: false,
- paused: false,
- push: push,
- kill: kill,
- unshift: unshift,
- remove: remove,
- process: isQueue ? runQueue : runCargo,
- length: getLength,
- running: running,
- workersList: getWorkersList,
- idle: idle,
- pause: pause,
- resume: resume,
- _worker: worker
- };
- return q;
- function push(tasks, callback) {
- _insert(tasks, callback);
- }
- function unshift(tasks, callback) {
- _insert(tasks, callback, true);
- }
- function _exec(task) {
- var item = {
- data: task,
- callback: _callback
- };
- if (_unshift) {
- q._tasks.unshift(item);
- } else {
- q._tasks.push(item);
- }
- nextTick(q.process);
- }
- function _insert(tasks, callback, unshift) {
- if (callback == null) {
- callback = noop;
- } else if (typeof callback !== 'function') {
- throw new Error('task callback must be a function');
- }
- q.started = true;
- var _tasks = isArray(tasks) ? tasks : [tasks];
- if (tasks === undefined || !_tasks.length) {
- if (q.idle()) {
- nextTick(q.drain);
- }
- return;
- }
- _unshift = unshift;
- _callback = callback;
- arrayEachSync(_tasks, _exec);
- // Avoid leaking the callback
- _callback = undefined;
- }
- function kill() {
- q.drain = noop;
- q._tasks.empty();
- }
- function _next(q, tasks) {
- var called = false;
- return function done(err, res) {
- if (called) {
- throwError();
- }
- called = true;
- workers--;
- var task;
- var index = -1;
- var size = workersList.length;
- var taskIndex = -1;
- var taskSize = tasks.length;
- var useApply = arguments.length > 2;
- var args = useApply && createArray(arguments);
- while (++taskIndex < taskSize) {
- task = tasks[taskIndex];
- while (++index < size) {
- if (workersList[index] === task) {
- if (index === 0) {
- workersList.shift();
- } else {
- workersList.splice(index, 1);
- }
- index = size;
- size--;
- }
- }
- index = -1;
- if (useApply) {
- task.callback.apply(task, args);
- } else {
- task.callback(err, res);
- }
- if (err) {
- q.error(err, task.data);
- }
- }
- if (workers <= q.concurrency - q.buffer) {
- q.unsaturated();
- }
- if (q._tasks.length + workers === 0) {
- q.drain();
- }
- q.process();
- };
- }
- function runQueue() {
- while (!q.paused && workers < q.concurrency && q._tasks.length) {
- var task = q._tasks.shift();
- workers++;
- workersList.push(task);
- if (q._tasks.length === 0) {
- q.empty();
- }
- if (workers === q.concurrency) {
- q.saturated();
- }
- var done = _next(q, [task]);
- worker(task.data, done);
- }
- }
- function runCargo() {
- while (!q.paused && workers < q.concurrency && q._tasks.length) {
- var tasks = q._tasks.splice(q.payload || q._tasks.length);
- var index = -1;
- var size = tasks.length;
- var data = Array(size);
- while (++index < size) {
- data[index] = tasks[index].data;
- }
- workers++;
- nativePush.apply(workersList, tasks);
- if (q._tasks.length === 0) {
- q.empty();
- }
- if (workers === q.concurrency) {
- q.saturated();
- }
- var done = _next(q, tasks);
- worker(data, done);
- }
- }
- function getLength() {
- return q._tasks.length;
- }
- function running() {
- return workers;
- }
- function getWorkersList() {
- return workersList;
- }
- function idle() {
- return q.length() + workers === 0;
- }
- function pause() {
- q.paused = true;
- }
- function _resume() {
- nextTick(q.process);
- }
- function resume() {
- if (q.paused === false) {
- return;
- }
- q.paused = false;
- var count = q.concurrency < q._tasks.length ? q.concurrency : q._tasks.length;
- timesSync(count, _resume);
- }
- /**
- * @param {Function} test
- */
- function remove(test) {
- q._tasks.remove(test);
- }
- }
- /**
- * @memberof async
- * @namespace queue
- */
- function queue(worker, concurrency) {
- return baseQueue(true, worker, concurrency);
- }
- /**
- * @memberof async
- * @namespace priorityQueue
- */
- function priorityQueue(worker, concurrency) {
- var q = baseQueue(true, worker, concurrency);
- q.push = push;
- delete q.unshift;
- return q;
- function push(tasks, priority, callback) {
- q.started = true;
- priority = priority || 0;
- var _tasks = isArray(tasks) ? tasks : [tasks];
- var taskSize = _tasks.length;
- if (tasks === undefined || taskSize === 0) {
- if (q.idle()) {
- nextTick(q.drain);
- }
- return;
- }
- callback = typeof callback === func ? callback : noop;
- var nextNode = q._tasks.head;
- while (nextNode && priority >= nextNode.priority) {
- nextNode = nextNode.next;
- }
- while (taskSize--) {
- var item = {
- data: _tasks[taskSize],
- priority: priority,
- callback: callback
- };
- if (nextNode) {
- q._tasks.insertBefore(nextNode, item);
- } else {
- q._tasks.push(item);
- }
- nextTick(q.process);
- }
- }
- }
- /**
- * @memberof async
- * @namespace cargo
- */
- function cargo(worker, payload) {
- return baseQueue(false, worker, 1, payload);
- }
- /**
- * @memberof async
- * @namespace auto
- * @param {Object} tasks
- * @param {number} [concurrency]
- * @param {Function} [callback]
- */
- function auto(tasks, concurrency, callback) {
- if (typeof concurrency === func) {
- callback = concurrency;
- concurrency = null;
- }
- var keys = nativeKeys(tasks);
- var rest = keys.length;
- var results = {};
- if (rest === 0) {
- return callback(null, results);
- }
- var runningTasks = 0;
- var readyTasks = new DLL();
- var listeners = Object.create(null);
- callback = onlyOnce(callback || noop);
- concurrency = concurrency || rest;
- baseEachSync(tasks, iterator, keys);
- proceedQueue();
- function iterator(task, key) {
- // no dependencies
- var _task, _taskSize;
- if (!isArray(task)) {
- _task = task;
- _taskSize = 0;
- readyTasks.push([_task, _taskSize, done]);
- return;
- }
- var dependencySize = task.length - 1;
- _task = task[dependencySize];
- _taskSize = dependencySize;
- if (dependencySize === 0) {
- readyTasks.push([_task, _taskSize, done]);
- return;
- }
- // dependencies
- var index = -1;
- while (++index < dependencySize) {
- var dependencyName = task[index];
- if (notInclude(keys, dependencyName)) {
- var msg =
- 'async.auto task `' +
- key +
- '` has non-existent dependency `' +
- dependencyName +
- '` in ' +
- task.join(', ');
- throw new Error(msg);
- }
- var taskListeners = listeners[dependencyName];
- if (!taskListeners) {
- taskListeners = listeners[dependencyName] = [];
- }
- taskListeners.push(taskListener);
- }
- function done(err, arg) {
- if (key === null) {
- throwError();
- }
- arg = arguments.length <= 2 ? arg : slice(arguments, 1);
- if (err) {
- rest = 0;
- runningTasks = 0;
- readyTasks.length = 0;
- var safeResults = objectClone(results);
- safeResults[key] = arg;
- key = null;
- var _callback = callback;
- callback = noop;
- _callback(err, safeResults);
- return;
- }
- runningTasks--;
- rest--;
- results[key] = arg;
- taskComplete(key);
- key = null;
- }
- function taskListener() {
- if (--dependencySize === 0) {
- readyTasks.push([_task, _taskSize, done]);
- }
- }
- }
- function proceedQueue() {
- if (readyTasks.length === 0 && runningTasks === 0) {
- if (rest !== 0) {
- throw new Error('async.auto task has cyclic dependencies');
- }
- return callback(null, results);
- }
- while (readyTasks.length && runningTasks < concurrency && callback !== noop) {
- runningTasks++;
- var array = readyTasks.shift();
- if (array[1] === 0) {
- array[0](array[2]);
- } else {
- array[0](results, array[2]);
- }
- }
- }
- function taskComplete(key) {
- var taskListeners = listeners[key] || [];
- arrayEachSync(taskListeners, function(task) {
- task();
- });
- proceedQueue();
- }
- }
- var FN_ARGS = /^(function)?\s*[^\(]*\(\s*([^\)]*)\)/m;
- var FN_ARG_SPLIT = /,/;
- var FN_ARG = /(=.+)?(\s*)$/;
- var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;
- /**
- * parse function arguments for `autoInject`
- *
- * @private
- */
- function parseParams(func) {
- func = func.toString().replace(STRIP_COMMENTS, '');
- func = func.match(FN_ARGS)[2].replace(' ', '');
- func = func ? func.split(FN_ARG_SPLIT) : [];
- func = func.map(function(arg) {
- return arg.replace(FN_ARG, '').trim();
- });
- return func;
- }
- /**
- * @memberof async
- * @namespace autoInject
- * @param {Object} tasks
- * @param {number} [concurrency]
- * @param {Function} [callback]
- */
- function autoInject(tasks, concurrency, callback) {
- var newTasks = {};
- baseEachSync(tasks, iterator, nativeKeys(tasks));
- auto(newTasks, concurrency, callback);
- function iterator(task, key) {
- var params;
- var taskLength = task.length;
- if (isArray(task)) {
- if (taskLength === 0) {
- throw new Error('autoInject task functions require explicit parameters.');
- }
- params = createArray(task);
- taskLength = params.length - 1;
- task = params[taskLength];
- if (taskLength === 0) {
- newTasks[key] = task;
- return;
- }
- } else if (taskLength === 1) {
- newTasks[key] = task;
- return;
- } else {
- params = parseParams(task);
- if (taskLength === 0 && params.length === 0) {
- throw new Error('autoInject task functions require explicit parameters.');
- }
- taskLength = params.length - 1;
- }
- params[taskLength] = newTask;
- newTasks[key] = params;
- function newTask(results, done) {
- switch (taskLength) {
- case 1:
- task(results[params[0]], done);
- break;
- case 2:
- task(results[params[0]], results[params[1]], done);
- break;
- case 3:
- task(results[params[0]], results[params[1]], results[params[2]], done);
- break;
- default:
- var i = -1;
- while (++i < taskLength) {
- params[i] = results[params[i]];
- }
- params[i] = done;
- task.apply(null, params);
- break;
- }
- }
- }
- }
- /**
- * @memberof async
- * @namespace retry
- * @param {integer|Object|Function} opts
- * @param {Function} [task]
- * @param {Function} [callback]
- */
- function retry(opts, task, callback) {
- var times, intervalFunc, errorFilter;
- var count = 0;
- if (arguments.length < 3 && typeof opts === func) {
- callback = task || noop;
- task = opts;
- opts = null;
- times = DEFAULT_TIMES;
- } else {
- callback = callback || noop;
- switch (typeof opts) {
- case 'object':
- if (typeof opts.errorFilter === func) {
- errorFilter = opts.errorFilter;
- }
- var interval = opts.interval;
- switch (typeof interval) {
- case func:
- intervalFunc = interval;
- break;
- case 'string':
- case 'number':
- interval = +interval;
- intervalFunc = interval
- ? function() {
- return interval;
- }
- : function() {
- return DEFAULT_INTERVAL;
- };
- break;
- }
- times = +opts.times || DEFAULT_TIMES;
- break;
- case 'number':
- times = opts || DEFAULT_TIMES;
- break;
- case 'string':
- times = +opts || DEFAULT_TIMES;
- break;
- default:
- throw new Error('Invalid arguments for async.retry');
- }
- }
- if (typeof task !== 'function') {
- throw new Error('Invalid arguments for async.retry');
- }
- if (intervalFunc) {
- task(intervalCallback);
- } else {
- task(simpleCallback);
- }
- function simpleIterator() {
- task(simpleCallback);
- }
- function simpleCallback(err, res) {
- if (++count === times || !err || (errorFilter && !errorFilter(err))) {
- if (arguments.length <= 2) {
- return callback(err, res);
- }
- var args = createArray(arguments);
- return callback.apply(null, args);
- }
- simpleIterator();
- }
- function intervalIterator() {
- task(intervalCallback);
- }
- function intervalCallback(err, res) {
- if (++count === times || !err || (errorFilter && !errorFilter(err))) {
- if (arguments.length <= 2) {
- return callback(err, res);
- }
- var args = createArray(arguments);
- return callback.apply(null, args);
- }
- setTimeout(intervalIterator, intervalFunc(count));
- }
- }
- function retryable(opts, task) {
- if (!task) {
- task = opts;
- opts = null;
- }
- return done;
- function done() {
- var taskFn;
- var args = createArray(arguments);
- var lastIndex = args.length - 1;
- var callback = args[lastIndex];
- switch (task.length) {
- case 1:
- taskFn = task1;
- break;
- case 2:
- taskFn = task2;
- break;
- case 3:
- taskFn = task3;
- break;
- default:
- taskFn = task4;
- }
- if (opts) {
- retry(opts, taskFn, callback);
- } else {
- retry(taskFn, callback);
- }
- function task1(done) {
- task(done);
- }
- function task2(done) {
- task(args[0], done);
- }
- function task3(done) {
- task(args[0], args[1], done);
- }
- function task4(callback) {
- args[lastIndex] = callback;
- task.apply(null, args);
- }
- }
- }
- /**
- * @memberof async
- * @namespace iterator
- */
- function iterator(tasks) {
- var size = 0;
- var keys = [];
- if (isArray(tasks)) {
- size = tasks.length;
- } else {
- keys = nativeKeys(tasks);
- size = keys.length;
- }
- return makeCallback(0);
- function makeCallback(index) {
- var fn = function() {
- if (size) {
- var key = keys[index] || index;
- tasks[key].apply(null, createArray(arguments));
- }
- return fn.next();
- };
- fn.next = function() {
- return index < size - 1 ? makeCallback(index + 1) : null;
- };
- return fn;
- }
- }
- /**
- * @memberof async
- * @namespace apply
- */
- function apply(func) {
- switch (arguments.length) {
- case 0:
- case 1:
- return func;
- case 2:
- return func.bind(null, arguments[1]);
- case 3:
- return func.bind(null, arguments[1], arguments[2]);
- case 4:
- return func.bind(null, arguments[1], arguments[2], arguments[3]);
- case 5:
- return func.bind(null, arguments[1], arguments[2], arguments[3], arguments[4]);
- default:
- var size = arguments.length;
- var index = 0;
- var args = Array(size);
- args[index] = null;
- while (++index < size) {
- args[index] = arguments[index];
- }
- return func.bind.apply(func, args);
- }
- }
- /**
- * @memberof async
- * @namespace timeout
- * @param {Function} func
- * @param {number} millisec
- * @param {*} info
- */
- function timeout(func, millisec, info) {
- var callback, timer;
- return wrappedFunc;
- function wrappedFunc() {
- timer = setTimeout(timeoutCallback, millisec);
- var args = createArray(arguments);
- var lastIndex = args.length - 1;
- callback = args[lastIndex];
- args[lastIndex] = injectedCallback;
- simpleApply(func, args);
- }
- function timeoutCallback() {
- var name = func.name || 'anonymous';
- var err = new Error('Callback function "' + name + '" timed out.');
- err.code = 'ETIMEDOUT';
- if (info) {
- err.info = info;
- }
- timer = null;
- callback(err);
- }
- function injectedCallback() {
- if (timer !== null) {
- simpleApply(callback, createArray(arguments));
- clearTimeout(timer);
- }
- }
- function simpleApply(func, args) {
- switch (args.length) {
- case 0:
- func();
- break;
- case 1:
- func(args[0]);
- break;
- case 2:
- func(args[0], args[1]);
- break;
- default:
- func.apply(null, args);
- break;
- }
- }
- }
- /**
- * @memberof async
- * @namespace times
- * @param {number} n - n >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * var iterator = function(n, done) {
- * done(null, n);
- * };
- * async.times(4, iterator, function(err, res) {
- * console.log(res); // [0, 1, 2, 3];
- * });
- *
- */
- function times(n, iterator, callback) {
- callback = callback || noop;
- n = +n;
- if (isNaN(n) || n < 1) {
- return callback(null, []);
- }
- var result = Array(n);
- timesSync(n, iterate);
- function iterate(num) {
- iterator(num, createCallback(num));
- }
- function createCallback(index) {
- return function(err, res) {
- if (index === null) {
- throwError();
- }
- result[index] = res;
- index = null;
- if (err) {
- callback(err);
- callback = noop;
- } else if (--n === 0) {
- callback(null, result);
- }
- };
- }
- }
- /**
- * @memberof async
- * @namespace timesSeries
- * @param {number} n - n >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * var iterator = function(n, done) {
- * done(null, n);
- * };
- * async.timesSeries(4, iterator, function(err, res) {
- * console.log(res); // [0, 1, 2, 3];
- * });
- *
- */
- function timesSeries(n, iterator, callback) {
- callback = callback || noop;
- n = +n;
- if (isNaN(n) || n < 1) {
- return callback(null, []);
- }
- var result = Array(n);
- var sync = false;
- var completed = 0;
- iterate();
- function iterate() {
- iterator(completed, done);
- }
- function done(err, res) {
- result[completed] = res;
- if (err) {
- callback(err);
- callback = throwError;
- } else if (++completed >= n) {
- callback(null, result);
- callback = throwError;
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- }
- }
- /**
- * @memberof async
- * @namespace timesLimit
- * @param {number} n - n >= 1
- * @param {number} limit - n >= 1
- * @param {Function} iterator
- * @param {Function} callback
- * @example
- *
- * var iterator = function(n, done) {
- * done(null, n);
- * };
- * async.timesLimit(4, 2, iterator, function(err, res) {
- * console.log(res); // [0, 1, 2, 3];
- * });
- *
- */
- function timesLimit(n, limit, iterator, callback) {
- callback = callback || noop;
- n = +n;
- if (isNaN(n) || n < 1 || isNaN(limit) || limit < 1) {
- return callback(null, []);
- }
- var result = Array(n);
- var sync = false;
- var started = 0;
- var completed = 0;
- timesSync(limit > n ? n : limit, iterate);
- function iterate() {
- var index = started++;
- if (index < n) {
- iterator(index, createCallback(index));
- }
- }
- function createCallback(index) {
- return function(err, res) {
- if (index === null) {
- throwError();
- }
- result[index] = res;
- index = null;
- if (err) {
- callback(err);
- callback = noop;
- } else if (++completed >= n) {
- callback(null, result);
- callback = throwError;
- } else if (sync) {
- nextTick(iterate);
- } else {
- sync = true;
- iterate();
- }
- sync = false;
- };
- }
- }
- /**
- * @memberof async
- * @namespace race
- * @param {Array|Object} tasks - functions
- * @param {Function} callback
- * @example
- *
- * // array
- * var called = 0;
- * var tasks = [
- * function(done) {
- * setTimeout(function() {
- * called++;
- * done(null, '1');
- * }, 30);
- * },
- * function(done) {
- * setTimeout(function() {
- * called++;
- * done(null, '2');
- * }, 20);
- * },
- * function(done) {
- * setTimeout(function() {
- * called++;
- * done(null, '3');
- * }, 10);
- * }
- * ];
- * async.race(tasks, function(err, res) {
- * console.log(res); // '3'
- * console.log(called); // 1
- * setTimeout(function() {
- * console.log(called); // 3
- * }, 50);
- * });
- *
- * @example
- *
- * // object
- * var called = 0;
- * var tasks = {
- * 'test1': function(done) {
- * setTimeout(function() {
- * called++;
- * done(null, '1');
- * }, 30);
- * },
- * 'test2': function(done) {
- * setTimeout(function() {
- * called++;
- * done(null, '2');
- * }, 20);
- * },
- * 'test3': function(done) {
- * setTimeout(function() {
- * called++;
- * done(null, '3');
- * }, 10);
- * }
- * };
- * async.race(tasks, function(err, res) {
- * console.log(res); // '3'
- * console.log(called); // 1
- * setTimeout(function() {
- * console.log(called); // 3
- * done();
- * }, 50);
- * });
- *
- */
- function race(tasks, callback) {
- callback = once(callback || noop);
- var size, keys;
- var index = -1;
- if (isArray(tasks)) {
- size = tasks.length;
- while (++index < size) {
- tasks[index](callback);
- }
- } else if (tasks && typeof tasks === obj) {
- keys = nativeKeys(tasks);
- size = keys.length;
- while (++index < size) {
- tasks[keys[index]](callback);
- }
- } else {
- return callback(new TypeError('First argument to race must be a collection of functions'));
- }
- if (!size) {
- callback(null);
- }
- }
- /**
- * @memberof async
- * @namespace memoize
- */
- function memoize(fn, hasher) {
- hasher =
- hasher ||
- function(hash) {
- return hash;
- };
- var memo = {};
- var queues = {};
- var memoized = function() {
- var args = createArray(arguments);
- var callback = args.pop();
- var key = hasher.apply(null, args);
- if (has(memo, key)) {
- nextTick(function() {
- callback.apply(null, memo[key]);
- });
- return;
- }
- if (has(queues, key)) {
- return queues[key].push(callback);
- }
- queues[key] = [callback];
- args.push(done);
- fn.apply(null, args);
- function done(err) {
- var args = createArray(arguments);
- if (!err) {
- memo[key] = args;
- }
- var q = queues[key];
- delete queues[key];
- var i = -1;
- var size = q.length;
- while (++i < size) {
- q[i].apply(null, args);
- }
- }
- };
- memoized.memo = memo;
- memoized.unmemoized = fn;
- return memoized;
- }
- /**
- * @memberof async
- * @namespace unmemoize
- */
- function unmemoize(fn) {
- return function() {
- return (fn.unmemoized || fn).apply(null, arguments);
- };
- }
- /**
- * @memberof async
- * @namespace ensureAsync
- */
- function ensureAsync(fn) {
- return function(/* ...args, callback */) {
- var args = createArray(arguments);
- var lastIndex = args.length - 1;
- var callback = args[lastIndex];
- var sync = true;
- args[lastIndex] = done;
- fn.apply(this, args);
- sync = false;
- function done() {
- var innerArgs = createArray(arguments);
- if (sync) {
- nextTick(function() {
- callback.apply(null, innerArgs);
- });
- } else {
- callback.apply(null, innerArgs);
- }
- }
- };
- }
- /**
- * @memberof async
- * @namespace constant
- */
- function constant(/* values... */) {
- var args = [null].concat(createArray(arguments));
- return function(callback) {
- callback = arguments[arguments.length - 1];
- callback.apply(this, args);
- };
- }
- function asyncify(fn) {
- return function(/* args..., callback */) {
- var args = createArray(arguments);
- var callback = args.pop();
- var result;
- try {
- result = fn.apply(this, args);
- } catch (e) {
- return callback(e);
- }
- if (result && typeof result.then === func) {
- result.then(
- function(value) {
- invokeCallback(callback, null, value);
- },
- function(err) {
- invokeCallback(callback, err && err.message ? err : new Error(err));
- }
- );
- } else {
- callback(null, result);
- }
- };
- }
- function invokeCallback(callback, err, value) {
- try {
- callback(err, value);
- } catch (e) {
- nextTick(rethrow, e);
- }
- }
- function rethrow(error) {
- throw error;
- }
- /**
- * @memberof async
- * @namespace reflect
- * @param {Function} func
- * @return {Function}
- */
- function reflect(func) {
- return function(/* args..., callback */) {
- var callback;
- switch (arguments.length) {
- case 1:
- callback = arguments[0];
- return func(done);
- case 2:
- callback = arguments[1];
- return func(arguments[0], done);
- default:
- var args = createArray(arguments);
- var lastIndex = args.length - 1;
- callback = args[lastIndex];
- args[lastIndex] = done;
- func.apply(this, args);
- }
- function done(err, res) {
- if (err) {
- return callback(null, {
- error: err
- });
- }
- if (arguments.length > 2) {
- res = slice(arguments, 1);
- }
- callback(null, {
- value: res
- });
- }
- };
- }
- /**
- * @memberof async
- * @namespace reflectAll
- * @param {Array[]|Object} tasks
- * @return {Function}
- */
- function reflectAll(tasks) {
- var newTasks, keys;
- if (isArray(tasks)) {
- newTasks = Array(tasks.length);
- arrayEachSync(tasks, iterate);
- } else if (tasks && typeof tasks === obj) {
- keys = nativeKeys(tasks);
- newTasks = {};
- baseEachSync(tasks, iterate, keys);
- }
- return newTasks;
- function iterate(func, key) {
- newTasks[key] = reflect(func);
- }
- }
- /**
- * @memberof async
- * @namespace createLogger
- */
- function createLogger(name) {
- return function(fn) {
- var args = slice(arguments, 1);
- args.push(done);
- fn.apply(null, args);
- };
- function done(err) {
- if (typeof console === obj) {
- if (err) {
- if (console.error) {
- console.error(err);
- }
- return;
- }
- if (console[name]) {
- var args = slice(arguments, 1);
- arrayEachSync(args, function(arg) {
- console[name](arg);
- });
- }
- }
- }
- }
- /**
- * @memberof async
- * @namespace safe
- */
- function safe() {
- createImmediate();
- return exports;
- }
- /**
- * @memberof async
- * @namespace fast
- */
- function fast() {
- createImmediate(false);
- return exports;
- }
- });
|